KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > HTTPTransportSender


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

16 package org.apache.axis2.transport.http;
17
18 import org.apache.axis2.addressing.EndpointReference;
19 import org.apache.axis2.context.ConfigurationContext;
20 import org.apache.axis2.context.MessageContext;
21 import org.apache.axis2.context.OperationContext;
22 import org.apache.axis2.description.Parameter;
23 import org.apache.axis2.description.TransportOutDescription;
24 import org.apache.axis2.engine.AxisFault;
25 import org.apache.axis2.om.OMOutput;
26 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Constants;
27 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants;
28 import org.apache.axis2.transport.AbstractTransportSender;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.net.*;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Class HTTPTransportSender
39  */

40 public class HTTPTransportSender extends AbstractTransportSender {
41     private boolean chuncked = false;
42
43     private String JavaDoc httpVersion = HTTPConstants.HEADER_PROTOCOL_10;
44     public static final String JavaDoc TRANSPORT_SENDER_INFO = "TRANSPORT_SENDER_INFO";
45
46     protected void writeTransportHeaders(
47         OutputStream JavaDoc out,
48         URL url,
49         MessageContext msgContext,
50         int contentLength)
51         throws AxisFault {
52         try {
53              
54             String JavaDoc soapActionString = msgContext.getSoapAction();
55             if(soapActionString == null || soapActionString.length() == 0){
56                 soapActionString = msgContext.getWSAAction();
57             }
58             if(soapActionString == null){
59                 soapActionString = "";
60             }
61             
62             boolean doMTOM = msgContext.isDoingMTOM();
63             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64             buf.append(HTTPConstants.HEADER_POST).append(" ");
65             buf.append(url.getFile()).append(" ").append(httpVersion).append("\n");
66             if(doMTOM){
67                 buf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(": ").append(OMOutput.getContentType(true)).append("\n");
68             }else{
69                 String JavaDoc nsURI = msgContext.getEnvelope().getNamespace().getName();
70                            if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(nsURI)) {
71                                buf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(": ").append(SOAP12Constants.SOAP_12_CONTENT_TYPE);
72                                buf.append("; charset=utf-8\n");
73                            } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(nsURI)) {
74                                buf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(": text/xml; charset=utf-8\n");
75                            }else {
76                                throw new AxisFault("Unknown SOAP Version. Current Axis handles only SOAP 1.1 and SOAP 1.2 messages");
77                            }
78                 
79             }
80             
81             buf.append(HTTPConstants.HEADER_ACCEPT).append(": application/soap+xml, application/dime, multipart/related, text/*\n");
82             buf.append(HTTPConstants.HEADER_HOST).append(": ").append(url.getHost()).append("\n");
83             buf.append(HTTPConstants.HEADER_CACHE_CONTROL).append(": no-cache\n");
84             buf.append(HTTPConstants.HEADER_PRAGMA).append(": no-cache\n");
85             if (chuncked) {
86                 buf
87                     .append(HTTPConstants.HEADER_TRANSFER_ENCODING)
88                     .append(": ")
89                     .append(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED)
90                     .append("\n");
91             }
92             if(!chuncked && !msgContext.isDoingMTOM()) {
93                 buf.append(HTTPConstants.HEADER_CONTENT_LENGTH).append(": " + contentLength + "\n");
94             }
95             if (!msgContext.isDoingREST()) {
96                 buf.append("SOAPAction: \"" + soapActionString + "\"\n");
97             }
98             buf.append("\n");
99             out.write(buf.toString().getBytes());
100         } catch (IOException JavaDoc e) {
101             throw new AxisFault(e);
102         }
103     }
104
105     public void finalizeSendWithOutputStreamFromIncomingConnection(
106         MessageContext msgContext,
107         OutputStream JavaDoc out) {
108     }
109
110     private OutputStream JavaDoc openSocket(MessageContext msgContext)
111         throws AxisFault {
112         TransportSenderInfo transportInfo =
113             (TransportSenderInfo) msgContext.getProperty(TRANSPORT_SENDER_INFO);
114
115         EndpointReference toURL = msgContext.getTo();
116         if (toURL != null) {
117             try {
118                 URL url = new URL(toURL.getAddress());
119                 SocketAddress add =
120                     new InetSocketAddress(
121                         url.getHost(),
122                         url.getPort() == -1 ? 80 : url.getPort());
123                 Socket socket = new Socket();
124                 socket.connect(add);
125                 transportInfo.url = url;
126                 transportInfo.in = socket.getInputStream();
127                 transportInfo.out = socket.getOutputStream();
128                 transportInfo.socket = socket;
129                 return transportInfo.out;
130             } catch (MalformedURLException e) {
131                 throw new AxisFault(e.getMessage(), e);
132             } catch (IOException JavaDoc e) {
133                 throw new AxisFault(e.getMessage(), e);
134             }
135         } else {
136             throw new AxisFault("to EPR must be specified");
137         }
138     }
139
140     public void finalizeSendWithToAddress(
141         MessageContext msgContext,
142         OutputStream JavaDoc out)
143         throws AxisFault {
144         try {
145             TransportSenderInfo transportInfo =
146                 (TransportSenderInfo) msgContext.getProperty(
147                     TRANSPORT_SENDER_INFO);
148             InputStream JavaDoc in = null;
149             if(chuncked || msgContext.isDoingMTOM()){
150                 if (chuncked) {
151                     ((ChunkedOutputStream) out).eos();
152                     in = new ChunkedInputStream(transportInfo.in);
153                 }
154                 in = transportInfo.in;
155             }else{
156                 openSocket(msgContext);
157                 OutputStream JavaDoc outS = transportInfo.out;
158                 in = transportInfo.in;
159                 byte[] bytes = transportInfo.outputStream.toByteArray();
160
161                 //write header to the out put stream
162
writeTransportHeaders(
163                     outS,
164                     transportInfo.url,
165                     msgContext,
166                     bytes.length);
167                 outS.flush();
168                 //write the content to the real output stream
169
outS.write(bytes);
170             }
171
172             transportInfo.socket.shutdownOutput();
173             HTTPTransportReceiver tr = new HTTPTransportReceiver();
174             Map JavaDoc map = tr.parseTheHeaders(transportInfo.in, false);
175             if (!HTTPConstants
176                 .RESPONSE_ACK_CODE_VAL
177                 .equals(map.get(HTTPConstants.RESPONSE_CODE))) {
178                 String JavaDoc transferEncoding =
179                     (String JavaDoc) map.get(HTTPConstants.HEADER_TRANSFER_ENCODING);
180                 if (transferEncoding != null
181                     && HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED.equals(
182                         transferEncoding)) {
183                     in = new ChunkedInputStream(transportInfo.in);
184                 }
185                 msgContext.setProperty(MessageContext.TRANSPORT_IN, in);
186                 
187                 String JavaDoc contentType = (String JavaDoc)map.get(HTTPConstants.HEADER_CONTENT_TYPE);
188                 if (contentType != null && contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) >= 0){
189                     OperationContext opContext = msgContext.getOperationContext();
190                     if(opContext != null){
191                         opContext.setProperty(HTTPConstants.MTOM_RECIVED_CONTENT_TYPE,contentType);
192                     }
193                 }
194             }
195         }catch (IOException JavaDoc e) {
196             throw new AxisFault(e);
197         }
198
199     }
200
201     protected OutputStream JavaDoc openTheConnection(
202         EndpointReference epr,
203         MessageContext msgctx)
204         throws AxisFault {
205         msgctx.setProperty(TRANSPORT_SENDER_INFO, new TransportSenderInfo());
206         
207         if(msgctx.isDoingMTOM() || chuncked){
208             return openSocket(msgctx);
209         } else {
210             TransportSenderInfo transportInfo =
211                 (TransportSenderInfo) msgctx.getProperty(TRANSPORT_SENDER_INFO);
212             transportInfo.outputStream = new ByteArrayOutputStream JavaDoc();
213             return transportInfo.outputStream;
214         }
215     }
216
217     public OutputStream JavaDoc startSendWithOutputStreamFromIncomingConnection(
218         MessageContext msgContext,
219         OutputStream JavaDoc out)
220         throws AxisFault {
221         if(msgContext.isDoingMTOM()){
222             HTTPOutTransportInfo httpOutTransportInfo = (HTTPOutTransportInfo)msgContext.getProperty(HTTPConstants.HTTPOutTransportInfo);
223             if(httpOutTransportInfo != null){
224                 httpOutTransportInfo.setContentType(OMOutput.getContentType(true));
225             }else{
226                 throw new AxisFault("Property "+ HTTPConstants.HTTPOutTransportInfo + " not set by the Server");
227             }
228             
229         }
230         return out;
231     }
232
233     public OutputStream JavaDoc startSendWithToAddress(
234         MessageContext msgContext,
235         OutputStream JavaDoc out)
236         throws AxisFault {
237         try {
238             if(msgContext.isDoingMTOM() || chuncked){
239                 TransportSenderInfo transportInfo =
240                     (TransportSenderInfo) msgContext.getProperty(
241                         TRANSPORT_SENDER_INFO);
242                 writeTransportHeaders(out, transportInfo.url, msgContext, -1);
243                 out.flush();
244                 if (chuncked) {
245                     return new ChunkedOutputStream(out);
246                 } else {
247                     return out;
248                 }
249             }else {
250                 return out;
251             }
252         } catch (IOException JavaDoc e) {
253             throw new AxisFault(e);
254         }
255
256     }
257
258     /* (non-Javadoc)
259      * @see org.apache.axis2.transport.TransportSender#cleanUp()
260      */

