View Javadoc

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