KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > proxy > Proxy


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java,v 1.17.2.2 2004/04/03 20:32:40 mstover1 Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 */

18
19 package org.apache.jmeter.protocol.http.proxy;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28
29 import org.apache.jmeter.protocol.http.control.HeaderManager;
30 import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
31 import org.apache.jmeter.samplers.SampleResult;
32 import org.apache.jmeter.testelement.TestElement;
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * Thread to handle one client request. Gets the request from the client and
38  * passes it on to the server, then sends the response back to the client.
39  * Information about the request and response is stored so it can be used in a
40  * JMeter test plan.
41  *
42  * @author mike
43  * @version $Revision: 1.17.2.2 $ Last updated: $Date: 2004/04/03 20:32:40 $
44  * Created June 8, 2001
45  */

46 public class Proxy extends Thread JavaDoc
47 {
48     /** Logging. */
49     private static transient Logger log = LoggingManager.getLoggerForClass();
50
51     /** Socket to client. */
52     private Socket JavaDoc clientSocket = null;
53
54     /** Target to receive the generated sampler. */
55     private ProxyControl target;
56     
57     /** Whether or not to capture the HTTP headers. */
58     private boolean captureHttpHeaders;
59
60     /**
61      * Default constructor.
62      */

63     public Proxy()
64     {
65     }
66
67     /**
68      * Create and configure a new Proxy object.
69      *
70      * @param clientSocket the socket connection to the client
71      * @param target the ProxyControl which will receive the generated
72      * sampler
73      */

74     Proxy(Socket JavaDoc clientSocket, ProxyControl target)
75     {
76         configure(clientSocket, target);
77     }
78
79     /**
80      * Configure the Proxy.
81      *
82      * @param clientSocket the socket connection to the client
83      * @param target the ProxyControl which will receive the generated
84      * sampler
85      */

86     void configure(
87         Socket JavaDoc clientSocket,
88         ProxyControl target)
89     {
90         this.target = target;
91         this.clientSocket = clientSocket;
92         this.captureHttpHeaders = target.getCaptureHttpHeaders();
93     }
94
95     /**
96      * Main processing method for the Proxy object
97      */

98     public void run()
99     {
100         HttpRequestHdr request = new HttpRequestHdr();
101         SampleResult result = null;
102         HeaderManager headers = null;
103
104         HTTPSampler sampler = new HTTPSampler();
105         try
106         {
107            request.parse(new BufferedInputStream JavaDoc(clientSocket.getInputStream()));
108
109             sampler = request.getSampler();
110             
111             /*
112              * Create a Header Manager to ensure that the browsers headers
113              * are captured and sent to the server
114             */

115             headers = request.getHeaderManager();
116             sampler.setHeaderManager(headers);
117             result = sampler.sample();
118             writeToClient(
119                 result,
120                 new BufferedOutputStream JavaDoc(clientSocket.getOutputStream()));
121             /*
122              * We don't want to store any cookies in the generated test plan
123              */

124             headers.removeHeaderNamed("cookie");// Always remove cookies
125
}
126         catch (UnknownHostException JavaDoc uhe)
127         {
128             log.warn("Server Not Found.", uhe);
129             writeErrorToClient(HttpReplyHdr.formServerNotFound());
130         }
131         catch (Exception JavaDoc e)
132         {
133             log.error("",e);
134             writeErrorToClient(HttpReplyHdr.formTimeout());
135         }
136         finally
137         {
138             target.deliverSampler(
139                                   sampler,
140                                   new TestElement[] {
141                                     captureHttpHeaders ? headers : null
142                                     },
143                                   result);
144             try
145             {
146                 clientSocket.close();
147             }
148             catch (Exception JavaDoc e)
149             {
150                 log.error("",e);
151             }
152         }
153     }
154
155     /**
156      * Write output to the output stream, then flush and close the stream.
157      *
158      * @param inBytes the bytes to write
159      * @param out the output stream to write to
160      * @throws IOException if an IOException occurs while writing
161      */

162     private void writeToClient(
163         SampleResult res,
164         OutputStream JavaDoc out)
165         throws IOException JavaDoc
166     {
167         try
168         {
169             String JavaDoc responseHeaders = massageResponseHeaders(res,res.getResponseHeaders());
170             out.write((responseHeaders+"\n").getBytes());
171             out.write(res.getResponseData());
172             out.flush();
173             log.debug("Done writing to client");
174         }
175         catch (IOException JavaDoc e)
176         {
177             log.error("",e);
178             throw e;
179         }
180         finally
181         {
182             try
183             {
184                 out.close();
185             }
186             catch (Exception JavaDoc ex)
187             {
188                 log.warn("Error while closing socket", ex);
189             }
190         }
191     }
192     
193     /**
194      * In the event the content was gzipped and unpacked, the content-encoding header must be
195      * removed and the content-length header should be corrected.
196      * @param res
197      * @param headers
198      * @return
199      */

200     private String JavaDoc massageResponseHeaders(SampleResult res,String JavaDoc headers)
201     {
202         int encodingHeaderLoc = headers.indexOf(": gzip");
203         String JavaDoc newHeaders = headers;
204         if(encodingHeaderLoc > -1)
205         {
206             int end = headers.indexOf("\n",encodingHeaderLoc);
207             int begin = headers.lastIndexOf("\n",encodingHeaderLoc);
208             newHeaders = newHeaders.substring(0,begin) + newHeaders.substring(end);
209             int lengthIndex = newHeaders.indexOf("ength: ");
210             end = newHeaders.indexOf("\n",lengthIndex);
211             newHeaders = newHeaders.substring(0,lengthIndex+7) + res.getResponseData().length +
212                     newHeaders.substring(end);
213         }
214         return newHeaders;
215     }
216
217     /**
218      * Write an error message to the client. The message should be the full
219      * HTTP response.
220      *
221      * @param message the message to write
222      */

223     private void writeErrorToClient(String JavaDoc message)
224     {
225         try
226         {
227             OutputStream JavaDoc sockOut = clientSocket.getOutputStream();
228             DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(sockOut);
229             out.writeBytes(message);
230             out.flush();
231         }
232         catch (Exception JavaDoc e)
233         {
234             log.warn("Exception while writing error", e);
235         }
236     }
237 }
Popular Tags