Coverage Report - org.mule.module.cxf.MuleSoapHeaders
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleSoapHeaders
0%
0/88
0%
0/42
2.333
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.cxf;
 8  
 
 9  
 import org.mule.api.MuleEvent;
 10  
 import org.mule.api.config.MuleProperties;
 11  
 import org.mule.api.transport.PropertyScope;
 12  
 
 13  
 import java.util.Iterator;
 14  
 
 15  
 import javax.xml.soap.Name;
 16  
 import javax.xml.soap.SOAPElement;
 17  
 import javax.xml.soap.SOAPEnvelope;
 18  
 import javax.xml.soap.SOAPException;
 19  
 import javax.xml.soap.SOAPHeader;
 20  
 import javax.xml.soap.SOAPHeaderElement;
 21  
 
 22  
 import org.dom4j.Namespace;
 23  
 import org.dom4j.QName;
 24  
 import org.dom4j.dom.DOMElement;
 25  
 import org.w3c.dom.Element;
 26  
 import org.w3c.dom.Node;
 27  
 
 28  
 /**
 29  
  * <code>MuleSoapHeaders</code> is a helper class for extracting and writing Mule
 30  
  * header properties to s Soap message
 31  
  */
 32  
 public class MuleSoapHeaders
 33  
 {
 34  
     private String replyTo;
 35  
     private String correlationId;
 36  
     private String correlationGroup;
 37  
     private String correlationSequence;
 38  
 
 39  
     public static final String MULE_10_ACTOR = "http://www.muleumo.org/providers/soap/1.0";
 40  
     public static final String MULE_NAMESPACE = "mule";
 41  
     public static final String MULE_HEADER = "header";
 42  
     public static final String ENV_REQUEST_HEADERS = "MULE_REQUEST_HEADERS";
 43  
 
 44  
     /**
 45  
      * Extracts header properties from a Mule event
 46  
      * 
 47  
      * @param event
 48  
      */
 49  
     public MuleSoapHeaders(MuleEvent event)
 50  0
     {
 51  0
         setCorrelationId(event.getMessage().getCorrelationId());
 52  0
         setCorrelationGroup(String.valueOf(event.getMessage().getCorrelationGroupSize()));
 53  0
         setCorrelationSequence(String.valueOf(event.getMessage().getCorrelationSequence()));
 54  
 
 55  
         // only propogate the reply to header if it's in the outbound scope
 56  0
         Object replyTo = event.getMessage().getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, PropertyScope.OUTBOUND);
 57  0
         if (replyTo != null)
 58  
         {
 59  0
             setReplyTo(replyTo.toString());
 60  
         }
 61  0
     }
 62  
 
 63  
     /**
 64  
      * Extracts Mule header properties from a Soap message
 65  
      * 
 66  
      * @param soapHeader
 67  
      */
 68  
     public MuleSoapHeaders(SOAPHeader soapHeader)
 69  0
     {
 70  0
         Iterator iter = soapHeader.examineHeaderElements(MULE_10_ACTOR);
 71  
         SOAPHeaderElement headerElement;
 72  0
         while (iter.hasNext())
 73  
         {
 74  0
             headerElement = (SOAPHeaderElement)iter.next();
 75  
 
 76  
             // checking that the elements are part of the mule namespace
 77  0
             if (org.mule.util.StringUtils.equals(MULE_10_ACTOR, headerElement.getNamespaceURI()))
 78  
             {
 79  0
                 Iterator iter2 = headerElement.getChildElements();
 80  0
                 readElements(iter2);
 81  0
             }
 82  
         }
 83  0
     }
 84  
 
 85  
     public MuleSoapHeaders(Iterator elements)
 86  0
     {
 87  0
         readElements(elements);
 88  0
     }
 89  
 
 90  
     protected void readElements(Iterator elements)
 91  
     {
 92  
 
 93  
         SOAPElement element;
 94  
 
 95  0
         while (elements.hasNext())
 96  
         {
 97  
 
 98  0
             Object elementObject = elements.next();
 99  
 
 100  
             // Fixed MULE-770 (http://mule.mulesoft.org/jira/browse/MULE-770)
 101  0
             if (elementObject instanceof SOAPElement)
 102  
             // if not, means that it is a value not an element, therefore we cannot
 103  
             // look for correlation_id ...
 104  
             {
 105  0
                 element = (SOAPElement)elementObject;
 106  0
                 String localName = element.getLocalName();
 107  0
                 String elementValue = getStringValue(element);
 108  
 
 109  0
                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(localName))
 110  
                 {
 111  0
                     correlationId = elementValue;
 112  
                 }
 113  0
                 else if (MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY.equals(localName))
 114  
                 {
 115  0
                     correlationGroup = elementValue;
 116  
                 }
 117  0
                 else if (MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY.equals(localName))
 118  
                 {
 119  0
                     correlationSequence = elementValue;
 120  
                 }
 121  0
                 else if (MuleProperties.MULE_REPLY_TO_PROPERTY.equals(localName))
 122  
                 {
 123  0
                     replyTo = elementValue;
 124  
                 }
 125  
 
 126  
             }
 127  0
         }
 128  0
     }
 129  
 
 130  
     private String getStringValue(Element e)
 131  
     {
 132  0
         String value = e.getNodeValue();
 133  0
         if (value == null && e.hasChildNodes())
 134  
         {
 135  
             // see if the value is base64 ecoded
 136  0
             value = e.getFirstChild().getNodeValue();
 137  0
             if (value != null)
 138  
             {
 139  
                 // value = new String(org.apache.axis.encoding.Base64.decode(value));
 140  
             }
 141  
         }
 142  0
         return value;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Writes the header properties to a Soap header
 147  
      * 
 148  
      * @param env
 149  
      * @throws SOAPException
 150  
      */
 151  
     public void addHeaders(SOAPEnvelope env) throws Exception
 152  
     {
 153  0
         SOAPHeader header = env.getHeader();
 154  
         SOAPHeaderElement muleHeader;
 155  0
         if (correlationId != null || replyTo != null)
 156  
         {
 157  0
             if (header == null)
 158  
             {
 159  0
                 header = env.addHeader();
 160  
             }
 161  0
             Name muleHeaderName = env.createName(MULE_HEADER, MULE_NAMESPACE, MULE_10_ACTOR);
 162  0
             muleHeader = header.addHeaderElement(muleHeaderName);
 163  0
             muleHeader.setActor(MULE_10_ACTOR);
 164  0
         }
 165  
         else
 166  
         {
 167  0
             return;
 168  
         }
 169  
 
 170  0
         if (correlationId != null)
 171  
         {
 172  0
             SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_ID_PROPERTY,
 173  
                 MULE_NAMESPACE);
 174  0
             e.addTextNode(correlationId);
 175  0
             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
 176  
                 MULE_NAMESPACE);
 177  0
             e.addTextNode(correlationGroup);
 178  0
             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, MULE_NAMESPACE);
 179  0
             e.addTextNode(correlationSequence);
 180  
         }
 181  0
         if (replyTo != null)
 182  
         {
 183  0
             SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_REPLY_TO_PROPERTY, MULE_NAMESPACE);
 184  
             // String enc = (String)encoder.transform(replyTo);
 185  
             // e.addTextNode(enc);
 186  0
             e.addTextNode(replyTo);
 187  
         }
 188  0
     }
 189  
 
 190  
     public Element createHeaders() throws Exception
 191  
     {
 192  0
         Element muleHeader = null;
 193  
 
 194  0
         if (correlationId != null || replyTo != null)
 195  
         {
 196  0
             muleHeader = new DOMElement(new QName(MULE_HEADER, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR)));
 197  
         }
 198  
         else
 199  
         {
 200  0
             return null;
 201  
         }
 202  
 
 203  0
         if (correlationId != null)
 204  
         {
 205  0
             Node e = muleHeader.appendChild(new DOMElement(new QName(
 206  
                 MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
 207  0
             e.setNodeValue(correlationId);
 208  
 
 209  0
             e = muleHeader.appendChild(new DOMElement(new QName(
 210  
                 MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, new Namespace(MULE_NAMESPACE,
 211  
                     MULE_10_ACTOR))));
 212  0
             e.setNodeValue(correlationGroup);
 213  
 
 214  0
             e = muleHeader.appendChild(new DOMElement(new QName(
 215  
                 MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, new Namespace(MULE_NAMESPACE,
 216  
                     MULE_10_ACTOR))));
 217  0
             e.setNodeValue(correlationSequence);
 218  
         }
 219  0
         if (replyTo != null)
 220  
         {
 221  
 
 222  0
             Node e = muleHeader.appendChild(new DOMElement(new QName(MuleProperties.MULE_REPLY_TO_PROPERTY,
 223  
                 new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
 224  0
             e.setNodeValue(replyTo);
 225  
         }
 226  0
         return muleHeader;
 227  
     }
 228  
 
 229  
     public String getReplyTo()
 230  
     {
 231  0
         return replyTo;
 232  
     }
 233  
 
 234  
     public void setReplyTo(String replyTo)
 235  
     {
 236  0
         this.replyTo = replyTo;
 237  0
     }
 238  
 
 239  
     public String getCorrelationId()
 240  
     {
 241  0
         return correlationId;
 242  
     }
 243  
 
 244  
     public void setCorrelationId(String correlationId)
 245  
     {
 246  0
         this.correlationId = correlationId;
 247  0
     }
 248  
 
 249  
     public String getCorrelationGroup()
 250  
     {
 251  0
         return correlationGroup;
 252  
     }
 253  
 
 254  
     public void setCorrelationGroup(String correlationGroup)
 255  
     {
 256  0
         this.correlationGroup = correlationGroup;
 257  0
     }
 258  
 
 259  
     public String getCorrelationSequence()
 260  
     {
 261  0
         return correlationSequence;
 262  
     }
 263  
 
 264  
     public void setCorrelationSequence(String correlationSequence)
 265  
     {
 266  0
         this.correlationSequence = correlationSequence;
 267  0
     }
 268  
 }