261     public void cleanUp(MessageContext msgContext) throws AxisFault {
262         TransportSenderInfo transportInfo =
263             (TransportSenderInfo) msgContext.getProperty(TRANSPORT_SENDER_INFO);
264         try {
265             if (transportInfo.socket != null) {
266                 transportInfo.socket.close();
267             }
268
269         } catch (IOException JavaDoc e) {
270         }
271
272     }
273
274     /* (non-Javadoc)
275      * @see org.apache.axis2.transport.TransportSender#init(org.apache.axis2.context.ConfigurationContext, org.apache.axis2.description.TransportOutDescription)
276      */

277     public void init(
278         ConfigurationContext confContext,
279         TransportOutDescription transportOut)
280         throws AxisFault {
281         //<parameter name="PROTOCOL" locked="xsd:false">HTTP/1.0</parameter> or
282
//<parameter name="PROTOCOL" locked="xsd:false">HTTP/1.1</parameter> is checked
283
Parameter version =
284             transportOut.getParameter(HTTPConstants.PROTOCOL_VERSION);
285         if (version != null) {
286             if (HTTPConstants.HEADER_PROTOCOL_11.equals(version.getValue())) {
287                 this.httpVersion = HTTPConstants.HEADER_PROTOCOL_11;
288                 Parameter transferEncoding =
289                     transportOut.getParameter(
290                         HTTPConstants.HEADER_TRANSFER_ENCODING);
291                 if (transferEncoding != null
292                     && HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED.equals(
293                         transferEncoding.getValue())) {
294                     this.chuncked = true;
295                 }
296             } else if (
297                 HTTPConstants.HEADER_PROTOCOL_10.equals(version.getValue())) {
298                 //TODO HTTP1.0 specific parameters
299
} else {
300                 throw new AxisFault(
301                     "Parameter "
302                         + HTTPConstants.PROTOCOL_VERSION
303                         + " Can have values only HTTP/1.0 or HTTP/1.1");
304             }
305         }
306
307     }
308
309     private class TransportSenderInfo {
310         public InputStream JavaDoc in;
311         public OutputStream JavaDoc out;
312         public ByteArrayOutputStream JavaDoc outputStream;
313         public URL url;
314         public Socket socket;
315     }
316
317 }
318
Popular Tags