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