001: /*
002: * Copyright 2005-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.bpmscript.jbi;
017:
018: import javax.jbi.messaging.ExchangeStatus;
019: import javax.jbi.messaging.MessageExchange;
020: import javax.jbi.messaging.MessagingException;
021: import javax.jbi.messaging.NormalizedMessage;
022: import javax.xml.soap.MimeHeaders;
023: import javax.xml.soap.SOAPConnection;
024: import javax.xml.soap.SOAPConnectionFactory;
025: import javax.xml.soap.SOAPException;
026: import javax.xml.soap.SOAPMessage;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.servicemix.MessageExchangeListener;
031: import org.apache.servicemix.components.util.ComponentSupport;
032:
033: /**
034: * Converts an inbound JBI message into a <a href="http://java.sun.com/xml/saaj/">SAAJ</a> (Soap With Attachments for Java)
035: * request-response and outputs the response back into JBI Bindings. This provides
036: * a message centric way of invoking SOAP services inside providers such as <a href="http://ws.apache.org/axis/">Apache Axis</a>
037: *
038: * @version $Revision: 1.3 $
039: */
040: public class DynamicSaajBinding extends ComponentSupport implements
041: MessageExchangeListener {
042:
043: private static final transient Log log = LogFactory
044: .getLog(DynamicSaajBinding.class);
045:
046: private SaajMarshaler marshaler = new SaajMarshaler();
047: private SOAPConnectionFactory connectionFactory;
048: private Object soapEndpoint;
049: private String soapAction;
050:
051: public SOAPConnectionFactory getConnectionFactory()
052: throws SOAPException {
053: if (connectionFactory == null) {
054: connectionFactory = createConnectionFactory();
055: }
056: return connectionFactory;
057: }
058:
059: public void setConnectionFactory(
060: SOAPConnectionFactory connectionFactory) {
061: this .connectionFactory = connectionFactory;
062: }
063:
064: public Object getSoapEndpoint() {
065: return soapEndpoint;
066: }
067:
068: public void setSoapEndpoint(Object soapEndpoint) {
069: this .soapEndpoint = soapEndpoint;
070: }
071:
072: public SaajMarshaler getMarshaller() {
073: return marshaler;
074: }
075:
076: public void setMarshaller(SaajMarshaler marshaler) {
077: this .marshaler = marshaler;
078: }
079:
080: public void onMessageExchange(MessageExchange exchange)
081: throws MessagingException {
082: if (exchange.getStatus() == ExchangeStatus.DONE) {
083: return;
084: } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
085: done(exchange);
086: return;
087: }
088: SOAPConnection connection = null;
089: try {
090: connection = getConnectionFactory().createConnection();
091:
092: SOAPMessage inMessage = marshaler
093: .createSOAPMessage(exchange.getMessage("in"));
094: if (soapAction != null) {
095: MimeHeaders mh = inMessage.getMimeHeaders();
096: if (mh.getHeader("SOAPAction") == null) {
097: mh
098: .addHeader("SOAPAction", "\"" + soapAction
099: + "\"");
100: inMessage.saveChanges();
101: }
102: }
103:
104: Object dynamicEndpoint = null;
105: NormalizedMessage in = getInMessage(exchange);
106:
107: if (in != null) {
108: dynamicEndpoint = in.getProperty("SOAPEndpoint");
109: }
110:
111: if (dynamicEndpoint == null) {
112: dynamicEndpoint = soapEndpoint;
113: }
114:
115: SOAPMessage response = connection.call(inMessage,
116: dynamicEndpoint);
117:
118: NormalizedMessage outMessage = exchange.createMessage();
119: marshaler.toNMS(outMessage, response);
120:
121: answer(exchange, outMessage);
122: } catch (Exception e) {
123: fail(exchange, e);
124: } finally {
125: if (connection != null) {
126: try {
127: connection.close();
128: } catch (SOAPException e) {
129: log.warn("Failed to close connection: " + e, e);
130: }
131: }
132: }
133: }
134:
135: protected SOAPConnectionFactory createConnectionFactory()
136: throws SOAPException {
137: return SOAPConnectionFactory.newInstance();
138: }
139:
140: protected SOAPConnection createConnection() throws SOAPException {
141: return getConnectionFactory().createConnection();
142: }
143:
144: /**
145: * @return Returns the soapAction.
146: */
147: public String getSoapAction() {
148: return soapAction;
149: }
150:
151: /**
152: * @param soapAction
153: * The soapAction to set.
154: */
155: public void setSoapAction(String soapAction) {
156: this.soapAction = soapAction;
157: }
158: }
|