* Renamed HttpClient#getContext() to HttpClient#getDefaultContext()
* Tweaked the way DefaualtHttpClient populates the default HTTP context returned with HttpClient#getDefaultContext() * Added local HTTP context example git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@552166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5847e1f6f2
commit
8f28158826
|
@ -49,7 +49,7 @@ import org.apache.http.params.HttpProtocolParams;
|
|||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class MethodAbort {
|
||||
public class ClientAbortMethod {
|
||||
|
||||
public final static void main(String[] args) throws Exception {
|
||||
|
||||
|
@ -57,7 +57,7 @@ public class MethodAbort {
|
|||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setContentCharset(params, "UTF-8");
|
||||
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
|
||||
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
|
||||
|
||||
HttpClient httpclient = new DefaultHttpClient(params);
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.examples.client;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.HttpState;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.cookie.Cookie;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.params.HttpProtocolParams;
|
||||
|
||||
/**
|
||||
* This examples demonstrates the use of a local HTTP context
|
||||
* populated with custom attributes.
|
||||
*
|
||||
* <!-- empty lines above to avoid 'svn diff' context problems -->
|
||||
* @version $Revision$
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ClientCustomContext {
|
||||
|
||||
public final static void main(String[] args) throws Exception {
|
||||
|
||||
// Initialize default parameters
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setContentCharset(params, "UTF-8");
|
||||
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
|
||||
|
||||
HttpClient httpclient = new DefaultHttpClient(params);
|
||||
|
||||
// Create a local instance of HttpState
|
||||
HttpState localState = new HttpState();
|
||||
|
||||
// Obtain default HTTP context
|
||||
HttpContext defaultContext = httpclient.getDefaultContext();
|
||||
// Create local HTTP context
|
||||
HttpContext localContent = new HttpClientContext(defaultContext);
|
||||
// Bind custom HTTP state to the local context
|
||||
localContent.setAttribute(HttpClientContext.HTTP_STATE, localState);
|
||||
|
||||
HttpGet httpget = new HttpGet("http://www.google.com/");
|
||||
|
||||
System.out.println("executing request " + httpget.getURI());
|
||||
|
||||
// Pass local context as a parameter
|
||||
HttpResponse response = httpclient.execute(httpget, localContent);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println(response.getStatusLine());
|
||||
if (entity != null) {
|
||||
System.out.println("Response content length: " + entity.getContentLength());
|
||||
System.out.println("Chunked?: " + entity.isChunked());
|
||||
}
|
||||
Cookie[] cookies = localState.getCookies();
|
||||
for (int i = 0; i < cookies.length; i++) {
|
||||
System.out.println("Local cookie: " + cookies[i]);
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -60,13 +60,13 @@ public interface HttpClient {
|
|||
|
||||
|
||||
/**
|
||||
* Obtains the default context used by this client.
|
||||
* This context will be used by default when executing requests
|
||||
* with this client.
|
||||
* Obtains the default context used by this client populated with
|
||||
* default attributes. This context will be used by default when
|
||||
* executing requests with this client.
|
||||
*
|
||||
* @return the default context
|
||||
*/
|
||||
HttpContext getContext()
|
||||
HttpContext getDefaultContext()
|
||||
;
|
||||
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ import org.apache.http.protocol.HttpExecutionContext;
|
|||
|
||||
public class HttpClientContext extends HttpExecutionContext {
|
||||
|
||||
public static final String SCHEME_REGISTRY = "http.scheme-registry";
|
||||
public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry";
|
||||
public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry";
|
||||
public static final String HTTP_STATE = "http.state";
|
||||
|
|
|
@ -51,12 +51,12 @@ import org.apache.http.client.HttpState;
|
|||
import org.apache.http.client.RedirectHandler;
|
||||
import org.apache.http.client.RoutedRequest;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.cookie.CookieSpecRegistry;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.BasicHttpProcessor;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HttpExecutionContext;
|
||||
import org.apache.http.protocol.HttpRequestInterceptorList;
|
||||
import org.apache.http.protocol.HttpResponseInterceptorList;
|
||||
|
||||
|
@ -292,10 +292,11 @@ public abstract class AbstractHttpClient
|
|||
}
|
||||
|
||||
|
||||
public synchronized final HttpContext getContext() {
|
||||
public synchronized final HttpContext getDefaultContext() {
|
||||
if (defaultContext == null) {
|
||||
defaultContext = createHttpContext();
|
||||
}
|
||||
populateContext(defaultContext);
|
||||
return defaultContext;
|
||||
}
|
||||
|
||||
|
@ -395,9 +396,7 @@ public abstract class AbstractHttpClient
|
|||
|
||||
synchronized (this) {
|
||||
if (context == null) {
|
||||
context = new HttpExecutionContext(getContext());
|
||||
// Populate the context for this request
|
||||
populateContext(context);
|
||||
context = new HttpClientContext(getDefaultContext());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -428,9 +427,7 @@ public abstract class AbstractHttpClient
|
|||
// all shared objects that are potentially threading unsafe.
|
||||
synchronized (this) {
|
||||
if (context == null) {
|
||||
context = new HttpExecutionContext(getContext());
|
||||
// Populate the context for this request
|
||||
populateContext(context);
|
||||
context = new HttpClientContext(getDefaultContext());
|
||||
}
|
||||
// Create a director for this request
|
||||
director = new DefaultClientRequestDirector(
|
||||
|
|
|
@ -118,7 +118,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
|||
HttpParams params = new BasicHttpParams();
|
||||
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
||||
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
|
||||
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
|
||||
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
|
||||
HttpProtocolParams.setUseExpectContinue(params, true);
|
||||
return params;
|
||||
}
|
||||
|
@ -234,9 +234,6 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
|||
|
||||
|
||||
protected void populateContext(final HttpContext context) {
|
||||
context.setAttribute(
|
||||
HttpClientContext.SCHEME_REGISTRY,
|
||||
getConnectionManager().getSchemeRegistry());
|
||||
context.setAttribute(
|
||||
HttpClientContext.AUTHSCHEME_REGISTRY,
|
||||
getAuthSchemes());
|
||||
|
@ -266,13 +263,17 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
|||
|
||||
HttpHost proxy = (HttpHost) request.getParams().getParameter(
|
||||
HttpClientParams.DEFAULT_PROXY);
|
||||
|
||||
Scheme schm = getConnectionManager().getSchemeRegistry().
|
||||
getScheme(target.getSchemeName());
|
||||
// as it is typically used for TLS/SSL, we assume that
|
||||
// a layered scheme implies a secure connection
|
||||
HttpRoute route = new HttpRoute(target, null, proxy, schm.isLayered());
|
||||
|
||||
HttpRoute route;
|
||||
if (proxy == null) {
|
||||
route = new HttpRoute(target);
|
||||
} else {
|
||||
Scheme schm = getConnectionManager().getSchemeRegistry().
|
||||
getScheme(target.getSchemeName());
|
||||
// as it is typically used for TLS/SSL, we assume that
|
||||
// a layered scheme implies a secure connection
|
||||
route = new HttpRoute(target, null, proxy, schm.isLayered());
|
||||
}
|
||||
return new RoutedRequest.Impl(request, route);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue