KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > MuleSoapHeaders


1 /*
2  * $Id: MuleSoapHeaders.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.dom4j.Namespace;
14 import org.dom4j.QName;
15 import org.dom4j.dom.DOMElement;
16 import org.mule.config.MuleProperties;
17 import org.mule.umo.UMOEvent;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20
21 import javax.xml.soap.Name JavaDoc;
22 import javax.xml.soap.SOAPElement JavaDoc;
23 import javax.xml.soap.SOAPEnvelope JavaDoc;
24 import javax.xml.soap.SOAPException JavaDoc;
25 import javax.xml.soap.SOAPHeader JavaDoc;
26 import javax.xml.soap.SOAPHeaderElement JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * <code>MuleSoapHeaders</code> is a helper class for extracting and writing Mule
31  * header properties to s Soap message
32  */

33 public class MuleSoapHeaders
34 {
35     private String JavaDoc replyTo;
36     private String JavaDoc correlationId;
37     private String JavaDoc correlationGroup;
38     private String JavaDoc correlationSequence;
39
40     public static final String JavaDoc MULE_10_ACTOR = "http://www.muleumo.org/providers/soap/1.0";
41     public static final String JavaDoc MULE_NAMESPACE = "mule";
42     public static final String JavaDoc MULE_HEADER = "header";
43     public static final String JavaDoc ENV_REQUEST_HEADERS = "MULE_REQUEST_HEADERS";
44
45     /**
46      * Extracts header properties from a Mule event
47      *
48      * @param event
49      */

50     public MuleSoapHeaders(UMOEvent event)
51     {
52         setCorrelationId(event.getMessage().getCorrelationId());
53         setCorrelationGroup(String.valueOf(event.getMessage().getCorrelationGroupSize()));
54         setCorrelationSequence(String.valueOf(event.getMessage().getCorrelationSequence()));
55         setReplyTo((String JavaDoc)event.getMessage().getReplyTo());
56     }
57
58     /**
59      * Extracts Mule header properties from a Soap message
60      *
61      * @param soapHeader
62      */

63     public MuleSoapHeaders(SOAPHeader JavaDoc soapHeader)
64     {
65         Iterator JavaDoc iter = soapHeader.examineHeaderElements(MULE_10_ACTOR);
66         SOAPHeaderElement JavaDoc headerElement;
67         while (iter.hasNext())
68         {
69             headerElement = (SOAPHeaderElement JavaDoc)iter.next();
70
71             // checking that the elements are part of the mule namespace
72
if (org.mule.util.StringUtils.equals(MULE_10_ACTOR, headerElement.getNamespaceURI()))
73             {
74                 Iterator JavaDoc iter2 = headerElement.getChildElements();
75                 readElements(iter2);
76             }
77         }
78     }
79
80     public MuleSoapHeaders(Iterator JavaDoc elements)
81     {
82         readElements(elements);
83     }
84
85     protected void readElements(Iterator JavaDoc elements)
86     {
87
88         SOAPElement JavaDoc element;
89
90         while (elements.hasNext())
91         {
92
93             Object JavaDoc elementObject = elements.next();
94
95             // Fixed MULE-770 (http://mule.mulesource.org/jira/browse/MULE-770)
96
if (elementObject instanceof SOAPElement JavaDoc)
97             // if not, means that it is a value not an element, therefore we cannot
98
// look for correlation_id ...
99
{
100                 element = (SOAPElement JavaDoc)elementObject;
101                 String JavaDoc localName = element.getLocalName();
102                 String JavaDoc elementValue = getStringValue(element);
103
104                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(localName))
105                 {
106                     correlationId = elementValue;
107                 }
108                 else if (MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY.equals(localName))
109                 {
110                     correlationGroup = elementValue;
111                 }
112                 else if (MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY.equals(localName))
113                 {
114                     correlationSequence = elementValue;
115                 }
116                 else if (MuleProperties.MULE_REPLY_TO_PROPERTY.equals(localName))
117                 {
118                     replyTo = elementValue;
119                 }
120
121             }
122         }
123     }
124
125     private String JavaDoc getStringValue(Element e)
126     {
127         String JavaDoc value = e.getNodeValue();
128         if (value == null && e.hasChildNodes())
129         {
130             // see if the value is base64 ecoded
131
value = e.getFirstChild().getNodeValue();
132             if (value != null)
133             {
134                 // value = new String(org.apache.axis.encoding.Base64.decode(value));
135
}
136         }
137         return value;
138     }
139
140     /**
141      * Writes the header properties to a Soap header
142      *
143      * @param env
144      * @throws SOAPException
145      */

