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