KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: UniversalSender.java 3982 2006-11-22 14:28:01Z lajos $
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 java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Message;
22 import org.apache.axis.MessageContext;
23 import org.apache.axis.client.Call;
24 import org.apache.axis.handlers.BasicHandler;
25 import org.apache.commons.io.output.ByteArrayOutputStream;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.mule.MuleManager;
29 import org.mule.config.MuleProperties;
30 import org.mule.impl.MuleEvent;
31 import org.mule.impl.MuleMessage;
32 import org.mule.impl.MuleSession;
33 import org.mule.impl.RequestContext;
34 import org.mule.impl.endpoint.MuleEndpoint;
35 import org.mule.impl.endpoint.MuleEndpointURI;
36 import org.mule.providers.AbstractConnector;
37 import org.mule.providers.http.HttpConstants;
38 import org.mule.providers.soap.axis.AxisConnector;
39 import org.mule.providers.soap.axis.extras.AxisCleanAndAddProperties;
40 import org.mule.umo.UMODescriptor;
41 import org.mule.umo.UMOEvent;
42 import org.mule.umo.UMOException;
43 import org.mule.umo.UMOMessage;
44 import org.mule.umo.UMOSession;
45 import org.mule.umo.endpoint.UMOEndpoint;
46 import org.mule.umo.endpoint.UMOEndpointURI;
47 import org.mule.umo.endpoint.UMOImmutableEndpoint;
48 import org.mule.umo.routing.UMOOutboundMessageRouter;
49 import org.mule.umo.routing.UMOOutboundRouter;
50
51 /**
52  * TODO document
53  */