146     public void addHeaders(SOAPEnvelope JavaDoc env) throws Exception JavaDoc
147     {
148         SOAPHeader JavaDoc header = env.getHeader();
149         SOAPHeaderElement JavaDoc muleHeader;
150         if (correlationId != null || replyTo != null)
151         {
152             if (header == null)
153             {
154                 header = env.addHeader();
155             }
156             Name muleHeaderName = env.createName(MULE_HEADER, MULE_NAMESPACE, MULE_10_ACTOR);
157             muleHeader = header.addHeaderElement(muleHeaderName);
158             muleHeader.setActor(MULE_10_ACTOR);
159         }
160         else
161         {
162             return;
163         }
164
165         if (correlationId != null)
166         {
167             SOAPElement JavaDoc e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_ID_PROPERTY,
168                 MULE_NAMESPACE);
169             e.addTextNode(correlationId);
170             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
171                 MULE_NAMESPACE);
172             e.addTextNode(correlationGroup);
173             e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, MULE_NAMESPACE);
174             e.addTextNode(correlationSequence);
175         }
176         if (replyTo != null)
177         {
178             SOAPElement JavaDoc e = muleHeader.addChildElement(MuleProperties.MULE_REPLY_TO_PROPERTY, MULE_NAMESPACE);
179             // String enc = (String)encoder.transform(replyTo);
180
// e.addTextNode(enc);
181
e.addTextNode(replyTo);
182         }
183     }
184
185     public Element createHeaders() throws Exception JavaDoc
186     {
187         Element muleHeader = null;
188
189         if (correlationId != null || replyTo != null)
190         {
191             muleHeader = new DOMElement(new QName(MULE_HEADER, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR)));
192         }
193         else
194         {
195             return null;
196         }
197
198         if (correlationId != null)
199         {
200             Node JavaDoc e = muleHeader.appendChild(new DOMElement(new QName(
201                 MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
202             e.setNodeValue(correlationId);
203
204             e = muleHeader.appendChild(new DOMElement(new QName(
205                 MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, new Namespace(MULE_NAMESPACE,
206                     MULE_10_ACTOR))));
207             e.setNodeValue(correlationGroup);
208
209             e = muleHeader.appendChild(new DOMElement(new QName(
210                 MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, new Namespace(MULE_NAMESPACE,
211                     MULE_10_ACTOR))));
212             e.setNodeValue(correlationSequence);
213         }
214         if (replyTo != null)
215         {
216
217             Node JavaDoc e = muleHeader.appendChild(new DOMElement(new QName(MuleProperties.MULE_REPLY_TO_PROPERTY,
218                 new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))));
219             e.setNodeValue(replyTo);
220         }
221         return muleHeader;
222     }
223
224     public String JavaDoc getReplyTo()
225     {
226         return replyTo;
227     }
228
229     public void setReplyTo(String JavaDoc replyTo)
230     {
231         this.replyTo = replyTo;
232     }
233
234     public String JavaDoc getCorrelationId()
235     {
236         return correlationId;
237     }
238
239     public void setCorrelationId(String JavaDoc correlationId)
240     {
241         this.correlationId = correlationId;
242     }
243
244     public String JavaDoc getCorrelationGroup()
245     {
246         return correlationGroup;
247     }
248
249     public void setCorrelationGroup(String JavaDoc correlationGroup)
250     {
251         this.correlationGroup = correlationGroup;
252     }
253
254     public String JavaDoc getCorrelationSequence()
255     {
256         return correlationSequence;
257     }
258
259     public void setCorrelationSequence(String JavaDoc correlationSequence)
260     {
261         this.correlationSequence = correlationSequence;
262     }
263 }
264
Popular Tags