KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpServerConnection


1 /*
2  * $Id: HttpServerConnection.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import org.apache.commons.httpclient.ChunkedOutputStream;
14 import org.apache.commons.httpclient.Header;
15 import org.apache.commons.httpclient.HttpParser;
16 import org.apache.commons.httpclient.StatusLine;
17 import org.apache.commons.io.IOUtils;
18 import org.mule.MuleManager;
19
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.SocketException JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * A connection to the SimpleHttpServer.
31  */

32 public class HttpServerConnection
33 {
34     private Socket JavaDoc socket = null;
35     private InputStream JavaDoc in = null;
36     private OutputStream out = null;
37     private boolean keepAlive = false;
38     private String JavaDoc encoding;
39
40     public HttpServerConnection(final Socket JavaDoc socket) throws IOException JavaDoc
41     {
42         this(socket, MuleManager.getConfiguration().getEncoding());
43     }
44
45     public HttpServerConnection(final Socket JavaDoc socket, String JavaDoc encoding) throws IOException JavaDoc
46     {
47         super();
48         if (socket == null)
49         {
50             throw new IllegalArgumentException JavaDoc("Socket may not be null");
51         }
52         this.socket = socket;
53         this.socket.setTcpNoDelay(true);
54         this.in = socket.getInputStream();
55         this.out = new DataOutputStream JavaDoc(socket.getOutputStream());
56         this.encoding = encoding;
57     }
58
59     public synchronized void close()
60     {
61         try
62         {
63             if (socket != null)
64             {
65                 socket.close();
66             }
67         }
68         catch (IOException JavaDoc e)
69         {
70             // ignore
71
}
72         finally
73         {
74             socket = null;
75         }
76     }
77
78     public synchronized boolean isOpen()
79     {
80         return this.socket != null;
81     }
82
83     public void setKeepAlive(boolean b)
84     {
85         this.keepAlive = b;
86     }
87
88     public boolean isKeepAlive()
89     {
90         return this.keepAlive;
91     }
92
93     public InputStream JavaDoc getInputStream()
94     {
95         return this.in;
96     }
97
98     public OutputStream getOutputStream()
99     {
100         return this.out;
101     }
102
103     /**
104      * Returns the ResponseWriter used to write the output to the socket.
105      *
106      * @return This connection's ResponseWriter
107      */

108     public ResponseWriter getWriter() throws UnsupportedEncodingException JavaDoc
109     {
110         return new ResponseWriter(out);
111     }
112
113     public HttpRequest readRequest() throws IOException JavaDoc
114     {
115         String JavaDoc line = null;
116         try
117         {
118             do
119             {
120                 line = HttpParser.readLine(in, encoding);
121             }
122             while (line != null && line.length() == 0);
123
124             if (line == null)
125             {
126                 setKeepAlive(false);
127                 return null;
128             }
129             HttpRequest request = new HttpRequest(RequestLine.parseLine(line), HttpParser.parseHeaders(
130                 this.in, encoding), this.in);
131             return request;
132         }
133         catch (IOException JavaDoc e)
134         {
135             close();
136             throw e;
137         }
138     }
139
140     public HttpResponse readResponse() throws IOException JavaDoc
141     {
142         try
143         {
144             String JavaDoc line = null;
145             do
146             {
147                 line = HttpParser.readLine(in, encoding);
148             }
149             while (line != null && line.length() == 0);
150
151             if (line == null)
152             {
153                 setKeepAlive(false);
154                 return null;
155             }
156             HttpResponse response = new HttpResponse(new StatusLine(line), HttpParser.parseHeaders(this.in,
157                 encoding), this.in);
158             return response;
159         }
160         catch (IOException JavaDoc e)
161         {
162             close();
163             throw e;
164         }
165     }
166
167     public void writeRequest(final HttpRequest request) throws IOException JavaDoc
168     {
169         if (request == null)
170         {
171             return;
172         }
173         ResponseWriter writer = new ResponseWriter(this.out, encoding);
174         writer.println(request.getRequestLine().toString());
175         Iterator JavaDoc item = request.getHeaderIterator();
176         while (item.hasNext())
177         {
178             Header header = (Header)item.next();
179             writer.print(header.toExternalForm());
180         }
181         writer.println();
182         writer.flush();
183
184         OutputStream outstream = this.out;
185         InputStream JavaDoc content = request.getBody();
186         if (content != null)
187         {
188             Header transferenc = request.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
189             if (transferenc != null)
190             {
191                 request.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
192                 if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
193                 {
194                     outstream = new ChunkedOutputStream(outstream);
195                 }
196             }
197
198             IOUtils.copy(content, outstream);
199
200             if (outstream instanceof ChunkedOutputStream)
201             {
202                 ((ChunkedOutputStream)outstream).finish();
203             }
204         }
205
206         outstream.flush();
207     }
208
209     public void writeResponse(final HttpResponse response) throws IOException JavaDoc
210     {
211         if (response == null)
212         {
213             return;
214         }
215         setKeepAlive(response.isKeepAlive());
216         ResponseWriter writer = new ResponseWriter(this.out, encoding);
217         OutputStream outstream = this.out;
218
219         writer.println(response.getStatusLine());
220         Iterator JavaDoc item = response.getHeaderIterator();
221         while (item.hasNext())
222         {
223             Header header = (Header)item.next();
224             writer.print(header.toExternalForm());
225
226         }
227         writer.println();
228         writer.flush();
229
230         InputStream JavaDoc content = response.getBody();
231         if (content != null)
232         {
233             Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
234             if (transferenc != null)
235             {
236                 response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
237                 if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
238                 {
239                     outstream = new ChunkedOutputStream(outstream);
240                 }
241             }
242
243             IOUtils.copy(content, outstream);
244
245             if (outstream instanceof ChunkedOutputStream)
246             {
247                 ((ChunkedOutputStream)outstream).finish();
248             }
249         }
250
251         outstream.flush();
252     }
253
254     public int getSocketTimeout() throws SocketException JavaDoc
255     {
256         return this.socket.getSoTimeout();
257     }
258
259     public void setSocketTimeout(int timeout) throws SocketException JavaDoc
260     {
261         this.socket.setSoTimeout(timeout);
262     }
263 }
264
Popular Tags