You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ru...@apache.org on 2008/03/11 10:42:07 UTC

svn commit: r635860 - in /synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: config/xml/ core/axis2/ util/

Author: ruwan
Date: Tue Mar 11 02:42:04 2008
New Revision: 635860

URL: http://svn.apache.org/viewvc?rev=635860&view=rev
Log:
Preparing for the bodyRelative and envelopeRelative xpaths (part 1 of SYNAPSE-235)

Added:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java   (with props)
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java   (with props)
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java   (with props)
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java?rev=635860&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java Tue Mar 11 02:42:04 2008
@@ -0,0 +1,84 @@
+/*
+ *  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.config.xml;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.util.SynapseXPath;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+
+/**
+ * 
+ */
+public class SynapseXPathFactory {
+
+    private static final Log log = LogFactory.getLog(SynapseXPathFactory.class);
+
+    public static SynapseXPath getSynapseXPath(OMElement elem, QName attribName) {
+
+        SynapseXPath xpath = null;
+        OMAttribute xpathAttrib = elem.getAttribute(attribName);
+        try {
+            
+            if (xpathAttrib != null && xpathAttrib.getAttributeValue() != null) {
+
+                AXIOMXPath omXpath = new AXIOMXPath(xpathAttrib.getAttributeValue());
+                xpath = new SynapseXPath(omXpath);
+
+                OMAttribute relativeAttrb
+                    = elem.getAttribute(XMLConfigConstants.ATT_XPATH_RELATIVE);
+
+                if (relativeAttrb != null && relativeAttrb.getAttributeValue() != null) {
+
+                    String relativity = relativeAttrb.getAttributeValue();
+
+                    if (XMLConfigConstants.XPATH_BODY_RELATIVE.equals(relativity)) {
+                        xpath.setBodyRelative(true);
+                    }
+                }
+
+            } else {
+                handleException("Couldn't find the XPath attribute with the QName : "
+                    + attribName.toString() + " in the element : " + elem.toString());
+            }
+            
+        } catch (JaxenException e) {
+            handleException("Error in creating the AXIOMXPath from : " + xpathAttrib, e);
+        }
+
+        return xpath;
+    }
+
+    private static void handleException(String message) {
+        log.error(message);
+        throw new SynapseException(message);
+    }
+
+    private static void handleException(String message, Throwable e) {
+        log.error(message);
+        throw new SynapseException(message);
+    }
+}

Propchange: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java?rev=635860&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java Tue Mar 11 02:42:04 2008
@@ -0,0 +1,79 @@
+/*
+ *  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.config.xml;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.util.SynapseXPath;
+
+/**
+ * 
+ */
+public class SynapseXPathSerializer {
+
+    private static final Log log = LogFactory.getLog(SynapseXPathSerializer.class);
+
+    public static OMElement serializeXPath(SynapseXPath xpath, OMElement elem, String attribName) {
+
+        AXIOMXPath omXPath = xpath.getAXIOMXPath();
+        OMNamespace nullNS = elem.getOMFactory()
+            .createOMNamespace(XMLConfigConstants.NULL_NAMESPACE, "");
+
+        if (omXPath != null) {
+            
+            elem.addAttribute(elem.getOMFactory().createOMAttribute(
+                attribName, nullNS, omXPath.toString()));
+
+            serializeNamespaces(elem, omXPath);
+
+            if (xpath.isBodyRelative()) {
+                
+                elem.addAttribute(elem.getOMFactory().createOMAttribute(
+                    XMLConfigConstants.ATT_XPATH_RELATIVE.getLocalPart(),
+                    nullNS, XMLConfigConstants.XPATH_BODY_RELATIVE));
+            }
+            
+        } else {
+            handleException("Couldn't find the AXIOMXPath in the SynapseXPath");
+        }
+
+        return elem;
+    }
+
+    private static void serializeNamespaces(OMElement elem, AXIOMXPath xpath) {
+
+        for (Object o : xpath.getNamespaces().keySet()) {
+            String prefix = (String) o;
+            String uri = xpath.getNamespaceContext().translateNamespacePrefixToUri(prefix);
+            if (!XMLConfigConstants.SYNAPSE_NAMESPACE.equals(uri)) {
+                elem.declareNamespace(uri, prefix);
+            }
+        }
+    }
+
+    private static void handleException(String message) {
+        log.error(message);
+        throw new SynapseException(message); 
+    }
+}

