Home » synapse-1.2-src » org.apache.synapse.mediators.eip » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.mediators.eip;
   21   
   22   import org.apache.axiom.om.OMElement;
   23   import org.apache.axiom.om.OMNode;
   24   import org.apache.axiom.soap.SOAPEnvelope;
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   import org.apache.synapse.util.xpath.SynapseXPath;
   28   import org.jaxen.JaxenException;
   29   
   30   import java.util.ArrayList;
   31   import java.util.Iterator;
   32   import java.util.List;
   33   
   34   /**
   35    * Utility methods for the EIP mediators
   36    */
   37   public class EIPUtils {
   38   
   39       private static final Log log = LogFactory.getLog(EIPUtils.class);
   40   
   41       /**
   42        * Return the set of elements specified by the XPath over the given envelope
   43        *
   44        * @param envelope SOAPEnvelope from which the elements will be extracted
   45        * @param expression SynapseXPath expression describing the elements to be extracted
   46        * @return List OMElements in the envelope matching the expression
   47        * @throws JaxenException if the XPath expression evaluation fails
   48        */
   49       public static List getMatchingElements(SOAPEnvelope envelope, SynapseXPath expression)
   50           throws JaxenException {
   51   
   52           Object o = expression.evaluate(envelope);
   53           if (o instanceof OMNode) {
   54               List list = new ArrayList();
   55               list.add(o);
   56               return list;
   57           } else if (o instanceof List) {
   58               return (List) o;
   59           } else {
   60               return new ArrayList();
   61           }
   62       }
   63   
   64       /**
   65        * Return the set of detached elements specified by the XPath over the given envelope
   66        *
   67        * @param envelope SOAPEnvelope from which the elements will be extracted
   68        * @param expression SynapseXPath expression describing the elements to be extracted
   69        * @return List detached OMElements in the envelope matching the expression
   70        * @throws JaxenException if the XPath expression evaluation fails
   71        */
   72       public static List<OMNode> getDetachedMatchingElements(SOAPEnvelope envelope, SynapseXPath expression)
   73           throws JaxenException {
   74   
   75           List<OMNode> elementList = new ArrayList<OMNode>();
   76           Object o = expression.evaluate(envelope);
   77           if (o instanceof OMNode) {
   78               elementList.add(((OMNode) o).detach());
   79           } else if (o instanceof List) {
   80               for (Object elem : (List) o) {
   81                   if (elem instanceof OMNode) {
   82                       elementList.add(((OMNode) elem).detach());
   83                   }
   84               }
   85           }
   86           return elementList;
   87       }
   88   
   89       /**
   90        * Merge two SOAP envelopes using the given XPath expression that specifies the
   91        * element that enriches the first envelope from the second
   92        *
   93        * @param envelope   SOAPEnvelope to be enriched with the content
   94        * @param enricher   SOAPEnvelope from which the enriching element will be extracted
   95        * @param expression SynapseXPath describing the enriching element
   96        * @throws JaxenException on failing of processing the xpath
   97        */
   98       public static void enrichEnvelope(SOAPEnvelope envelope, SOAPEnvelope enricher,
   99           SynapseXPath expression) throws JaxenException {
  100   
  101           OMElement enrichingElement;
  102           List elementList = getMatchingElements(envelope, expression);
  103   
  104           if (elementList != null && !elementList.isEmpty()) {
  105   
  106               // attach at parent of the first result from the XPath, or to the SOAPBody
  107               Object o = elementList.get(0);
  108   
  109               if (o instanceof OMElement &&
  110                   ((OMElement) o).getParent() != null &&
  111                   ((OMElement) o).getParent() instanceof OMElement) {
  112                   enrichingElement = (OMElement) ((OMElement) o).getParent();
  113               } else {
  114                   enrichingElement = envelope.getBody();
  115               }
  116   
  117               List list = getMatchingElements(enricher, expression);
  118               if (list != null) {
  119                   Iterator itr = list.iterator();
  120                   while (itr.hasNext()) {
  121                       o = itr.next();
  122                       if (o != null && o instanceof OMElement) {
  123                           enrichingElement.addChild((OMElement) o);
  124                       }
  125                   }
  126               }
  127           }
  128       }    
  129   }

Home » synapse-1.2-src » org.apache.synapse.mediators.eip » [javadoc | source]