KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > axis2 > listener > jbi > JBIListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JBIListener.java 154 27 sept. 06 alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.axis2.listener.jbi;
23
24 import java.io.StringReader JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.messaging.MessagingException;
30 import javax.jbi.messaging.NormalizedMessage;
31 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
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 /**
64  * Listener for incoming JBI messages. A SOAP message is created from the JBI
65  * one.
66  *
67  * @author alouis - EBM Websourcing
68  * @author Christophe Hamerling - EBM Websourcing
69  *
70  */

71 public class JBIListener implements
72     org.objectweb.petals.component.common.bc.JBIListener {
73
74     /**
75      * The logger
76      */

77     protected Logger JavaDoc log;
78
79     /**
80      * The SU
81      */

82     protected SimpleServiceUnitManager bindingSUM;
83
84     public static final String JavaDoc SOAP_VERSION_11 = "11";
85
86     public static final String JavaDoc SOAP_VERSION_12 = "12";
87
88     /**
89      * Creates a new instance of JBIListener
90      *
91      * @param bindingSUM
92      * @param log
93      */

94     public JBIListener(SimpleServiceUnitManager bindingSUM, Logger JavaDoc log) {
95         super();
96         this.log = log;
97         this.bindingSUM = bindingSUM;
98     }
99
100     /**
101      * Called by the JBI channel listener when a jbi message is accepted. A
102      * remote webservice call is performed.
103      *
104      * @param address
105      * @param exchange
106      * @param extensions
107      * @return true if a response has been provided by the external webservice
108      * @throws HandlingException
109      */

110     public boolean onJBIMessage(String JavaDoc 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         // Get the incoming Normalized message
121
NormalizedMessage in = exchange.getInMessage();
122         if (in == null) {
123             throw new HandlingException(
124                 "Message exchange must handle an In normalized message");
125         }
126
127         // Get the outgoing Normalized message
128
NormalizedMessage out = exchange.getOutMessage();
129
130         // Get operation
131
QName JavaDoc operation = exchange.getOperation();
132
133         try {
134             // create Options for the stub
135
Options options = createOptions(address, operation, extensions);
136
137             ServiceClient serviceClient = createServiceClient(exchange
138                 .getExchangePattern(), operation);
139             serviceClient.setOptions(options);
140
141             // create the in body
142
OMElement inBodyElement = createSOAPMessageContent(in.getContent());
143
144             // add JBI attachments to SOAP
145
addJBIAttachmentsToSOAP(in, serviceClient);
146
147             // set Addressing mode
148
serviceClient.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
149             // send as InOnly message
150
if (exchange.isInOnlyPattern()) {
151                 serviceClient.fireAndForget(operation, inBodyElement);
152                 responseSet = false;
153
154             } else if (exchange.isRobustInOnlyPattern()) {
155                 // send as RobustInOnly message
156
// If the robust in only exchange throws a Fault
157
// It will be catch by the component-common framework
158
serviceClient.sendRobust(operation, inBodyElement);
159                 responseSet = false;
160
161             } else if (exchange.isInOptionalOutPattern()
162                 || exchange.isInOutPattern()) {
163                 // send as In(optional)Out message
164
OMElement outBodyElement = serviceClient.sendReceive(operation,
165                     inBodyElement);
166
167                 // if msg exchange required a response, set it
168
if (outBodyElement != null
169                     && outBodyElement.toString().length() > 0) {
170                     Source JavaDoc 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 JavaDoc e) {
186             log.log(Level.WARNING, e.getMessage(), e);
187             throw new HandlingException(e);
188         }
189         return responseSet;
190     }
191
192     /**
193      * Create a ServiceClient with an AxisService set with the good operation
194      *
195      * @param mep
196      * the message exchange pattern used. Non null
197      * @param operation
198      * the target operation QName. Non null
199      * @return a ServiceClient. Not null.
200      * @throws HandlingException
201      */

202     private ServiceClient createServiceClient(URI JavaDoc mep, QName JavaDoc 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     /**
228      * Create an Axis OMElement representing the given body TODO create from a
229      * Source element, not a string
230      *
231      * @param body
232      * @return
233      * @throws FactoryConfigurationError
234      * @throws XMLStreamException
235      */

236     protected OMElement createSOAPMessageContent(Source JavaDoc body)
237         throws XMLStreamException, FactoryConfigurationError,
238         PEtALSComponentSDKException {
239         String JavaDoc content = SourceHelper.createString(body);
240         XMLStreamReader parser = XMLInputFactory.newInstance()
241             .createXMLStreamReader(new StringReader JavaDoc(content));
242         StAXOMBuilder builder = new StAXOMBuilder(parser);
243         return builder.getDocumentElement();
244     }
245
246     /**
247      * Create the SOAP options
248      *
249      * @param address
250      * @param operation
251      * @param extensions
252      * @return
253      */

254     protected Options createOptions(String JavaDoc address, QName JavaDoc operation,
255         Extensions extensions) {
256
257         // Get the soapEnvelopeNamespaceURI version to use - optional, default
258
// is 1.1 version
259
String JavaDoc soapEnvelopeNamespaceURI = retrieveSoapEnvelopeNamespaceURI(extensions);
260
261         Options options = new Options();
262
263         // set destination address
264
options.setTo(new EndpointReference(address));
265
266         // set operation if provided in the JBI message
267
if (operation != null)
268             options.setAction(operation.toString());
269
270         options.setSoapVersionURI(soapEnvelopeNamespaceURI);
271         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
272
273         // get proxy settings if they are defined in the extensions
274
HttpTransportProperties.ProxyProperties proxyProperties = retrieveProxySettings(extensions);
275         if (proxyProperties != null) {
276             options.setProperty(HTTPConstants.PROXY, proxyProperties);
277         }
278
279         // TODO enable SOAP attachments
280
// options.setProperty(Constants.Configuration.ENABLE_SWA,
281
// Constants.VALUE_TRUE);
282

283         // TODO set From address
284
// options.setFrom(new EndpointReference(fromAddress));
285

286         // FIXME, seems to be unusefull
287
// options.setProperty(
288
// org.apache.axis2.transport.http.HTTPConstants.CHUNKED,
289
// org.apache.axis2.Constants.VALUE_FALSE);
290
return options;
291     }
292
293     /**
294      * TODO Set the message attachments as SOAP attachments;
295      *
296      * @param exchange
297      * @param serviceClient
298      * @throws AxisFault
299      */

300     protected void addJBIAttachmentsToSOAP(NormalizedMessage message,
301         ServiceClient serviceClient) throws AxisFault {
302         // MessageContext mc = new MessageContext();
303
// mc.setEnvelope(null);
304
// NormalizedMessage message = exchange.getInMessage();
305
// Set attachmentNames = message.getAttachmentNames();
306
// for (Object name : attachmentNames) {
307
// String id = (String) name;
308
// mc.addAttachment(id, message.getAttachment(id));
309
// }
310
}
311     
312     /**
313      * TODO
314      *
315      * @param message
316      * @param soapResponse
317      */

318     protected void addSOAPAttachmentsToJBI(NormalizedMessage message, OMElement soapResponse) {
319         
320     }
321
322     /**
323      * Retrieve, in the jbi.xml descriptor, the SOAP version to use for
324      * SoapEnvelopeNamespaceURI Search a <em>soap-version</em> tag. <br>
325      * Legal values are <b>11</b> and <b>12</b>. 11 is used if no tag is
326      * found.
327      *
328      * @param extensions
329      * must be non null. Contains a <em>soap-version</em> optional
330      * tag.
331      * @return the Axis2 soapEnvelopeNamespaceURI constant.
332      * @see org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI
333      * @see org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI
334      */

335     protected String JavaDoc retrieveSoapEnvelopeNamespaceURI(Extensions extensions) {
336         String JavaDoc soapEnvelopeNamespaceURI = null;
337         String JavaDoc 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     /**
349      * Retrieve the proxy settings from the extensions. The proxy-host value is
350      * required. The other ones will be set to default values by Axis if they
351      * have not been setted by the SU.
352      *
353      *
354      * @param extensions
355      * must be not null. Contains <em>proxy-*</em> optional tags.
356      *
357      * @return the proxy settings if they are present in the extensions, null
358      * otherwise.
359      * @see org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties
360      *
361      */

362     protected HttpTransportProperties.ProxyProperties retrieveProxySettings(
363         Extensions extensions) {
364
365         HttpTransportProperties.ProxyProperties proxyProperties = null;
366
367         if (PetalsExtensionsUtil.hasKeyValueExtension(extensions, "proxy-host")) {
368             // proxy host is required, if it is not set the proxy mode is not
369
// activated
370

371             proxyProperties = new HttpTransportProperties.ProxyProperties();
372
373             String JavaDoc domain = PetalsExtensionsUtil
374                 .extractValueFromKeyValueExtension(extensions, "proxy-domain");
375             String JavaDoc password = PetalsExtensionsUtil
376                 .extractValueFromKeyValueExtension(extensions, "proxy-password");
377             String JavaDoc proxyHost = PetalsExtensionsUtil
378                 .extractValueFromKeyValueExtension(extensions, "proxy-host");
379
380             int proxyPort = -1;
381             String JavaDoc tmp = PetalsExtensionsUtil
382                 .extractValueFromKeyValueExtension(extensions, "proxy-port");
383             if (tmp != null) {
384                 try {
385                     Integer.parseInt(tmp);
386                 } catch (NumberFormatException JavaDoc e) {
387                 }
388             }
389
390             String JavaDoc 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     /**
404      * TODO Enable the client to contact a service over an SSL connection. The
405      * required values are in the PEtALS extensions if the SU has been
406      * parametrized to enable SSL.
407      *
408      * @param extensions
409      */

410     protected void enableSSLSupport(Extensions extensions) {
411         String JavaDoc trustStore = PetalsExtensionsUtil
412             .extractValueFromKeyValueExtension(extensions, "ssl-trustStore");
413         String JavaDoc trustStorePassword = PetalsExtensionsUtil
414             .extractValueFromKeyValueExtension(extensions,
415                 "ssl-trustStorePassword");
416         String JavaDoc keyStore = PetalsExtensionsUtil
417             .extractValueFromKeyValueExtension(extensions, "ssl-keyStore");
418         String JavaDoc keyStorePassword = PetalsExtensionsUtil
419             .extractValueFromKeyValueExtension(extensions,
420                 "ssl-keyStorePassword");
421
422         if (trustStore != null && trustStorePassword != null
423             && keyStore != null && keyStorePassword != null) {
424             // TODO
425
}
426     }
427
428 }
429
Popular Tags