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