KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > server > TransparentProxyRequestHandler


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v 1.1.2.2 2004/02/22 18:21:18 olegk Exp $
3  * $Revision: 1.1.2.2 $
4  * $Date: 2004/02/22 18:21:18 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.server;
33
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.OutputStreamWriter JavaDoc;
38 import java.io.UnsupportedEncodingException JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.net.Socket JavaDoc;
41
42 import org.apache.commons.httpclient.Header;
43 import org.apache.commons.httpclient.HttpConstants;
44 import org.apache.commons.httpclient.HttpURL;
45 import org.apache.commons.httpclient.URI;
46
47 /**
48  * This request handler can handle the CONNECT method. It does
49  * nothing for any other HTTP methods.
50  *
51  * @author Ortwin Glueck
52  */

53 public class TransparentProxyRequestHandler implements HttpRequestHandler {
54
55     /* (non-Javadoc)
56      * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
57      */

58     public boolean processRequest(SimpleHttpServerConnection conn) throws IOException JavaDoc {
59         RequestLine line = conn.getRequestLine();
60         String JavaDoc method = line.getMethod();
61         if (!"CONNECT".equalsIgnoreCase(method)) return false;
62         URI url = new HttpURL(line.getUri());
63         handshake(conn, url);
64         return true;
65     }
66
67     private void handshake(SimpleHttpServerConnection conn, URI url) throws IOException JavaDoc {
68         Socket JavaDoc targetSocket = new Socket JavaDoc(url.getHost(), url.getPort());
69         InputStream JavaDoc sourceIn = conn.getInputStream();
70         OutputStream JavaDoc sourceOut = conn.getOutputStream();
71         InputStream JavaDoc targetIn = targetSocket.getInputStream();
72         OutputStream JavaDoc targetOut = targetSocket.getOutputStream();
73
74         ResponseWriter out = conn.getWriter();
75         out.println("HTTP/1.1 200 Connection established");
76         out.flush();
77
78         BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut, targetIn, targetOut);
79         bdsp.start();
80         try {
81             bdsp.block();
82         } catch (InterruptedException JavaDoc e) {
83             throw new IOException JavaDoc(e.toString());
84         }
85     }
86     
87     private void sendHeaders(Header[] headers, OutputStream JavaDoc os) throws IOException JavaDoc {
88         Writer JavaDoc out;
89         try {
90             out = new OutputStreamWriter JavaDoc(os, HttpConstants.HTTP_ELEMENT_CHARSET);
91         } catch (UnsupportedEncodingException JavaDoc e) {
92             throw new RuntimeException JavaDoc(e.toString());
93         }
94         for (int i=0; i<headers.length; i++) {
95             out.write(headers[i].toExternalForm());
96         }
97     }
98 }
99
Popular Tags