KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Runtime state of the engine
17  */

18 package org.apache.axis2.transport.http;
19
20 import org.apache.axis2.Constants;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.context.MessageContext;
23 import org.apache.axis2.description.TransportOutDescription;
24 import org.apache.axis2.engine.AxisEngine;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.util.threadpool.AxisWorker;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.Socket JavaDoc;
34 import java.util.Map JavaDoc;
35
36 public class HTTPWorker implements AxisWorker {
37     protected Log log = LogFactory.getLog(getClass().getName());
38     private ConfigurationContext configurationContext;
39     private Socket JavaDoc socket;
40
41     public HTTPWorker(ConfigurationContext configurationContext, Socket JavaDoc socket) {
42         this.configurationContext = configurationContext;
43         this.socket = socket;
44     }
45
46     public void doWork() {
47         MessageContext msgContext = null;
48         SimpleHTTPOutputStream out = null;
49         try {
50             if (socket != null) {
51                 if (configurationContext == null) {
52                     throw new AxisFault("Engine Must be null");
53                 }
54
55                 InputStream JavaDoc inStream = socket.getInputStream();
56
57                 TransportOutDescription transportOut =
58                     configurationContext.getAxisConfiguration().getTransportOut(
59                         new QName JavaDoc(Constants.TRANSPORT_HTTP));
60                 msgContext =
61                     new MessageContext(
62                         configurationContext,
63                         configurationContext.getAxisConfiguration().getTransportIn(
64                             new QName JavaDoc(Constants.TRANSPORT_HTTP)),
65                         transportOut);
66                 msgContext.setServerSide(true);
67
68                 //parse the Transport Headers
69
HTTPTransportReceiver reciver = new HTTPTransportReceiver();
70                 Map JavaDoc map = reciver.parseTheHeaders(inStream, true);
71
72                 //build a way to write the respone if the Axis choose to do so
73

74                 String JavaDoc transferEncoding = (String JavaDoc) map.get(HTTPConstants.HEADER_TRANSFER_ENCODING);
75                 if (transferEncoding != null
76                     && HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED.equals(transferEncoding)) {
77                     inStream = new ChunkedInputStream(inStream);
78                     out = new SimpleHTTPOutputStream(socket.getOutputStream(), true);
79                 } else {
80                     out = new SimpleHTTPOutputStream(socket.getOutputStream(), false);
81                 }
82                 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
83
84                 //This is way to provide Accsess to the transport information to the transport Sender
85
msgContext.setProperty(
86                     HTTPConstants.HTTPOutTransportInfo,
87                     new SimpleHTTPOutTransportInfo(out));
88
89                 if (HTTPConstants.HEADER_GET.equals(map.get(HTTPConstants.HTTP_REQ_TYPE))) {
90                     //It is GET handle the Get request
91
boolean processed =
92                         HTTPTransportUtils.processHTTPGetRequest(
93                             msgContext,
94                             inStream,
95                             out,
96                             (String JavaDoc) map.get(HTTPConstants.HEADER_CONTENT_TYPE),
97                             (String JavaDoc) map.get(HTTPConstants.HEADER_SOAP_ACTION),
98                             (String JavaDoc) map.get(HTTPConstants.REQUEST_URI),
99                             configurationContext,
100                             HTTPTransportReceiver.getGetRequestParameters(
101                                 (String JavaDoc) map.get(HTTPConstants.REQUEST_URI)));
102
103                     if (!processed) {
104                         out.write(
105                             HTTPTransportReceiver.getServicesHTML(configurationContext).getBytes());
106                         out.flush();
107                     }
108                 } else {
109                     //It is POST, handle it
110
HTTPTransportUtils.processHTTPPostRequest(
111                         msgContext,
112                         inStream,
113                         out,
114                         (String JavaDoc) map.get(HTTPConstants.HEADER_CONTENT_TYPE),
115                         (String JavaDoc) map.get(HTTPConstants.HEADER_SOAP_ACTION),
116                         (String JavaDoc) map.get(HTTPConstants.REQUEST_URI),
117                         configurationContext);
118                 }
119
120                 out.finalize();
121             }
122         } catch (Throwable JavaDoc e) {
123             try {
124                 AxisEngine engine = new AxisEngine(configurationContext);
125                 if (msgContext != null) {
126                     if (out == null) {
127                         out = new SimpleHTTPOutputStream(socket.getOutputStream(), false);
128                     }
129                     msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
130                     engine.handleFault(msgContext, e);
131                 }
132             } catch (Exception JavaDoc e1) {
133                 log.error(e1);
134                 e1.printStackTrace();
135             }
136             e.printStackTrace();
137         } finally {
138             if (socket != null) {
139                 try {
140                     this.socket.close();
141                 } catch (IOException JavaDoc e1) {
142                     log.error(e1);
143                 }
144             }
145         }
146
147     }
148
149 }
150
Popular Tags