KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > axis > extensions > MuleSoapHeadersHandler


1 /*
2  * $Id: MuleSoapHeadersHandler.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.axis.extensions;
12
13 import org.apache.axis.AxisFault;
14 import org.apache.axis.MessageContext;
15 import org.apache.axis.handlers.BasicHandler;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.config.MuleProperties;
19 import org.mule.providers.soap.MuleSoapHeaders;
20 import org.mule.umo.UMOEvent;
21
22 import javax.xml.soap.SOAPEnvelope JavaDoc;
23 import javax.xml.soap.SOAPMessage JavaDoc;
24
25 /**
26  * <code>MuleSoapHeadersHandler</code> is an Axis handler that can read and write
27  * Mule header properties to a Soap message
28  *
29  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30  * @version $Revision: 3798 $
31  */

32 public class MuleSoapHeadersHandler extends BasicHandler
33 {
34     /**
35      * Serial version
36      */

37     private static final long serialVersionUID = 1813393257662701953L;
38
39     /**
40      * logger used by this class
41      */

42     protected static Log logger = LogFactory.getLog(MuleSoapHeadersHandler.class);
43
44     public void invoke(MessageContext msgContext) throws AxisFault
45     {
46         boolean setMustUnderstand = msgContext.isPropertyTrue("MULE_HEADER_MUST_UNDERSTAND");
47
48         try
49         {
50             if (msgContext.isClient())
51             {
52                 if (!msgContext.getPastPivot())
53                 {
54                     processClientRequest(msgContext, setMustUnderstand);
55                     if (logger.isDebugEnabled())
56                     {
57                         logger.debug("After Client Request, Message is:\n"
58                                      + msgContext.getRequestMessage().getSOAPPartAsString());
59                     }
60                 }
61                 else
62                 {
63                     processClientResponse(msgContext);
64                     if (logger.isDebugEnabled())
65                     {
66                         logger.debug("After Client Response, Message is:\n"
67                                      + msgContext.getRequestMessage().getSOAPPartAsString());
68                     }
69                 }
70             }
71             else
72             {
73                 if (!msgContext.getPastPivot())
74                 {
75                     processServerRequest(msgContext);
76                     if (logger.isDebugEnabled())
77                     {
78                         logger.debug("After Server Request, Message is:\n"
79                                      + msgContext.getRequestMessage().getSOAPPartAsString());
80                     }
81                 }
82                 else
83                 {
84                     processServerResponse(msgContext, setMustUnderstand);
85                     if (logger.isDebugEnabled())
86                     {
87                         logger.debug("After Server Response, Message is:\n"
88                                      + msgContext.getRequestMessage().getSOAPPartAsString());
89                     }
90                 }
91             }
92         }
93         catch (Exception JavaDoc e)
94         {
95             throw AxisFault.makeFault(e);
96         }
97     }
98
99     /**
100      * Method processClientRequest
101      *
102      * @param msgContext
103      */

104     protected synchronized void processClientRequest(MessageContext msgContext, boolean setMustUnderstand)
105         throws Exception JavaDoc
106     {
107         SOAPMessage JavaDoc msg = msgContext.getMessage();
108         if (msg == null)
109         {
110             return;
111         }
112         UMOEvent event = (UMOEvent)msgContext.getProperty(MuleProperties.MULE_EVENT_PROPERTY);
113
114         if (event == null)
115         {
116             return;
117         }
118         else
119         {
120             synchronized (msgContext)
121             {
122                 MuleSoapHeaders headers = new MuleSoapHeaders(event);
123                 headers.addHeaders(msgContext.getMessage().getSOAPPart().getEnvelope());
124             }
125         }
126     }
127
128     /**
129      * Method processClientResponse
130      *
131      * @param msgContext
132      */

133     protected void processClientResponse(MessageContext msgContext) throws Exception JavaDoc
134     {
135         SOAPMessage JavaDoc msg = msgContext.getMessage();
136         if (msg == null)
137         {
138             return;
139         }
140         SOAPEnvelope JavaDoc env = msg.getSOAPPart().getEnvelope();
141         MuleSoapHeaders headers = new MuleSoapHeaders(env.getHeader());
142
143         if (headers.getCorrelationId() != null)
144         {
145             msgContext.setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, headers.getCorrelationId());
146         }
147         if (headers.getCorrelationGroup() != null)
148         {
149             msgContext.setProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
150                 headers.getCorrelationGroup());
151         }
152         if (headers.getCorrelationSequence() != null)
153         {
154             msgContext.setProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY,
155                 headers.getCorrelationSequence());
156         }
157
158         if (headers.getReplyTo() != null)
159         {
160             msgContext.setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, headers.getReplyTo());
161         }
162     }
163
164     /**
165      * Method processServerRequest
166      *
167      * @param msgContext
168      * @throws Exception
169      */

170     protected void processServerRequest(MessageContext msgContext) throws Exception JavaDoc
171     {
172         SOAPMessage JavaDoc msg = msgContext.getMessage();
173         if (msg == null)
174         {
175             return;
176         }
177         MuleSoapHeaders headers = new MuleSoapHeaders(msg.getSOAPPart().getEnvelope().getHeader());
178         msgContext.setProperty(MuleSoapHeaders.ENV_REQUEST_HEADERS, headers);
179     }
180
181     /**
182      * Method processServerResponse
183      *
184      * @param msgContext
185      */

186     protected void processServerResponse(MessageContext msgContext, boolean setMustUnderstand)
187         throws Exception JavaDoc
188     {
189         SOAPMessage JavaDoc msg = msgContext.getMessage();
190         if (msg == null)
191         {
192             return;
193         }
194         MuleSoapHeaders headers = (MuleSoapHeaders)msgContext.getProperty(MuleSoapHeaders.ENV_REQUEST_HEADERS);
195
196         if (headers == null)
197         {
198             return;
199         }
200         else
201         {
202             headers.addHeaders(msgContext.getMessage().getSOAPPart().getEnvelope());
203         }
204     }
205
206 }
207
Popular Tags