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
This commit is contained in:
Oleg Kalnichevski 2009-10-13 15:38:57 +00:00
parent 0af4f398c0
commit 01b7d5dc40
7 changed files with 77 additions and 251 deletions

View File

@ -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
}

View File

@ -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);
String authority = target.getHostName() + ":" + target.getPort();
HttpRequest connect = new BasicHttpRequest("CONNECT", authority, HttpVersion.HTTP_1_1);
connect.addHeader("Host", authority);
HttpRequest connect = createConnect(target);
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; i<headers.length; i++) {
System.out.println(headers[i]);
}
}
} // class ManagerConnectProxy
}

View File

@ -40,13 +40,12 @@ import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
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 ClientConnectionOperator ClientConnectionOperator}.
@ -71,7 +70,7 @@ public class OperatorConnectDirect {
// Prepare parameters.
// Since this example doesn't use the full core framework,
// only few parameters are actually required.
HttpParams params = new BasicHttpParams();
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, false);

View File

@ -40,9 +40,9 @@ import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
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;
@ -75,7 +75,7 @@ public class OperatorConnectProxy {
// Prepare parameters.
// Since this example doesn't use the full core framework,
// only few parameters are actually required.
HttpParams params = new BasicHttpParams();
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, false);

View File

@ -67,10 +67,10 @@ import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
import org.apache.http.impl.cookie.RFC2109SpecFactory;
import org.apache.http.impl.cookie.RFC2965SpecFactory;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
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.HTTP;
@ -187,7 +187,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
@Override
protected HttpParams createHttpParams() {
HttpParams params = new BasicHttpParams();
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params,
HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,

View File

@ -46,10 +46,10 @@ import org.apache.http.HttpServerConnection;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
@ -64,10 +64,6 @@ import org.apache.http.protocol.ResponseServer;
/**
* Local HTTP server for tests that require one.
* Based on the <code>ElementalHttpServer</code> example in HttpCore.
*
*
*
* <!-- empty lines to avoid 'svn diff' problems -->
*/
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)

View File

@ -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