1 10 11 package org.mule.providers.soap.axis.extensions; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 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 54 public class UniversalSender extends BasicHandler 55 { 56 59 private static final long serialVersionUID = 7943380365092172940L; 60 61 64 protected transient Log logger = LogFactory.getLog(getClass()); 65 66 protected Map endpointsCache = new HashMap (); 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 ( 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 String 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 payload = null; 107 int contentLength = 0; 108 if (msgContext.getRequestMessage().countAttachments() > 0) 109 { 110 File temp = File.createTempFile("soap", ".tmp"); 111 temp.deleteOnExit(); FileOutputStream fos = new FileOutputStream (temp); 114 msgContext.getRequestMessage().writeTo(fos); 115 fos.close(); 116 contentLength = (int)temp.length(); 117 payload = new FileInputStream (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 props = new HashMap (); 128 for (Iterator iterator = msgContext.getPropertyNames(); iterator.hasNext();) 130 { 131 String name = (String )iterator.next(); 132 if (!name.equals("call_object") && !name.equals("wsdl.service")) 133 { 134 props.put(name, msgContext.getProperty(name)); 135 } 136 } 137 138 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)); } 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 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 if (payload instanceof File ) 197 { 198 ((File )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 e) 211 { 212 throw new AxisFault(e.getMessage(), new Throwable (e)); 213 } 214 215 } 216 217 protected UMOEndpoint lookupEndpoint(String 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 iterator = router.getRouters().iterator(); iterator.hasNext();) 260 { 261 UMOOutboundRouter r = (UMOOutboundRouter)iterator.next(); 262 for (Iterator 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 |