From 01b7d5dc40d4c0054f36c976dc7528a85684fe09 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 13 Oct 2009 15:38:57 +0000 Subject: [PATCH] HTTPCLIENT-824: Use synchronized SyncBasicHttpParams instead of non-synchronized BasicHttpParams where HttpParams can be accessed simultaneously by multiple threads git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@824810 13f79535-47bb-0310-9956-ffa450edef68 --- .../examples/conn/ManagerConnectDirect.java | 120 +++--------- .../examples/conn/ManagerConnectProxy.java | 182 +++++------------- .../examples/conn/OperatorConnectDirect.java | 5 +- .../examples/conn/OperatorConnectProxy.java | 4 +- .../http/impl/client/DefaultHttpClient.java | 4 +- .../http/localserver/LocalTestServer.java | 8 +- .../http/localserver/ServerTestBase.java | 5 +- 7 files changed, 77 insertions(+), 251 deletions(-) diff --git a/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java b/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java index c1f77420b..f5604c2c4 100644 --- a/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java +++ b/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java @@ -27,7 +27,6 @@ package org.apache.http.examples.conn; - import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -43,14 +42,12 @@ import org.apache.http.conn.ClientConnectionRequest; import org.apache.http.conn.ManagedClientConnection; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.message.BasicHttpRequest; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; +import org.apache.http.params.SyncBasicHttpParams; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.BasicHttpContext; - - /** * How to open a direct connection using * {@link ClientConnectionManager ClientConnectionManager}. @@ -58,41 +55,38 @@ import org.apache.http.protocol.BasicHttpContext; * The subsequent message exchange in this example should not * be used as a template. * - * - * * @since 4.0 */ public class ManagerConnectDirect { - /** - * The default parameters. - * Instantiated in {@link #setup setup}. - */ - private static HttpParams defaultParameters = null; - - /** - * The scheme registry. - * Instantiated in {@link #setup setup}. - */ - private static SchemeRegistry supportedSchemes; - - /** * Main entry point to this example. * * @param args ignored */ - public final static void main(String[] args) - throws Exception { + public final static void main(String[] args) throws Exception { - final HttpHost target = new HttpHost("jakarta.apache.org", 80, "http"); + HttpHost target = new HttpHost("www.apache.org", 80, "http"); - setup(); // some general setup + // Register the "http" protocol scheme, it is required + // by the default operator to look up socket factories. + SchemeRegistry supportedSchemes = new SchemeRegistry(); + SocketFactory sf = PlainSocketFactory.getSocketFactory(); + supportedSchemes.register(new Scheme("http", sf, 80)); - ClientConnectionManager clcm = createManager(); + // Prepare parameters. + // Since this example doesn't use the full core framework, + // only few parameters are actually required. + HttpParams params = new SyncBasicHttpParams(); + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); + HttpProtocolParams.setUseExpectContinue(params, false); - HttpRequest req = createRequest(target); - HttpContext ctx = createContext(); + ClientConnectionManager clcm = new ThreadSafeClientConnManager(supportedSchemes); + + HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1); + req.addHeader("Host", target.getHostName()); + + HttpContext ctx = new BasicHttpContext(); System.out.println("preparing route to " + target); HttpRoute route = new HttpRoute @@ -103,7 +97,7 @@ public class ManagerConnectDirect { ManagedClientConnection conn = connRequest.getConnection(0, null); try { System.out.println("opening connection"); - conn.open(route, ctx, getParams()); + conn.open(route, ctx, params); System.out.println("sending request"); conn.sendRequestHeader(req); @@ -130,9 +124,9 @@ public class ManagerConnectDirect { System.out.println("shutting down connection"); try { conn.shutdown(); - } catch (Exception x) { + } catch (Exception ex) { System.out.println("problem during shutdown"); - x.printStackTrace(System.out); + ex.printStackTrace(); } } @@ -140,73 +134,7 @@ public class ManagerConnectDirect { clcm.releaseConnection(conn, -1, null); } - } // main - - - private final static ClientConnectionManager createManager() { - - return new ThreadSafeClientConnManager(supportedSchemes); } - - /** - * Performs general setup. - * This should be called only once. - */ - private final static void setup() { - - // Register the "http" protocol scheme, it is required - // by the default operator to look up socket factories. - supportedSchemes = new SchemeRegistry(); - SocketFactory sf = PlainSocketFactory.getSocketFactory(); - supportedSchemes.register(new Scheme("http", sf, 80)); - - // Prepare parameters. - // Since this example doesn't use the full core framework, - // only few parameters are actually required. - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setUseExpectContinue(params, false); - defaultParameters = params; - - } // setup - - - private final static HttpParams getParams() { - return defaultParameters; - } - - - /** - * Creates a request to execute in this example. - * In a real application, request interceptors should be used - * to add the required headers. - * - * @param target the target server for the request - * - * @return a request without an entity - */ - private final static HttpRequest createRequest(HttpHost target) { - - HttpRequest req = new BasicHttpRequest - ("OPTIONS", "*", HttpVersion.HTTP_1_1); - - req.addHeader("Host", target.getHostName()); - - return req; - } - - - /** - * Creates a context for executing a request. - * Since this example doesn't really use the execution framework, - * the context can be left empty. - * - * @return a new, empty context - */ - private final static HttpContext createContext() { - return new BasicHttpContext(null); - } - -} // class ManagerConnectDirect +} diff --git a/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java b/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java index 9b10ec952..1f5048ad5 100644 --- a/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java +++ b/httpclient/src/examples/org/apache/http/examples/conn/ManagerConnectProxy.java @@ -27,7 +27,6 @@ package org.apache.http.examples.conn; - import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -44,14 +43,12 @@ import org.apache.http.conn.scheme.SocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.message.BasicHttpRequest; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; +import org.apache.http.params.SyncBasicHttpParams; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.BasicHttpContext; - - /** * How to open a secure connection through a proxy using * {@link ClientConnectionManager ClientConnectionManager}. @@ -59,45 +56,42 @@ import org.apache.http.protocol.BasicHttpContext; * The message exchange, both subsequently and for tunnelling, * should not be used as a template. * - * - * * @since 4.0 */ public class ManagerConnectProxy { - /** - * The default parameters. - * Instantiated in {@link #setup setup}. - */ - private static HttpParams defaultParameters = null; - - /** - * The scheme registry. - * Instantiated in {@link #setup setup}. - */ - private static SchemeRegistry supportedSchemes; - - /** * Main entry point to this example. * * @param args ignored */ - public final static void main(String[] args) - throws Exception { + public final static void main(String[] args) throws Exception { // make sure to use a proxy that supports CONNECT - final HttpHost target = - new HttpHost("issues.apache.org", 443, "https"); - final HttpHost proxy = - new HttpHost("127.0.0.1", 8666, "http"); + HttpHost target = new HttpHost("issues.apache.org", 443, "https"); + HttpHost proxy = new HttpHost("127.0.0.1", 8666, "http"); - setup(); // some general setup + // Register the "http" and "https" protocol schemes, they are + // required by the default operator to look up socket factories. + SchemeRegistry supportedSchemes = new SchemeRegistry(); + SocketFactory sf = PlainSocketFactory.getSocketFactory(); + supportedSchemes.register(new Scheme("http", sf, 80)); + sf = SSLSocketFactory.getSocketFactory(); + supportedSchemes.register(new Scheme("https", sf, 80)); - ClientConnectionManager clcm = createManager(); + // Prepare parameters. + // Since this example doesn't use the full core framework, + // only few parameters are actually required. + HttpParams params = new SyncBasicHttpParams(); + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); + HttpProtocolParams.setUseExpectContinue(params, false); - HttpRequest req = createRequest(target); - HttpContext ctx = createContext(); + ClientConnectionManager clcm = new ThreadSafeClientConnManager(supportedSchemes); + + HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1); + req.addHeader("Host", target.getHostName()); + + HttpContext ctx = new BasicHttpContext(); System.out.println("preparing route to " + target + " via " + proxy); HttpRoute route = new HttpRoute @@ -109,9 +103,12 @@ public class ManagerConnectProxy { ManagedClientConnection conn = connRequest.getConnection(0, null); try { System.out.println("opening connection"); - conn.open(route, ctx, getParams()); + conn.open(route, ctx, params); - HttpRequest connect = createConnect(target); + String authority = target.getHostName() + ":" + target.getPort(); + HttpRequest connect = new BasicHttpRequest("CONNECT", authority, HttpVersion.HTTP_1_1); + connect.addHeader("Host", authority); + System.out.println("opening tunnel to " + target); conn.sendRequestHeader(connect); // there is no request entity @@ -120,7 +117,11 @@ public class ManagerConnectProxy { System.out.println("receiving confirmation for tunnel"); HttpResponse connected = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); - printResponseHeader(connected); + System.out.println(connected.getStatusLine()); + Header[] headers = connected.getAllHeaders(); + for (int i = 0; i < headers.length; i++) { + System.out.println(headers[i]); + } System.out.println("----------------------------------------"); int status = connected.getStatusLine().getStatusCode(); if ((status < 200) || (status > 299)) { @@ -130,10 +131,10 @@ public class ManagerConnectProxy { System.out.println("receiving response body (ignored)"); conn.receiveResponseEntity(connected); - conn.tunnelTarget(false, getParams()); + conn.tunnelTarget(false, params); System.out.println("layering secure connection"); - conn.layerProtocol(ctx, getParams()); + conn.layerProtocol(ctx, params); // finally we have the secure connection and can send the request @@ -146,7 +147,11 @@ public class ManagerConnectProxy { HttpResponse rsp = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); - printResponseHeader(rsp); + System.out.println(rsp.getStatusLine()); + headers = rsp.getAllHeaders(); + for (int i = 0; i < headers.length; i++) { + System.out.println(headers[i]); + } System.out.println("----------------------------------------"); System.out.println("closing connection"); @@ -158,9 +163,9 @@ public class ManagerConnectProxy { System.out.println("shutting down connection"); try { conn.shutdown(); - } catch (Exception x) { + } catch (Exception ex) { System.out.println("problem during shutdown"); - x.printStackTrace(System.out); + ex.printStackTrace(); } } @@ -168,108 +173,7 @@ public class ManagerConnectProxy { clcm.releaseConnection(conn, -1, null); } - } // main - - - private final static ClientConnectionManager createManager() { - - return new ThreadSafeClientConnManager(supportedSchemes); } - - /** - * Performs general setup. - * This should be called only once. - */ - private final static void setup() { - - // Register the "http" and "https" protocol schemes, they are - // required by the default operator to look up socket factories. - supportedSchemes = new SchemeRegistry(); - SocketFactory sf = PlainSocketFactory.getSocketFactory(); - supportedSchemes.register(new Scheme("http", sf, 80)); - sf = SSLSocketFactory.getSocketFactory(); - supportedSchemes.register(new Scheme("https", sf, 80)); - - // Prepare parameters. - // Since this example doesn't use the full core framework, - // only few parameters are actually required. - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setUseExpectContinue(params, false); - defaultParameters = params; - - } // setup - - - private final static HttpParams getParams() { - return defaultParameters; - } - - - /** - * Creates a request to tunnel a connection. - * In a real application, request interceptors should be used - * to add the required headers. - * - * @param target the target server for the tunnel - * - * @return a CONNECT request without an entity - */ - private final static HttpRequest createConnect(HttpHost target) { - - // see RFC 2817, section 5.2 - final String authority = target.getHostName()+":"+target.getPort(); - - HttpRequest req = new BasicHttpRequest - ("CONNECT", authority, HttpVersion.HTTP_1_1); - - req.addHeader("Host", authority); - - return req; - } - - - /** - * Creates a request to execute in this example. - * In a real application, request interceptors should be used - * to add the required headers. - * - * @param target the target server for the request - * - * @return a request without an entity - */ - private final static HttpRequest createRequest(HttpHost target) { - - HttpRequest req = new BasicHttpRequest - ("OPTIONS", "*", HttpVersion.HTTP_1_1); - - req.addHeader("Host", target.getHostName()); - - return req; - } - - - /** - * Creates a context for executing a request. - * Since this example doesn't really use the execution framework, - * the context can be left empty. - * - * @return a new, empty context - */ - private final static HttpContext createContext() { - return new BasicHttpContext(null); - } - - - private final static void printResponseHeader(HttpResponse rsp) { - - System.out.println(rsp.getStatusLine()); - Header[] headers = rsp.getAllHeaders(); - for (int i=0; iElementalHttpServer example in HttpCore. - * - * - * - * */ public class LocalTestServer { @@ -178,7 +174,7 @@ public class LocalTestServer { * @return default parameters */ protected HttpParams newDefaultParams() { - HttpParams params = new BasicHttpParams(); + HttpParams params = new SyncBasicHttpParams(); params .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000) diff --git a/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java b/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java index 431b5be9f..cbf3370b3 100644 --- a/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java +++ b/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java @@ -36,16 +36,15 @@ import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.scheme.SocketFactory; import org.apache.http.impl.DefaultHttpClientConnection; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; +import org.apache.http.params.SyncBasicHttpParams; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.RequestConnControl; import org.apache.http.protocol.RequestContent; - /** * Base class for tests using {@link LocalTestServer LocalTestServer}. * Note that the test server will be {@link #setUp set up} before each @@ -104,7 +103,7 @@ public abstract class ServerTestBase extends BasicServerTestBase { protected void setUp() throws Exception { if (defaultParams == null) { - defaultParams = new BasicHttpParams(); + defaultParams = new SyncBasicHttpParams(); HttpProtocolParams.setVersion (defaultParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset