* Fully initialize the default instances of HttpParams, CookieSpecRegistry, and AuthSchemeRegistry

* Instantiate the default ClientConnectionManager based on the HttpParams content

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@534839 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-05-03 13:03:41 +00:00
parent 7cda278cd5
commit 55c1c75b10
8 changed files with 515 additions and 5 deletions

View File

@ -65,7 +65,7 @@ public final class AuthSchemeRegistry {
*
* @see #getAuthScheme
*/
public synchronized void registerAuthScheme(
public synchronized void register(
final String name,
final AuthSchemeFactory factory) {
if (name == null) {
@ -83,7 +83,7 @@ public final class AuthSchemeRegistry {
*
* @param name the identifier of the class to unregister
*/
public synchronized void unregisterAuthScheme(final String name) {
public synchronized void unregister(final String name) {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}

View File

@ -0,0 +1,58 @@
/*
* $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.client.params;
public final class AuthPolicy {
private AuthPolicy() {
super();
}
/**
* The NTLM scheme is a proprietary Microsoft Windows Authentication
* protocol (considered to be the most secure among currently supported
* authentication schemes).
*/
public static final String NTLM = "NTLM";
/**
* Digest authentication scheme as defined in RFC2617.
*/
public static final String DIGEST = "Digest";
/**
* Basic authentication scheme as defined in RFC2617 (considered inherently
* insecure, but most widely supported)
*/
public static final String BASIC = "Basic";
}

View File

@ -0,0 +1,66 @@
/*
* $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.client.params;
public final class CookiePolicy {
/**
* The policy that provides high degree of compatibilty
* with common cookie management of popular HTTP agents.
*/
public static final String BROWSER_COMPATIBILITY = "compatibility";
/**
* The Netscape cookie draft compliant policy.
*/
public static final String NETSCAPE = "netscape";
/**
* The RFC 2109 compliant policy.
*/
public static final String RFC_2109 = "rfc2109";
/**
* The RFC 2965 compliant policy.
*/
public static final String RFC_2965 = "rfc2965";
/**
* The policy that ignores cookies.
*/
public static final String IGNORE_COOKIES = "ignoreCookies";
private CookiePolicy() {
super();
}
}

View File

@ -0,0 +1,184 @@
/*
* $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.client.params;
import org.apache.http.params.HttpParams;
/**
* An adaptor for accessing HTTP client parameters in {@link HttpParams}.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision$
*
* @since 3.0
*/
public class HttpClientParams {
/**
* Sets the timeout in milliseconds used when retrieving an instance of
* {@link org.apache.http.conn.ManagedClientConnection} from the
* {@link org.apache.http.conn.ClientConnectionManager}.
* <p>
* This parameter expects a value of type {@link Long}.
* </p>
*/
public static final String CONNECTION_MANAGER_TIMEOUT = "http.connection-manager.timeout";
/**
* Defines the class name of the default {@link org.apache.http.conn.ClientConnectionManager}
* <p>
* This parameter expects a value of type {@link String}.
* </p>
*/
public static final String CONNECTION_MANAGER_FACTORY = "http.connection-manager.factory";
/**
* Defines whether authentication should be attempted preemptively.
* <p>
* This parameter expects a value of type {@link Boolean}.
* </p>
*/
public static final String PREEMPTIVE_AUTHENTICATION = "http.authentication.preemptive";
/**
* Defines whether relative redirects should be rejected.
* <p>
* This parameter expects a value of type {@link Boolean}.
* </p>
*/
public static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect";
/**
* Defines the maximum number of redirects to be followed.
* The limit on number of redirects is intended to prevent infinite loops.
* <p>
* This parameter expects a value of type {@link Integer}.
* </p>
*/
public static final String MAX_REDIRECTS = "http.protocol.max-redirects";
/**
* Defines whether circular redirects (redirects to the same location) should be allowed.
* The HTTP spec is not sufficiently clear whether circular redirects are permitted,
* therefore optionally they can be enabled
* <p>
* This parameter expects a value of type {@link Boolean}.
* </p>
*/
public static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects";
/**
* Defines the name of the cookie specification to be used for HTTP state management.
* <p>
* This parameter expects a value of type {@link String}.
* </p>
*/
public static final String COOKIE_POLICY = "http.protocol.cookie-policy";
private HttpClientParams() {
super();
}
/**
* Returns the timeout in milliseconds used when retrieving an
* {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
* {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
*
* @return timeout in milliseconds.
*/
public static long getConnectionManagerTimeout(final HttpParams params) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
return params.getLongParameter(CONNECTION_MANAGER_TIMEOUT, 0);
}
/**
* Sets the timeout in milliseconds used when retrieving an
* {@link org.apache.commons.httpclient.HttpConnection HTTP connection} from the
* {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection manager}.
*
* @param timeout the timeout in milliseconds
*/
public static void setConnectionManagerTimeout(final HttpParams params, long timeout) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
params.setLongParameter(CONNECTION_MANAGER_TIMEOUT, timeout);
}
/**
* Returns <tt>true</tt> if authentication should be attempted preemptively,
* <tt>false</tt> otherwise.
*
* @return <tt>true</tt> if authentication should be attempted preemptively,
* <tt>false</tt> otherwise.
*/
public static boolean isAuthenticationPreemptive(final HttpParams params) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
return params.getBooleanParameter(PREEMPTIVE_AUTHENTICATION, false);
}
/**
* Sets whether authentication should be attempted preemptively.
*
* @param value <tt>true</tt> if authentication should be attempted preemptively,
* <tt>false</tt> otherwise.
*/
public static void setAuthenticationPreemptive(final HttpParams params, boolean value) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
params.setBooleanParameter(PREEMPTIVE_AUTHENTICATION, value);
}
public static String getCookiePolicy(final HttpParams params) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
String cookiePolicy = (String) params.getParameter(COOKIE_POLICY);
if (cookiePolicy == null) {
return CookiePolicy.BROWSER_COMPATIBILITY;
}
return cookiePolicy;
}
public static void setCookiePolicy(final HttpParams params, final String cookiePolicy) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
params.setParameter(COOKIE_POLICY, cookiePolicy);
}
}

