You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by hi...@apache.org on 2010/09/11 15:05:21 UTC

svn commit: r996140 - in /synapse/trunk/java/modules/commons/src: main/java/org/apache/synapse/commons/evaluators/ main/java/org/apache/synapse/commons/evaluators/source/ test/java/org/apache/synapse/commons/evaluators/source/

Author: hiranya
Date: Sat Sep 11 13:05:21 2010
New Revision: 996140

URL: http://svn.apache.org/viewvc?rev=996140&view=rev
Log:
Extending the evaluator framework to support evaluation of properties and SOAP payload content (SYNAPSE-684). Also added some tests for the older text retriever implementations.
 

Added:
    synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/PropertyTextRetriever.java
    synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
    synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/
    synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java
Modified:
    synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/EvaluatorContext.java

Modified: synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/EvaluatorContext.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/EvaluatorContext.java?rev=996140&r1=996139&r2=996140&view=diff
==============================================================================
--- synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/EvaluatorContext.java (original)
+++ synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/EvaluatorContext.java Sat Sep 11 13:05:21 2010
@@ -20,21 +20,24 @@
 package org.apache.synapse.commons.evaluators;
 
 import org.apache.axis2.transport.http.util.URIEncoderDecoder;
+import org.apache.axis2.context.MessageContext;
 
 import java.io.UnsupportedEncodingException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * Holds the information about the HTTP request. Created on per request basis and
  * passed to each and every evaluator.
  */
 public class EvaluatorContext {
-    private String url;
 
+    private String url;
     private Map<String, String> headers;
-
     private Map<String, String> params;
+    private MessageContext messageContext;
+    private Properties properties;
 
     /**
      * Creates the Evalutor context with the URL and the set of HTTP headers
@@ -114,6 +117,28 @@ public class EvaluatorContext {
     }
 
     /**
+     * Get the message context associated with this evaluator context
+     *
+     * @return an Axis2 MessageContext instance or null
+     */
+    public MessageContext getMessageContext() {
+        return messageContext;
+    }
+
+    /**
+     * Get the value of the named property
+     *
+     * @param name Name of the property
+     * @return A string property value or null
+     */
+    public String getProperty(String name) {
+        if (properties != null) {
+            return properties.getProperty(name);
+        }
+        return null;
+    }
+
+    /**
      * Set the URL
      * @param url to be set
      */
@@ -136,4 +161,22 @@ public class EvaluatorContext {
     public void setParams(Map<String, String> params) {
         this.params = params;
     }
+
+    /**
+     * Set the current Axis2 MessageContext to this evaluator context
+     *
+     * @param messageContext an Axis2 MessageContext object
+     */
+    public void setMessageContext(MessageContext messageContext) {
+        this.messageContext = messageContext;
+    }
+
+    /**
+     * Associate a set of properties with this evaluator context
+     *
+     * @param properties a Properties map
+     */
+    public void setProperties(Properties properties) {
+        this.properties = properties;
+    }
 }

Added: synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/PropertyTextRetriever.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/PropertyTextRetriever.java?rev=996140&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/PropertyTextRetriever.java (added)
+++ synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/PropertyTextRetriever.java Sat Sep 11 13:05:21 2010
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.commons.evaluators.source;
+
+import org.apache.synapse.commons.evaluators.EvaluatorContext;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+
+public class PropertyTextRetriever implements SourceTextRetriever {
+
+    private String key;
+
+    public PropertyTextRetriever(String key) {
+        this.key = key;
+    }
+
+    public String getSourceText(EvaluatorContext context) throws EvaluatorException {
+        return context.getProperty(key);
+    }
+
+    public String getKey() {
+        return key;
+    }
+}

Added: synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java?rev=996140&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java (added)
+++ synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java Sat Sep 11 13:05:21 2010
@@ -0,0 +1,88 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.commons.evaluators.source;
+
+import org.apache.synapse.commons.evaluators.EvaluatorContext;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.axiom.om.impl.llom.OMTextImpl;
+import org.apache.axiom.om.impl.llom.OMElementImpl;
+import org.apache.axiom.om.impl.llom.OMDocumentImpl;
+import org.apache.axiom.om.OMAttribute;
+import org.jaxen.JaxenException;
+
+import java.util.List;
+
+public class SOAPEnvelopeTextRetriever implements SourceTextRetriever {
+
+    private String xpath;
+
+    public SOAPEnvelopeTextRetriever(String xpath) {
+        this.xpath = xpath;
+    }
+
+    public String getSourceText(EvaluatorContext context) throws EvaluatorException {
+        SOAPEnvelope envelope = context.getMessageContext().getEnvelope();
+        try {
+            AXIOMXPath axiomXPath = new AXIOMXPath(xpath);
+            Object result = axiomXPath.evaluate(envelope);
+
+            if (result instanceof List) {
+                List list = (List) result;
+                if (list.size() == 1 && list.get(0) == null) {
+                    return null;
+                }
+
+                StringBuffer textValue = new StringBuffer();
+                for (Object o : list) {
+                    if (o instanceof OMTextImpl) {
+                        textValue.append(((OMTextImpl) o).getText());
+
+                    } else if (o instanceof OMElementImpl) {
+                        String s = ((OMElementImpl) o).getText();
+                        if (s.trim().length() == 0) {
+                            s = o.toString();
+                        }
+                        textValue.append(s);
+
+                    } else if (o instanceof OMDocumentImpl) {
+                        textValue.append(((OMDocumentImpl) o).getOMDocumentElement().toString());
+                    } else if (o instanceof OMAttribute) {
+                        textValue.append(((OMAttribute) o).getAttributeValue());
+                    }
+                }
+
+                return textValue.toString();
+
+            } else {
+                return result.toString();
+            }
+
+
+        } catch (JaxenException e) {
+            throw new EvaluatorException("", e);
+        }
+    }
+
+    public String getXpath() {
+        return xpath;
+    }
+}

Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java?rev=996140&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java Sat Sep 11 13:05:21 2010
@@ -0,0 +1,85 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.commons.evaluators.source;
+
+import junit.framework.TestCase;
+import org.apache.synapse.commons.evaluators.EvaluatorContext;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+import org.apache.synapse.commons.evaluators.EvaluatorConstants;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+import java.util.HashMap;
+
+public class SourceTextRetrieverTest extends TestCase {
+
+    public void testURLTextRetriever() throws EvaluatorException {
+        try {
+            URI uri = new URI("http://host:9000/path");
+            EvaluatorContext context = new EvaluatorContext(uri.toString(), null);
+
+            URLTextRetriever txtRtvr = new URLTextRetriever();
+            assertEquals(uri.toString(), txtRtvr.getSourceText(context));
+
+            txtRtvr.setFragment(EvaluatorConstants.URI_FRAGMENTS.host.name());
+            assertEquals(uri.getHost(), txtRtvr.getSourceText(context));
+
+            txtRtvr.setFragment(EvaluatorConstants.URI_FRAGMENTS.port.name());
+            assertEquals(String.valueOf(uri.getPort()), txtRtvr.getSourceText(context));
+
+            txtRtvr.setFragment(EvaluatorConstants.URI_FRAGMENTS.protocol.name());
+            assertEquals(uri.getScheme(), txtRtvr.getSourceText(context));
+
+            txtRtvr.setFragment(EvaluatorConstants.URI_FRAGMENTS.path.name());
+            assertEquals(uri.getPath(), txtRtvr.getSourceText(context));
+        } catch (URISyntaxException ignore) {
+
+        }
+    }
+
+    public void testHeaderTextRetriever() throws EvaluatorException {
+        Map<String,String> headers = new HashMap<String,String>();
+        headers.put("key1", "value1");
+        headers.put("key2", "value2");
+        EvaluatorContext context = new EvaluatorContext(null, headers);
+
+        HeaderTextRetriever txtRtvr = new HeaderTextRetriever("key1");
+        assertEquals(headers.get("key1"), txtRtvr.getSourceText(context));
+
+        txtRtvr = new HeaderTextRetriever("bogusKey");
+        assertNull(txtRtvr.getSourceText(context));
+    }
+
+    public void testParameterTextRetriever() throws EvaluatorException {
+        String url = "http://host:9000/path?key1=value1&key2=value2";
+        EvaluatorContext context = new EvaluatorContext(url, null);
+
+        ParameterTextRetriever txtRtvr = new ParameterTextRetriever("key1");
+        assertEquals("value1", txtRtvr.getSourceText(context));
+
+        txtRtvr = new ParameterTextRetriever("key2");
+        assertEquals("value2", txtRtvr.getSourceText(context));
+
+        txtRtvr = new ParameterTextRetriever("bogusKey");
+        assertNull(txtRtvr.getSourceText(context));
+    }
+
+}