54 public class UniversalSender extends BasicHandler
55 {
56     /**
57      * Serial version
58      */

59     private static final long serialVersionUID = 7943380365092172940L;
60
61     /**
62      * logger used by this class
63      */

64     protected transient Log logger = LogFactory.getLog(getClass());
65
66     protected Map JavaDoc endpointsCache = new HashMap JavaDoc();
67
68     public void invoke(MessageContext msgContext) throws AxisFault
69     {
70         boolean sync = true;
71         Call call = (Call)msgContext.getProperty("call_object");
72         if (call == null)
73         {
74             throw new IllegalStateException JavaDoc(
75                 "The call_object property must be set on the message context to the client Call object");
76         }
77         if (Boolean.TRUE.equals(call.getProperty("axis.one.way")))
78         {
79             sync = false;
80         }
81         // Get the event stored in call
82
// If a receive call is made there will be no event
83
// UMOEvent event =
84
// (UMOEvent)call.getProperty(MuleProperties.MULE_EVENT_PROPERTY);
85
// Get the dispatch endpoint
86
String JavaDoc uri = msgContext.getStrProp(MessageContext.TRANS_URL);
87         UMOImmutableEndpoint requestEndpoint = (UMOImmutableEndpoint)call.getProperty(MuleProperties.MULE_ENDPOINT_PROPERTY);
88         UMOImmutableEndpoint endpoint = null;
89         try
90         {
91             endpoint = lookupEndpoint(uri);
92         }
93         catch (UMOException e)
94         {
95             requestEndpoint.getConnector().handleException(e);
96             return;
97         }
98
99         try
100         {
101             if (requestEndpoint.getConnector() instanceof AxisConnector)
102             {
103                 msgContext.setTypeMappingRegistry(((AxisConnector)requestEndpoint.getConnector()).getAxisServer()
104                     .getTypeMappingRegistry());
105             }
106             Object JavaDoc payload = null;
107             int contentLength = 0;
108             if (msgContext.getRequestMessage().countAttachments() > 0)
109             {
110                 File JavaDoc temp = File.createTempFile("soap", ".tmp");
111                 temp.deleteOnExit(); // TODO cleanup files earlier (IOUtils has a
112
// file tracker)
113
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(temp);
114                 msgContext.getRequestMessage().writeTo(fos);
115                 fos.close();
116                 contentLength = (int)temp.length();
117                 payload = new FileInputStream JavaDoc(temp);
118             }
119             else
120             {
121                 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
122                 msgContext.getRequestMessage().writeTo(baos);
123                 baos.close();
124                 payload = baos.toByteArray();
125             }
126
127             Map JavaDoc props = new HashMap JavaDoc();
128             // props.putAll(event.getProperties());
129
for (Iterator JavaDoc iterator = msgContext.getPropertyNames(); iterator.hasNext();)
130             {
131                 String JavaDoc name = (String JavaDoc)iterator.next();
132                 if (!name.equals("call_object") && !name.equals("wsdl.service"))
133                 {
134                     props.put(name, msgContext.getProperty(name));
135                 }
136             }
137
138             // add all custom headers, filter out all mule headers (such as
139
// MULE_SESSION) except
140
// for MULE_USER header. Filter out other headers like "soapMethods" and
141
// MuleProperties.MULE_METHOD_PROPERTY and "soapAction"
142
// and also filter out any http related header
143
if ((RequestContext.getEvent() != null) && (RequestContext.getEvent().getMessage() != null))
144             {
145                 props = AxisCleanAndAddProperties.cleanAndAdd(RequestContext.getEventContext());
146             }
147
148             if (call.useSOAPAction())
149             {
150                 uri = call.getSOAPActionURI();
151             }
152             props.put("SOAPAction", uri);
153             if (contentLength > 0)
154             {
155                 props.put(HttpConstants.HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); // necessary
156
// for
157
// supporting
158
// httpclient
159
}
160
161             if (props.get(HttpConstants.HEADER_CONTENT_TYPE) == null)
162             {
163                 props.put(HttpConstants.HEADER_CONTENT_TYPE, "text/xml");
164             }
165             UMOMessage message = new MuleMessage(payload, props);
166             UMOSession session = new MuleSession(message,
167                 ((AbstractConnector)endpoint.getConnector()).getSessionHandler());
168
169             UMOEvent dispatchEvent = new MuleEvent(message, endpoint, session, sync);
170             logger.info("Making Axis soap request on: " + uri);
171             if (logger.isDebugEnabled())
172             {
173                 logger.debug("Soap request is:\n" + payload.toString());
174             }
175             if (sync)
176             {
177                 // We need to rewrite the endpoint on the event to set the
178
// reomoteSync property
179
MuleEndpoint syncEndpoint = new MuleEndpoint(dispatchEvent.getEndpoint());
180                 syncEndpoint.setRemoteSync(true);
181                 dispatchEvent = new MuleEvent(dispatchEvent.getMessage(), syncEndpoint,
182                     dispatchEvent.getSession(), dispatchEvent.isSynchronous());
183                 UMOMessage result = session.sendEvent(dispatchEvent);
184                 if (result != null)
185                 {
186                     byte[] response = result.getPayloadAsBytes();
187                     Message responseMessage = new Message(response);
188                     msgContext.setResponseMessage(responseMessage);
189
190                 }
191                 else
192                 {
193                     logger.warn("No response message was returned from synchronous call to: " + uri);
194                 }
195                 // remove temp file created for streaming
196
if (payload instanceof File JavaDoc)
197                 {
198                     ((File JavaDoc)payload).delete();
199                 }
200             }
201             else
202             {
203                 session.dispatchEvent(dispatchEvent);
204             }
205         }
206         catch (AxisFault axisFault)
207         {
208             throw axisFault;
209         }
210         catch (Exception JavaDoc e)
211         {
212             throw new AxisFault(e.getMessage(), new Throwable JavaDoc(e));
213         }
214
215     }
216
217     protected UMOEndpoint lookupEndpoint(String JavaDoc uri) throws UMOException
218     {
219         UMODescriptor axis = MuleManager.getInstance().getModel().getDescriptor(
220             AxisConnector.AXIS_SERVICE_COMPONENT_NAME);
221         UMOEndpointURI endpoint = new MuleEndpointURI(uri);
222         UMOEndpoint ep;
223         if (axis != null)
224         {
225             synchronized (endpointsCache)
226             {
227                 ep = (UMOEndpoint)endpointsCache.get(endpoint.getAddress());
228                 if (ep == null)
229                 {
230                     updateEndpointCache(axis.getOutboundRouter());
231                     ep = (UMOEndpoint)endpointsCache.get(endpoint.getAddress());
232                     if (ep == null)
233                     {
234                         logger.debug("Dispatch Endpoint uri: " + uri
235                                      + " not found on the cache. Creating the endpoint instead.");
236                         ep = new MuleEndpoint(uri, false);
237                     }
238                     else
239                     {
240                         logger.info("Found endpoint: " + uri + " on the Axis service component");
241                     }
242                 }
243                 else
244                 {
245                     logger.info("Found endpoint: " + uri + " on the Axis service component");
246                 }
247             }
248         }
249         else
250         {
251             ep = new MuleEndpoint(uri, false);
252         }
253         return ep;
254     }
255
256     private void updateEndpointCache(UMOOutboundMessageRouter router)
257     {
258         endpointsCache.clear();
259         for (Iterator JavaDoc iterator = router.getRouters().iterator(); iterator.hasNext();)
260         {
261             UMOOutboundRouter r = (UMOOutboundRouter)iterator.next();
262             for (Iterator JavaDoc iterator1 = r.getEndpoints().iterator(); iterator1.hasNext();)
263             {
264                 UMOEndpoint endpoint = (UMOEndpoint)iterator1.next();
265                 endpointsCache.put(endpoint.getEndpointURI().getAddress(), endpoint);
266             }
267         }
268     }
269 }
270
Popular Tags