View Javadoc
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      {
51          setCorrelationId(event.getMessage().getCorrelationId());
52          setCorrelationGroup(String.valueOf(event.getMessage().getCorrelationGroupSize()));
53          setCorrelationSequence(String.valueOf(event.getMessage().getCorrelationSequence()));
54  
55          // only propogate the reply to header if it's in the outbound scope
56          Object replyTo = event.getMessage().getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, PropertyScope.OUTBOUND);
57          if (replyTo != null)
58          {
59              setReplyTo(replyTo.toString());
60          }
61      }
62  
63      /**
64       * Extracts Mule header properties from a Soap message
65       * 
66       * @param soapHeader
67       */
68      public MuleSoapHeaders(SOAPHeader soapHeader)
69      {
70          Iterator iter = soapHeader.examineHeaderElements(MULE_10_ACTOR);
71          SOAPHeaderElement headerElement;
72          while (iter.hasNext())
73          {
74              headerElement = (SOAPHeaderElement)iter.next();
75  
76              // checking that the elements are part of the mule namespace
77              if (org.mule.util.StringUtils.equals(MULE_10_ACTOR, headerElement.getNamespaceURI()))
78              {
79                  Iterator iter2 = headerElement.getChildElements();
80                  readElements(iter2);
81              }
82          }
83      }
84  
85      public MuleSoapHeaders(Iterator elements)
86      {
87          readElements(elements);
88      }
89  
90      protected void readElements(Iterator elements)
91      {
92  
93          SOAPElement element;
94  
95          while (elements.hasNext())
96          {
97  
98              Object elementObject = elements.next();
99  
100             // Fixed MULE-770 (http://mule.mulesoft.org/jira/browse/MULE-770)
101             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                 element = (SOAPElement)elementObject;
106                 String localName = element.getLocalName();
107                 String elementValue = getStringValue(element);
108 
109                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(localName))
110                 {
111                     correlationId = elementValue;
112                 }
113                 else if (MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY.equals(localName))
114                 {
115                     correlationGroup = elementValue;
116                 }
117                 else if (MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY.equals(localName))
118                 {
119                     correlationSequence = elementValue;
120                 }
121                 else if (MuleProperties.MULE_REPLY_TO_PROPERTY.equals(localName))
122                 {
123                     replyTo = elementValue;
124                 }
125 
126             }
127         }
128     }
129 
130     private String getStringValue(Element e)
131     {
132         String value = e.getNodeValue();
133         if (value == null && e.hasChildNodes())
134         {
135             // see if the value is base64 ecoded
136             value = e.getFirstChild().getNodeValue();
137             if (value != null)
138             {
139                 // value = new String(org.apache.axis.encoding.Base64.decode(value));
140             }
141         }
142         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         SOAPHeader header = env.getHeader();
154         SOAPHeaderElement muleHeader;
155         if (correlationId != null || replyTo != null)
156         {
157             if (header == null)
158             {
159                 header = env.addHeader();
160             }
161             Name muleHeaderName = env.createName(MULE_HEADER, MULE_NAMESPACE, MULE_10_ACTOR);
162             muleHeader = header.addHeaderElement(muleHeaderName);
163             muleHeader.setActor(MULE_10_ACTOR);
164         }
165         else
166         {
167             return;
168         }
169 
170         if (correlationId != null)
171         {
172             SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_ID_PROPERTY,
173                 MULE_NAMESPACE);
174             e.addTextNode(correlationId);
175             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
176                 MULE_NAMESPACE);
177             e.addTextNode(correlationGroup);
178             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, MULE_NAMESPACE);
179             e.addTextNode(correlationSequence);
180         }
181         if (replyTo != null)
182         {
183             SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_REPLY_TO_PROPERTY, MULE_NAMESPACE);
184             // String enc = (String)encoder.transform(replyTo);
185             // e.addTextNode(enc);
186             e.addTextNode(replyTo);
187         }
188     }
189 
190     public Element createHeaders() throws Exception
191     {
192         Element muleHeader = null;
193 
194         if (correlationId != null || replyTo != null)
195         {
196             muleHeader = new DOMElement(new QName(MULE_HEADER, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR)));
197         }
198         else
199         {
200             return null;
201         }
202 
203         if (correlationId != null)
204         {
205             Node e = muleHeader.appendChild(new DOMElement(new QName(
206                 MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
207             e.setNodeValue(correlationId);
208 
209             e = muleHeader.appendChild(new DOMElement(new QName(
210                 MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, new Namespace(MULE_NAMESPACE,
211                     MULE_10_ACTOR))));
212             e.setNodeValue(correlationGroup);
213 
214             e = muleHeader.appendChild(new DOMElement(new QName(
215                 MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, new Namespace(MULE_NAMESPACE,
216                     MULE_10_ACTOR))));
217             e.setNodeValue(correlationSequence);
218         }
219         if (replyTo != null)
220         {
221 
222             Node e = muleHeader.appendChild(new DOMElement(new QName(MuleProperties.MULE_REPLY_TO_PROPERTY,
223                 new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
224             e.setNodeValue(replyTo);
225         }
226         return muleHeader;
227     }
228 
229     public String getReplyTo()
230     {
231         return replyTo;
232     }
233 
234     public void setReplyTo(String replyTo)
235     {
236         this.replyTo = replyTo;
237     }
238 
239     public String getCorrelationId()
240     {
241         return correlationId;
242     }
243 
244     public void setCorrelationId(String correlationId)
245     {
246         this.correlationId = correlationId;
247     }
248 
249     public String getCorrelationGroup()
250     {
251         return correlationGroup;
252     }
253 
254     public void setCorrelationGroup(String correlationGroup)
255     {
256         this.correlationGroup = correlationGroup;
257     }
258 
259     public String getCorrelationSequence()
260     {
261         return correlationSequence;
262     }
263 
264     public void setCorrelationSequence(String correlationSequence)
265     {
266         this.correlationSequence = correlationSequence;
267     }
268 }