Propchange: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java?rev=635860&r1=635859&r2=635860&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java Tue Mar 11 02:42:04 2008
@@ -78,4 +78,8 @@
 
     public static final String ONREJECT = "onReject";
 	public static final String ONACCEPT = "onAccept";
+
+    public static final QName ATT_XPATH_RELATIVE = new QName("relative");
+    public static final String XPATH_BODY_RELATIVE = "body";
+    public static final String XPATH_ENVELOPE_RELATIVE = "envelope";
 }

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java?rev=635860&r1=635859&r2=635860&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2MessageContext.java Tue Mar 11 02:42:04 2008
@@ -20,9 +20,9 @@
 package org.apache.synapse.core.axis2;
 
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.llom.OMDocumentImpl;
 import org.apache.axiom.om.impl.llom.OMElementImpl;
 import org.apache.axiom.om.impl.llom.OMTextImpl;
-import org.apache.axiom.om.impl.llom.OMDocumentImpl;
 import org.apache.axiom.om.xpath.AXIOMXPath;
 import org.apache.axiom.soap.SOAPEnvelope;
 import org.apache.axiom.soap.SOAPHeader;
@@ -40,6 +40,7 @@
 import org.apache.synapse.endpoints.Endpoint;
 import org.apache.synapse.mediators.GetPropertyFunction;
 import org.apache.synapse.mediators.MediatorFaultHandler;
+import org.apache.synapse.util.SynapseXPath;
 import org.jaxen.JaxenException;
 import org.jaxen.SimpleFunctionContext;
 import org.jaxen.XPathFunctionContext;
@@ -424,6 +425,11 @@
         axis2MessageContext.setServerSide(value);
     }
 