View File

@ -0,0 +1,48 @@
/*
* $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.conn;
import org.apache.http.params.HttpParams;
/**
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/
public interface ClientConnectionManagerFactory {
ClientConnectionManager newInstance(
HttpParams params,
SchemeRegistry schemeRegistry);
}

View File

@ -0,0 +1,50 @@
/*
* $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.impl.auth;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.params.HttpParams;
/**
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/
public class BasicSchemeFactory implements AuthSchemeFactory {
public AuthScheme newInstance(final HttpParams params) {
return new BasicScheme();
}
}

View File

@ -0,0 +1,50 @@
/*
* $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.impl.auth;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.params.HttpParams;
/**
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/
public class DigestSchemeFactory implements AuthSchemeFactory {
public AuthScheme newInstance(final HttpParams params) {
return new DigestScheme();
}
}

View File

@ -35,11 +35,16 @@ import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthSchemeRegistry;
import org.apache.http.client.HttpState;
import org.apache.http.client.RoutedRequest;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionManagerFactory;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
@ -47,10 +52,17 @@ import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.CookieSpecRegistry;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.conn.SingleClientConnManager;
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.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
@ -99,7 +111,12 @@ public class DefaultHttpClient extends AbstractHttpClient {
protected HttpParams createHttpParams() {
return new BasicHttpParams();
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.setUseExpectContinue(params, true);
return params;
}
@ -110,7 +127,29 @@ public class DefaultHttpClient extends AbstractHttpClient {
registry.register(
new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
ClientConnectionManager connManager = null;
HttpParams params = getParams();
String className = (String) params.getParameter(
HttpClientParams.CONNECTION_MANAGER_FACTORY);
if (className != null) {
try {
Class clazz = Class.forName(className);
ClientConnectionManagerFactory factory =
(ClientConnectionManagerFactory) clazz.newInstance();
connManager = factory.newInstance(params, registry);
} catch (ClassNotFoundException ex) {
throw new IllegalStateException("Invalid class name: " + className);
} catch (IllegalAccessException ex) {
throw new IllegalAccessError(ex.getMessage());
} catch (InstantiationException ex) {
throw new InstantiationError(ex.getMessage());
}
} else {
connManager = new SingleClientConnManager(getParams(), registry);
}
return connManager;
}
@ -126,12 +165,27 @@ public class DefaultHttpClient extends AbstractHttpClient {
protected AuthSchemeRegistry createAuthSchemeRegistry() {
AuthSchemeRegistry registry = new AuthSchemeRegistry();
registry.register(
AuthPolicy.BASIC,
new BasicSchemeFactory());
registry.register(
AuthPolicy.DIGEST,
new DigestSchemeFactory());
return registry;
}
protected CookieSpecRegistry createCookieSpecRegistry() {
CookieSpecRegistry registry = new CookieSpecRegistry();
CookieSpecRegistry registry = new CookieSpecRegistry();
registry.register(
CookiePolicy.BROWSER_COMPATIBILITY,
new BrowserCompatSpecFactory());
registry.register(
CookiePolicy.NETSCAPE,
new NetscapeDraftSpecFactory());
registry.register(
CookiePolicy.RFC_2109,
new RFC2109SpecFactory());
return registry;
}