1 10 11 package org.mule.providers.soap.xfire; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.lang.reflect.Constructor ; 17 import java.util.Enumeration ; 18 19 import javax.xml.stream.XMLStreamException; 20 21 import org.apache.commons.io.output.ByteArrayOutputStream; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.codehaus.xfire.XFire; 25 import org.codehaus.xfire.service.Service; 26 import org.codehaus.xfire.service.ServiceRegistry; 27 import org.codehaus.xfire.transport.Transport; 28 import org.codehaus.xfire.transport.TransportManager; 29 import org.codehaus.xfire.transport.http.HtmlServiceWriter; 30 import org.mule.MuleRuntimeException; 31 import org.mule.config.i18n.Message; 32 import org.mule.config.i18n.Messages; 33 import org.mule.impl.MuleDescriptor; 34 import org.mule.impl.MuleMessage; 35 import org.mule.impl.UMODescriptorAware; 36 import org.mule.providers.http.HttpConnector; 37 import org.mule.providers.http.HttpConstants; 38 import org.mule.providers.soap.SoapConstants; 39 import org.mule.providers.soap.xfire.transport.MuleLocalChannel; 40 import org.mule.providers.soap.xfire.transport.MuleLocalTransport; 41 import org.mule.providers.soap.xfire.transport.MuleUniversalTransport; 42 import org.mule.providers.streaming.OutStreamMessageAdapter; 43 import org.mule.providers.streaming.StreamMessageAdapter; 44 import org.mule.umo.UMODescriptor; 45 import org.mule.umo.UMOEventContext; 46 import org.mule.umo.UMOException; 47 import org.mule.umo.UMOMessage; 48 import org.mule.umo.lifecycle.Callable; 49 import org.mule.umo.lifecycle.Initialisable; 50 import org.mule.umo.lifecycle.InitialisationException; 51 import org.mule.umo.lifecycle.Lifecycle; 52 import org.mule.umo.manager.UMOWorkManager; 53 import org.mule.umo.provider.UMOStreamMessageAdapter; 54 import org.mule.util.StringUtils; 55 56 61 public class XFireServiceComponent implements Callable, Initialisable, Lifecycle, UMODescriptorAware 62 { 63 66 protected transient Log logger = LogFactory.getLog(getClass()); 67 68 protected XFire xfire; 69 70 protected Transport transport; 72 protected Transport universalTransport; 73 protected String transportClass; 74 75 public void setDescriptor(UMODescriptor descriptor) 76 { 77 UMOWorkManager wm = ((MuleDescriptor)descriptor).getThreadingProfile().createWorkManager( 78 "xfire-local-transport"); 79 try 80 { 81 wm.start(); 82 } 83 catch (UMOException e) 84 { 85 throw new MuleRuntimeException(new Message(Messages.FAILED_TO_START_X, 86 "local channel work manager", e)); 87 } 88 if(transportClass == null) 89 { 90 transport = new MuleLocalTransport(wm); 91 } 92 else 93 { 94 try { 95 Class transportClazz = Class.forName(transportClass); 96 try{ 97 Constructor constructor = transportClazz.getConstructor(new Class []{UMOWorkManager.class}); 98 transport = (Transport)constructor.newInstance(new Object []{wm}); 99 } 100 catch(NoSuchMethodException ne) 101 { 102 if (logger.isDebugEnabled()) 103 { 104 logger.debug(ne.getCause()); 105 } 106 } 107 if(transport == null) 108 { 109 Constructor constructor = transportClazz.getConstructor(null); 110 transport = (Transport)constructor.newInstance(null); 111 } 112 } 113 catch(Exception e) 114 { 115 throw new MuleRuntimeException(new Message(Messages.FAILED_LOAD_X, "xfire service transport", e)); 116 } 117 } 118 119 universalTransport = new MuleUniversalTransport(); 120 121 getTransportManager().register(transport); 122 getTransportManager().register(universalTransport); 123 } 124 125 public Object onCall(UMOEventContext eventContext) throws Exception 126 { 127 if(logger.isDebugEnabled()) 128 { 129 logger.debug(eventContext); 130 } 131 132 boolean wsdlRequested = false; 133 134 String request = eventContext.getMessage().getStringProperty(HttpConnector.HTTP_REQUEST_PROPERTY, 136 StringUtils.EMPTY); 137 if (request.toLowerCase().endsWith(org.mule.providers.soap.SoapConstants.WSDL_PROPERTY)) 138 { 139 wsdlRequested = true; 140 } 141 else { 143 Enumeration keys = eventContext.getEndpointURI().getParams().keys(); 144 while(keys.hasMoreElements()){ 145 if ((keys.nextElement()).toString().equalsIgnoreCase(SoapConstants.WSDL_PROPERTY)) { 146 wsdlRequested = true; 147 break; 148 } 149 } 150 } 151 152 if (wsdlRequested) 153 { 154 ByteArrayOutputStream out = new ByteArrayOutputStream(); 155 getXfire().generateWSDL(getServiceName(eventContext), out); 156 UMOMessage result = new MuleMessage(out.toString(eventContext.getEncoding())); 157 result.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/xml"); 158 return result; 159 } 160 else 161 { 162 MuleLocalChannel channel = (MuleLocalChannel)transport.createChannel(eventContext.getEndpointURI() 163 .getFullScheme()); 164 return channel.onCall(eventContext); 165 } 166 167 } 168 169 public void start() throws UMOException 170 { 171 } 173 174 public void stop() throws UMOException 175 { 176 } 178 179 public void initialise() throws InitialisationException 180 { 181 if (xfire == null) 182 { 183 throw new InitialisationException(new Message(Messages.X_IS_NULL, "xfire"), this); 184 } 185 } 186 187 public void dispose() 188 { 189 } 191 192 protected TransportManager getTransportManager() 193 { 194 return getXfire().getTransportManager(); 195 } 196 197 protected void generateService(OutStreamMessageAdapter response, String serviceName) 198 throws IOException , XMLStreamException 199 { 200 response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html"); 201 Service endpoint = getServiceRegistry().getService(serviceName); 202 HtmlServiceWriter writer = new HtmlServiceWriter(); 203 writer.write(response.getStream(), endpoint); 204 } 205 206 209 protected void generateServices(OutStreamMessageAdapter response) throws IOException , XMLStreamException 210 { 211 response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html"); 212 213 HtmlServiceWriter writer = new HtmlServiceWriter(); 214 writer.write(response.getStream(), getServiceRegistry().getServices()); 215 } 216 217 227 228 protected InputStream getMessageStream(UMOEventContext context) throws UMOException 229 { 230 InputStream is; 231 UMOMessage eventMsg = context.getMessage(); 232 Object eventMsgPayload = eventMsg.getPayload(); 233 234 if (eventMsgPayload instanceof InputStream ) 235 { 236 is = (InputStream )eventMsgPayload; 237 } 238 else if (eventMsg.getAdapter() instanceof UMOStreamMessageAdapter) 239 { 240 StreamMessageAdapter sma = (StreamMessageAdapter)eventMsg.getAdapter(); 241 is = sma.getInputStream(); 242 243 } 244 else 245 { 246 is = new ByteArrayInputStream (context.getTransformedMessageAsBytes()); 247 } 248 return is; 249 } 250 251 257 protected String getServiceName(UMOEventContext context) 258 { 259 String pathInfo = context.getEndpointURI().getPath(); 260 261 if (StringUtils.isEmpty(pathInfo)) 262 { 263 return context.getEndpointURI().getHost(); 264 } 265 266 String serviceName; 267 268 int i = pathInfo.lastIndexOf("/"); 269 270 if (i > -1) 271 { 272 serviceName = pathInfo.substring(i + 1); 273 } 274 else 275 { 276 serviceName = pathInfo; 277 } 278 279 return serviceName; 280 } 281 282 protected Service getService(String name) 283 { 284 return getXfire().getServiceRegistry().getService(name); 285 } 286 287 public XFire getXfire() 288 { 289 return xfire; 290 } 291 292 public void setXfire(XFire xfire) 293 { 294 this.xfire = xfire; 295 } 296 297 public void setTransport(Transport transport) 298 { 299 this.transport = transport; 300 } 301 302 public void setTransportClass(String clazz) 303 { 304 transportClass = clazz; 305 } 306 307 public ServiceRegistry getServiceRegistry() 308 { 309 return xfire.getServiceRegistry(); 310 } 311 } 312 | Popular Tags |