+    public static String getStringValue(SynapseXPath xpath, MessageContext synCtx) {
+       // default xpath evaluated against the full envelope
+       return getStringValue(xpath.getAXIOMXPath(), synCtx, xpath.isBodyRelative());
+    }
+
     /**
      * Evaluates the given XPath expression against the SOAPEnvelope of the
      * current message and returns a String representation of the result
@@ -450,74 +456,90 @@
 
         synchronized(xpath) {
 
-        if (xpath != null) {
-            try {
-                // create an instance of a synapse:get-property() function and set it to the xpath
-                GetPropertyFunction getPropertyFunc = new GetPropertyFunction();
-                getPropertyFunc.setSynCtx(synCtx);
-
-                // set function context into XPath
-                SimpleFunctionContext fc = new XPathFunctionContext();
-                fc.registerFunction(SynapseConstants.SYNAPSE_NAMESPACE,
-                    "get-property", getPropertyFunc);
-                fc.registerFunction(null, "get-property", getPropertyFunc);
-                xpath.setFunctionContext(fc);
-
-                // register namespace for XPath extension function
-                xpath.addNamespace("synapse", SynapseConstants.SYNAPSE_NAMESPACE);
-                xpath.addNamespace("syn", SynapseConstants.SYNAPSE_NAMESPACE);
-
-            } catch (JaxenException je) {
-                handleException("Error setting up the Synapse XPath " +
-                    "extension function for XPath : " + xpath, je);
-            }
-            try {
-                Object result;
-                if(bodyRelative) {
-                    result = xpath.evaluate(synCtx.getEnvelope().getBody());
+            if (xpath != null) {
+                
+                try {
+                    // create an instance of a synapse:get-property()
+                    // function and set it to the xpath
+                    GetPropertyFunction getPropertyFunc = new GetPropertyFunction();
+                    getPropertyFunc.setSynCtx(synCtx);
+
+                    // set function context into XPath
+                    SimpleFunctionContext fc = new XPathFunctionContext();
+                    fc.registerFunction(SynapseConstants.SYNAPSE_NAMESPACE,
+                        "get-property", getPropertyFunc);
+                    fc.registerFunction(null, "get-property", getPropertyFunc);
+                    xpath.setFunctionContext(fc);
+
+                    // register namespace for XPath extension function
+                    xpath.addNamespace("synapse", SynapseConstants.SYNAPSE_NAMESPACE);
+                    xpath.addNamespace("syn", SynapseConstants.SYNAPSE_NAMESPACE);
+
+                } catch (JaxenException je) {
+                    handleException("Error setting up the Synapse XPath " +
+                        "extension function for XPath : " + xpath, je);
+                }
+                
+                try {
+                    
+                    Object result;
+
+                    // if the xpath is provided as a body relative XPath
+                    if(bodyRelative) {
+                        result = xpath.evaluate(synCtx.getEnvelope().getBody());
+                    // if the XPath is relative to the envelope
+                    } else {
+                        result = xpath.evaluate(synCtx.getEnvelope());
+                    }
+                    
+                    if (result == null) {
+                        return null;
+                    }
 
-                } else {
-                    result = xpath.evaluate(synCtx.getEnvelope());
+                    StringBuffer textValue = new StringBuffer();
+                    if (result instanceof List) {
 
-                }
-                if (result == null) {
-                    return null;
-                }
-                StringBuffer textValue = new StringBuffer();
-                if (result instanceof List) {
-                    List list = (List) result;
-                    Iterator iter = list.iterator();
-                    while (iter.hasNext()) {
-                        Object o = iter.next();
-                        if (o == null && list.size() == 1) {
-                            return null;
-                        }
-                        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();
+                        List list = (List) result;
+                        for (Object o : list) {
+
+                            if (o == null && list.size() == 1) {
+                                return null;
+                            }
+
+                            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());
                             }
-                            textValue.append(s);
-                        } else if (o instanceof OMDocumentImpl) {
-                            textValue.append(
-                                ((OMDocumentImpl) o).getOMDocumentElement().toString());
                         }
+                        
+                    } else {
+                        textValue.append(result.toString());
                     }
-                } else {
-                    textValue.append(result.toString());
+                    
+                    return textValue.toString();
+
+                } catch (JaxenException je) {
+                    handleException("Evaluation of the XPath expression " + xpath.toString() +
+                        " resulted in an error", je);
                 }
-                return textValue.toString();
 
-            } catch (JaxenException je) {
-                handleException("Evaluation of the XPath expression " + xpath.toString() +
-                    " resulted in an error", je);
+            } else {
+                handleException("Invalid (null) XPath expression");
             }
-        } else {
-            handleException("Invalid (null) XPath expression");
-        }
-        return null;
+            
+            return null;
         }
     }
 

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java?rev=635860&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java Tue Mar 11 02:42:04 2008
@@ -0,0 +1,56 @@
+/*
+ *  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.util;
+
+import org.apache.axiom.om.xpath.AXIOMXPath;
+
+/**
+ * 
+ */
+public class SynapseXPath {
+
+    private AXIOMXPath aXIOMXPath;
+    private boolean bodyRelative;
+
+    public SynapseXPath(AXIOMXPath aXIOMXPath) {
+        this.aXIOMXPath = aXIOMXPath;
+    }
+
+    public SynapseXPath(AXIOMXPath aXIOMXPath, boolean bodyRelative) {
+        this.aXIOMXPath = aXIOMXPath;
+        this.bodyRelative = bodyRelative;
+    }
+
+    public AXIOMXPath getAXIOMXPath() {
+        return aXIOMXPath;
+    }
+
+    public void setAXIOMXPath(AXIOMXPath aXIOMXPath) {
+        this.aXIOMXPath = aXIOMXPath;
+    }
+
+    public boolean isBodyRelative() {
+        return bodyRelative;
+    }
+
+    public void setBodyRelative(boolean bodyRelative) {
+        this.bodyRelative = bodyRelative;
+    }
+}

Propchange: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseXPath.java
------------------------------------------------------------------------------
    svn:executable = *