View Javadoc

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