KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > saaj > SaajBinding


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.saaj;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.messaging.NormalizedMessage;
23 import javax.xml.soap.MimeHeaders JavaDoc;
24 import javax.xml.soap.SOAPConnection JavaDoc;
25 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
26 import javax.xml.soap.SOAPException JavaDoc;
27 import javax.xml.soap.SOAPMessage JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.MessageExchangeListener;
32 import org.apache.servicemix.components.util.TransformComponentSupport;
33
34 /**
35  * Converts an inbound JBI message into a <a HREF="http://java.sun.com/xml/saaj/">SAAJ</a> (Soap With Attachments for Java)
36  * request-response and outputs the response back into JBI Bindings. This provides
37  * a message centric way of invoking SOAP services inside providers such as <a HREF="http://ws.apache.org/axis/">Apache Axis</a>
38  *
39  * @version $Revision: 429277 $
40  */

41 public class SaajBinding extends TransformComponentSupport implements MessageExchangeListener {
42
43     private static final transient Log log = LogFactory.getLog(SaajBinding.class);
44
45     private SaajMarshaler marshaler = new SaajMarshaler();
46     private SOAPConnectionFactory JavaDoc connectionFactory;
47     private Object JavaDoc soapEndpoint;
48     private String JavaDoc soapAction;
49
50
51     public SOAPConnectionFactory JavaDoc getConnectionFactory() throws SOAPException JavaDoc {
52         if (connectionFactory == null) {
53             connectionFactory = createConnectionFactory();
54         }
55         return connectionFactory;
56     }
57
58     public void setConnectionFactory(SOAPConnectionFactory JavaDoc connectionFactory) {
59         this.connectionFactory = connectionFactory;
60     }
61
62     public Object JavaDoc getSoapEndpoint() {
63         return soapEndpoint;
64     }
65
66     public void setSoapEndpoint(Object JavaDoc soapEndpoint) {
67         this.soapEndpoint = soapEndpoint;
68     }
69
70     /**
71      * @deprecated use getMarshaler instead
72      */

73     public SaajMarshaler getMarshaller() {
74         return marshaler;
75     }
76
77     /**
78      * @deprecated use setMarshaler instead
79      */

80     public void setMarshaller(SaajMarshaler marshaler) {
81         this.marshaler = marshaler;
82     }
83
84     /**
85      * @return the marshaler
86      */

87     public SaajMarshaler getMarshaler() {
88         return marshaler;
89     }
90
91     /**
92      * @param marshaler the marshaler to set
93      */

94     public void setMarshaler(SaajMarshaler marshaler) {
95         this.marshaler = marshaler;
96     }
97
98     protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception JavaDoc {
99         SOAPConnection JavaDoc connection = getConnectionFactory().createConnection();
100         try {
101             SOAPMessage JavaDoc inMessage = marshaler.createSOAPMessage(in);
102             MimeHeaders JavaDoc mh = inMessage.getMimeHeaders();
103             if (mh.getHeader("SOAPAction") == null) {
104                 if (soapAction != null && soapAction.length() > 0) {
105                     mh.addHeader("SOAPAction", soapAction);
106                 } else {
107                     mh.addHeader("SOAPAction", "\"\"");
108                 }
109                 inMessage.saveChanges();
110             }
111
112             if (log.isDebugEnabled()) {
113                 ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
114                 inMessage.writeTo(buffer);
115                 log.debug(new String JavaDoc(buffer.toByteArray()));
116             }
117             
118             SOAPMessage JavaDoc response = connection.call(inMessage, soapEndpoint);
119             if (response != null) {
120                 marshaler.toNMS(out, response);
121                 return true;
122             } else {
123                 return false;
124             }
125         }
126         finally {
127             try {
128                 connection.close();
129             }
130             catch (SOAPException JavaDoc e) {
131                 log.warn("Failed to close connection: " + e, e);
132             }
133         }
134     }
135
136     protected SOAPConnectionFactory JavaDoc createConnectionFactory() throws SOAPException JavaDoc {
137         return SOAPConnectionFactory.newInstance();
138     }
139
140
141     protected SOAPConnection JavaDoc createConnection() throws SOAPException JavaDoc {
142         return getConnectionFactory().createConnection();
143     }
144
145     /**
146      * @return Returns the soapAction.
147      */

148     public String JavaDoc getSoapAction() {
149         return soapAction;
150     }
151  
152     /**
153      * @param soapAction
154      * The soapAction to set.
155      */

156     public void setSoapAction(String JavaDoc soapAction) {
157         this.soapAction = soapAction;
158     }
159 }
160
Popular Tags