1 22 package org.objectweb.petals.binding.axis2.listener.soap; 23 24 import java.io.ByteArrayInputStream ; 25 import java.util.Map ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.jbi.component.ComponentContext; 30 import javax.jbi.messaging.DeliveryChannel; 31 import javax.jbi.messaging.ExchangeStatus; 32 import javax.jbi.messaging.InOptionalOut; 33 import javax.jbi.messaging.InOut; 34 import javax.jbi.messaging.MessageExchange; 35 import javax.jbi.messaging.MessageExchangeFactory; 36 import javax.jbi.messaging.MessagingException; 37 import javax.jbi.messaging.NormalizedMessage; 38 import javax.jbi.servicedesc.ServiceEndpoint; 39 import javax.xml.namespace.QName ; 40 import javax.xml.stream.XMLInputFactory; 41 import javax.xml.stream.XMLStreamReader; 42 import javax.xml.transform.Source ; 43 import javax.xml.transform.TransformerException ; 44 45 import org.apache.axiom.om.OMElement; 46 import org.apache.axiom.om.impl.builder.StAXOMBuilder; 47 import org.apache.axiom.soap.SOAPBody; 48 import org.apache.axiom.soap.SOAPEnvelope; 49 import org.apache.axiom.soap.SOAPFactory; 50 import org.apache.axis2.AxisFault; 51 import org.apache.axis2.client.Options; 52 import org.apache.axis2.context.MessageContext; 53 import org.apache.axis2.description.AxisService; 54 import org.apache.axis2.engine.AxisEngine; 55 import org.apache.axis2.receivers.AbstractMessageReceiver; 56 import org.apache.axis2.util.Utils; 57 import org.objectweb.petals.binding.axis2.SOAPHelper; 58 import org.objectweb.petals.component.common.su.SimpleServiceUnitManager; 59 import org.objectweb.petals.tools.jbicommon.util.StringHelper; 60 import org.objectweb.petals.tools.jbicommon.util.XMLUtil; 61 import org.w3c.dom.Document ; 62 63 72 public class PetalsReceiver extends AbstractMessageReceiver { 73 74 protected static final String ERROR_SERVICE_NOT_FOUND = "Requested service and/or operation not found/available"; 75 76 protected static final String ERROR_GET_WSDL = "Error while retrieving the service WSDL"; 77 78 protected static final String ERROR_WRONG_MESSAGE_STATUS = "JBI message has wrong status"; 79 80 protected static final String ERROR_SERVICE_INVOKATION = "An error occured in service invokation"; 81 82 protected static final String ERROR_CREATE_RESPONSE_FROM_DOM = "Error creating a string from a DOM service description"; 83 84 protected static final String ERROR_CREATE_STRING_FROM_NM = "Error creating a string from normalized message"; 85 86 protected Axis2ReceiverUtil axis2ReceiverUtil; 87 88 protected Logger log; 89 90 protected ComponentContext context; 91 92 protected DeliveryChannel channel; 93 94 protected MessageExchangeFactory exchangeFactory; 95 96 protected SimpleServiceUnitManager bindingSUM; 97 98 106 public PetalsReceiver(ComponentContext context, DeliveryChannel channel, 107 SimpleServiceUnitManager bindingSUM, Logger log) { 108 super(); 109 this.axis2ReceiverUtil = new Axis2ReceiverUtil(); 110 this.channel = channel; 111 this.context = context; 112 this.log = log; 113 this.bindingSUM = bindingSUM; 114 this.exchangeFactory = channel.createExchangeFactory(); 115 } 116 117 122 public final void receive(final MessageContext msgContext) throws AxisFault { 123 log.log(Level.FINE, "Receive SOAP message"); 124 125 MessageContext outMsgContext = Utils 126 .createOutMessageContext(msgContext); 127 outMsgContext.getOperationContext().addMessageContext(outMsgContext); 128 129 ServiceEndpoint jbiEndpoint = findEndpoint(msgContext); 130 process(jbiEndpoint, msgContext, outMsgContext); 131 132 if (outMsgContext.getEnvelope() != null) { 134 AxisEngine engine = new AxisEngine(msgContext.getOperationContext() 135 .getServiceContext().getConfigurationContext()); 136 137 engine.send(outMsgContext); 138 } 139 } 140 141 150 protected SOAPEnvelope createResponseEnvelope(MessageContext msgContext) 151 throws AxisFault { 152 SOAPFactory factory = getSOAPFactory(msgContext); 153 return factory.createSOAPEnvelope(); 154 } 155 156 164 protected ServiceEndpoint findEndpoint(MessageContext msgContext) { 165 AxisService axisService = msgContext.getAxisService(); 166 String endpointName = axisService.getEndpoint(); 167 QName serviceName = new QName (axisService.getTargetNamespace(), 168 endpointName); 169 170 return context.getEndpoint(serviceName, endpointName); 171 } 172 173 181 public String createStringFromDOMDocument(Document document) 182 throws TransformerException { 183 return XMLUtil.createStringFromDOMNode(document); 184 } 185 186 200 public void process(ServiceEndpoint endpoint, MessageContext inContext, 201 MessageContext outContext) throws AxisFault { 202 log.log(Level.FINE, "Process SOAP request"); 203 204 QName operationName = findSOAPOperation(inContext); 206 if (operationName == null) { 207 throw new AxisFault("Operation not found in message context"); 208 } 209 210 MessageExchange exchange = null; 211 try { 212 exchange = createMessageExchange(inContext, endpoint, operationName); 213 214 } catch (AxisFault e) { 215 String errorMsg = "Error while transforming SOAP request to JBI MessageExchange"; 216 log.log(Level.SEVERE, errorMsg, e); 217 throw new AxisFault(errorMsg, e); 218 } 219 220 try { 224 channel.sendSync(exchange); 225 } catch (MessagingException e) { 226 String errorMsg = "Error while sending message through JBI NMR."; 227 log.log(Level.SEVERE, errorMsg, e); 228 throw new AxisFault(errorMsg, e); 229 } 230 231 processJbiNmrResponse(exchange, outContext, getSOAPFactory(inContext)); 233 } 234 235 242 protected QName findSOAPOperation(MessageContext inContext) { 243 String operation = inContext.getWSAAction(); 244 QName operationName = null; 245 if (!StringHelper.isNullOrEmpty(operation)) { 246 operationName = QName.valueOf(operation); 247 } 248 return operationName; 249 } 250 251 278 protected void processJbiNmrResponse(MessageExchange exchange, 279 MessageContext outMessage, SOAPFactory factory) throws AxisFault { 280 if (exchange.getStatus().equals(ExchangeStatus.DONE)) { 281 283 } else if (exchange.getStatus().equals(ExchangeStatus.ERROR)) { 284 throw new AxisFault(exchange.getError()); 286 287 } else if (exchange.getStatus().equals(ExchangeStatus.ACTIVE)) { 288 if (exchange.getFault() != null) { 289 try { 291 outMessage.setEnvelope(createResponseFromNormalizedMessage( 292 factory, exchange.getFault())); 293 } catch (AxisFault e) { 294 String errorMsg = "Error while creating SOAP Fault response."; 295 log.log(Level.SEVERE, errorMsg, e); 296 throw new AxisFault(errorMsg, e); 297 } 298 299 } else { 300 try { 302 NormalizedMessage nm = null; 303 if (exchange instanceof InOptionalOut) { 304 InOptionalOut mex = (InOptionalOut) exchange; 305 nm = mex.getOutMessage(); 306 } else if (exchange instanceof InOut) { 307 InOut mex = (InOut) exchange; 308 nm = mex.getOutMessage(); 309 } 310 311 outMessage.setEnvelope(createResponseFromNormalizedMessage( 312 factory, nm)); 313 314 SOAPHelper.copyAttachmentsFromJBIToSOAP(nm, outMessage); 315 } catch (AxisFault e) { 316 String errorMsg = "Error while creating SOAP Fault response."; 317 log.log(Level.SEVERE, errorMsg, e); 318 throw new AxisFault(errorMsg, e); 319 } 320 } 321 try { 323 exchange.setStatus(ExchangeStatus.DONE); 324 channel.send(exchange); 325 } catch (MessagingException e) { 326 String errorMsg = "Error while closing JBI MessageExchange."; 327 log.log(Level.SEVERE, errorMsg, e); 328 throw new AxisFault(errorMsg, e); 329 } 330 331 } else { 332 throw new AxisFault(ERROR_WRONG_MESSAGE_STATUS); 333 } 334 335 } 336 337 347 protected SOAPEnvelope createResponseFromNormalizedMessage( 348 SOAPFactory factory, NormalizedMessage nm) throws AxisFault { 349 350 SOAPEnvelope responseEnv = createSoapEnvelope(factory); 351 352 356 String response = null; 357 if (nm != null) { 358 try { 359 response = axis2ReceiverUtil.createStringContent(nm); 360 } catch (Exception e) { 361 log.log(Level.SEVERE, ERROR_CREATE_STRING_FROM_NM); 362 throw new AxisFault(ERROR_CREATE_STRING_FROM_NM, e); 363 } 364 } else { 365 response = "<Response>Done</Response>"; 366 } 367 createSoapBody(factory, responseEnv, response); 368 return responseEnv; 369 } 370 371 382 protected OMElement createSoapBody(SOAPFactory factory, 383 SOAPEnvelope envelope, String response) throws AxisFault { 384 OMElement bodyContent = null; 385 ByteArrayInputStream inputStream = new ByteArrayInputStream (response 386 .getBytes()); 387 try { 388 XMLStreamReader parser = XMLInputFactory.newInstance() 389 .createXMLStreamReader(inputStream); 390 StAXOMBuilder builder = new StAXOMBuilder(factory, parser); 391 bodyContent = (OMElement) builder.getDocumentElement(); 392 } catch (Exception e) { 393 throw new AxisFault( 394 "Error parsing the response from JBI service to a SOAPBody", e); 395 } 396 SOAPBody respBody = factory.createSOAPBody(envelope); 397 respBody.addChild(bodyContent); 398 return respBody; 399 } 400 401 410 protected SOAPEnvelope createSoapEnvelope(SOAPFactory factory) { 411 return factory.createSOAPEnvelope(); 412 } 413 414 423 protected MessageExchange createMessageExchange(MessageContext inContext, 424 ServiceEndpoint endpoint, QName operationName) throws AxisFault { 425 InOptionalOut exchange = null; 426 NormalizedMessage nm = null; 427 428 try { 429 430 Source source = axis2ReceiverUtil.createSourceContent(inContext); 433 exchange = exchangeFactory.createInOptionalOutExchange(); 434 nm = exchange.createMessage(); 435 exchange.setInMessage(nm); 436 nm.setContent(source); 437 exchange.setEndpoint(endpoint); 438 exchange.setOperation(operationName); 439 440 SOAPHelper.copyAttachmentsFromSOAPToJBI(inContext, nm); 442 443 addOptions(inContext, exchange); 446 447 } catch (Exception e) { 448 throw AxisFault.makeFault(e); 449 } 450 451 return exchange; 452 } 453 454 461 protected void addOptions(MessageContext context, MessageExchange exchange) { 462 Options options = context.getOptions(); 463 Map properties = options.getProperties(); 464 for (Object key : properties.keySet()) { 465 exchange.setProperty(key.toString(), properties.get(key)); 466 } 467 } 468 } 469 | Popular Tags |