1 22 package org.objectweb.petals.binding.axis2.listener.jbi; 23 24 import java.io.StringReader ; 25 import java.net.URI ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.jbi.messaging.MessagingException; 30 import javax.jbi.messaging.NormalizedMessage; 31 import javax.xml.namespace.QName ; 32 import javax.xml.stream.FactoryConfigurationError; 33 import javax.xml.stream.XMLInputFactory; 34 import javax.xml.stream.XMLStreamException; 35 import javax.xml.stream.XMLStreamReader; 36 import javax.xml.transform.Source ; 37 38 import org.apache.axiom.om.OMElement; 39 import org.apache.axiom.om.impl.builder.StAXOMBuilder; 40 import org.apache.axiom.soap.SOAP11Constants; 41 import org.apache.axiom.soap.SOAP12Constants; 42 import org.apache.axis2.AxisFault; 43 import org.apache.axis2.Constants; 44 import org.apache.axis2.addressing.EndpointReference; 45 import org.apache.axis2.client.Options; 46 import org.apache.axis2.client.ServiceClient; 47 import org.apache.axis2.description.AxisOperation; 48 import org.apache.axis2.description.AxisService; 49 import org.apache.axis2.description.OutInAxisOperation; 50 import org.apache.axis2.description.OutOnlyAxisOperation; 51 import org.apache.axis2.description.RobustOutOnlyAxisOperation; 52 import org.apache.axis2.transport.http.HTTPConstants; 53 import org.apache.axis2.transport.http.HttpTransportProperties; 54 import org.objectweb.petals.component.common.HandlingException; 55 import org.objectweb.petals.component.common.MEPConstants; 56 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 57 import org.objectweb.petals.component.common.su.SimpleServiceUnitManager; 58 import org.objectweb.petals.component.common.util.MessageExchangeWrapper; 59 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil; 60 import org.objectweb.petals.component.common.util.SourceHelper; 61 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions; 62 63 71 public class JBIListener implements 72 org.objectweb.petals.component.common.bc.JBIListener { 73 74 77 protected Logger log; 78 79 82 protected SimpleServiceUnitManager bindingSUM; 83 84 public static final String SOAP_VERSION_11 = "11"; 85 86 public static final String SOAP_VERSION_12 = "12"; 87 88 94 public JBIListener(SimpleServiceUnitManager bindingSUM, Logger log) { 95 super(); 96 this.log = log; 97 this.bindingSUM = bindingSUM; 98 } 99 100 110 public boolean onJBIMessage(String address, 111 MessageExchangeWrapper exchange, Extensions extensions) 112 throws HandlingException { 113 114 log.log(Level.FINE, 115 "JBI message received on SOAP JBI listener for address : " 116 + address); 117 118 boolean responseSet = false; 119 120 NormalizedMessage in = exchange.getInMessage(); 122 if (in == null) { 123 throw new HandlingException( 124 "Message exchange must handle an In normalized message"); 125 } 126 127 NormalizedMessage out = exchange.getOutMessage(); 129 130 QName operation = exchange.getOperation(); 132 133 try { 134 Options options = createOptions(address, operation, extensions); 136 137 ServiceClient serviceClient = createServiceClient(exchange 138 .getExchangePattern(), operation); 139 serviceClient.setOptions(options); 140 141 OMElement inBodyElement = createSOAPMessageContent(in.getContent()); 143 144 addJBIAttachmentsToSOAP(in, serviceClient); 146 147 serviceClient.engageModule(new QName (Constants.MODULE_ADDRESSING)); 149 if (exchange.isInOnlyPattern()) { 151 serviceClient.fireAndForget(operation, inBodyElement); 152 responseSet = false; 153 154 } else if (exchange.isRobustInOnlyPattern()) { 155 serviceClient.sendRobust(operation, inBodyElement); 159 responseSet = false; 160 161 } else if (exchange.isInOptionalOutPattern() 162 || exchange.isInOutPattern()) { 163 OMElement outBodyElement = serviceClient.sendReceive(operation, 165 inBodyElement); 166 167 if (outBodyElement != null 169 && outBodyElement.toString().length() > 0) { 170 Source responseSource = SourceHelper 171 .createSource(outBodyElement.toString()); 172 if (out == null) { 173 out = exchange.createMessage(); 174 exchange.setOutMessage(out); 175 } 176 out.setContent(responseSource); 177 178 addSOAPAttachmentsToJBI(out, outBodyElement); 179 responseSet = true; 180 } 181 } else 182 throw new MessagingException( 183 "JBIListener - MEP not recognized : " 184 + exchange.getPattern().toString()); 185 } catch (Throwable e) { 186 log.log(Level.WARNING, e.getMessage(), e); 187 throw new HandlingException(e); 188 } 189 return responseSet; 190 } 191 192 202 private ServiceClient createServiceClient(URI mep, QName operation) 203 throws HandlingException { 204 205 AxisService service = new AxisService("anonService" + this.hashCode() 206 + System.currentTimeMillis()); 207 AxisOperation axisOperation = null; 208 if (MEPConstants.IN_ONLY_PATTERN.value().equals(mep)) { 209 axisOperation = new OutOnlyAxisOperation(operation); 210 } else if (MEPConstants.ROBUST_IN_ONLY_PATTERN.value().equals(mep)) { 211 axisOperation = new RobustOutOnlyAxisOperation(operation); 212 } else if (MEPConstants.IN_OPTIONAL_OUT_PATTERN.value().equals(mep) 213 || MEPConstants.IN_OUT_PATTERN.value().equals(mep)) { 214 axisOperation = new OutInAxisOperation(operation); 215 } 216 service.addOperation(axisOperation); 217 218 ServiceClient client = null; 219 try { 220 client = new ServiceClient(null, service); 221 } catch (AxisFault e) { 222 throw new HandlingException("Can't create ServiceClient", e); 223 } 224 return client; 225 } 226 227 236 protected OMElement createSOAPMessageContent(Source body) 237 throws XMLStreamException, FactoryConfigurationError, 238 PEtALSComponentSDKException { 239 String content = SourceHelper.createString(body); 240 XMLStreamReader parser = XMLInputFactory.newInstance() 241 .createXMLStreamReader(new StringReader (content)); 242 StAXOMBuilder builder = new StAXOMBuilder(parser); 243 return builder.getDocumentElement(); 244 } 245 246 254 protected Options createOptions(String address, QName operation, 255 Extensions extensions) { 256 257 String soapEnvelopeNamespaceURI = retrieveSoapEnvelopeNamespaceURI(extensions); 260 261 Options options = new Options(); 262 263 options.setTo(new EndpointReference(address)); 265 266 if (operation != null) 268 options.setAction(operation.toString()); 269 270 options.setSoapVersionURI(soapEnvelopeNamespaceURI); 271 options.setTransportInProtocol(Constants.TRANSPORT_HTTP); 272 273 HttpTransportProperties.ProxyProperties proxyProperties = retrieveProxySettings(extensions); 275 if (proxyProperties != null) { 276 options.setProperty(HTTPConstants.PROXY, proxyProperties); 277 } 278 279 283 286 return options; 291 } 292 293 300 protected void addJBIAttachmentsToSOAP(NormalizedMessage message, 301 ServiceClient serviceClient) throws AxisFault { 302 } 311 312 318 protected void addSOAPAttachmentsToJBI(NormalizedMessage message, OMElement soapResponse) { 319 320 } 321 322 335 protected String retrieveSoapEnvelopeNamespaceURI(Extensions extensions) { 336 String soapEnvelopeNamespaceURI = null; 337 String soapVersion = PetalsExtensionsUtil 338 .extractValueFromKeyValueExtension(extensions, "soap-version"); 339 340 if (SOAP_VERSION_12.equals(soapVersion)) { 341 soapEnvelopeNamespaceURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI; 342 } else { 343 soapEnvelopeNamespaceURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI; 344 } 345 return soapEnvelopeNamespaceURI; 346 } 347 348 362 protected HttpTransportProperties.ProxyProperties retrieveProxySettings( 363 Extensions extensions) { 364 365 HttpTransportProperties.ProxyProperties proxyProperties = null; 366 367 if (PetalsExtensionsUtil.hasKeyValueExtension(extensions, "proxy-host")) { 368 371 proxyProperties = new HttpTransportProperties.ProxyProperties(); 372 373 String domain = PetalsExtensionsUtil 374 .extractValueFromKeyValueExtension(extensions, "proxy-domain"); 375 String password = PetalsExtensionsUtil 376 .extractValueFromKeyValueExtension(extensions, "proxy-password"); 377 String proxyHost = PetalsExtensionsUtil 378 .extractValueFromKeyValueExtension(extensions, "proxy-host"); 379 380 int proxyPort = -1; 381 String tmp = PetalsExtensionsUtil 382 .extractValueFromKeyValueExtension(extensions, "proxy-port"); 383 if (tmp != null) { 384 try { 385 Integer.parseInt(tmp); 386 } catch (NumberFormatException e) { 387 } 388 } 389 390 String proxyUserName = PetalsExtensionsUtil 391 .extractValueFromKeyValueExtension(extensions, "proxy-user"); 392 393 proxyProperties.setDomain(domain); 394 proxyProperties.setPassWord(password); 395 proxyProperties.setProxyName(proxyHost); 396 proxyProperties.setProxyPort(proxyPort); 397 proxyProperties.setUserName(proxyUserName); 398 } 399 400 return proxyProperties; 401 } 402 403 410 protected void enableSSLSupport(Extensions extensions) { 411 String trustStore = PetalsExtensionsUtil 412 .extractValueFromKeyValueExtension(extensions, "ssl-trustStore"); 413 String trustStorePassword = PetalsExtensionsUtil 414 .extractValueFromKeyValueExtension(extensions, 415 "ssl-trustStorePassword"); 416 String keyStore = PetalsExtensionsUtil 417 .extractValueFromKeyValueExtension(extensions, "ssl-keyStore"); 418 String keyStorePassword = PetalsExtensionsUtil 419 .extractValueFromKeyValueExtension(extensions, 420 "ssl-keyStorePassword"); 421 422 if (trustStore != null && trustStorePassword != null 423 && keyStore != null && keyStorePassword != null) { 424 } 426 } 427 428 } 429 | Popular Tags |