Index: src/java/org/apache/commons/httpclient/HostConfiguration.java
===================================================================
--- src/java/org/apache/commons/httpclient/HostConfiguration.java (revision 915514)
+++ src/java/org/apache/commons/httpclient/HostConfiguration.java (working copy)
@@ -31,7 +31,9 @@
package org.apache.commons.httpclient;
import org.apache.commons.httpclient.params.HostParams;
+import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.ProtocolFactory;
import org.apache.commons.httpclient.util.LangUtils;
import java.net.InetAddress;
@@ -250,7 +252,10 @@
* @param protocol The protocol.
*/
public synchronized void setHost(final String host, int port, final String protocol) {
- this.host = new HttpHost(host, port, Protocol.getProtocol(protocol));
+ if (this.params.getParameter(HttpClientParams.PROTOCOL_FACTORY) != null)
+ this.host = new HttpHost(host, port, ((ProtocolFactory)this.params.getParameter(HttpClientParams.PROTOCOL_FACTORY)).getProtocol(protocol));
+ else
+ this.host = new HttpHost(host, port, Protocol.getProtocol(protocol));
}
/**
@@ -293,7 +298,7 @@
* @param port The port
*/
public synchronized void setHost(final String host, int port) {
- setHost(host, port, Protocol.getProtocol("http"));
+ setHost(host, port, "http");
}
/**
@@ -302,7 +307,11 @@
* @param host The host(IP or DNS name).
*/
public synchronized void setHost(final String host) {
- Protocol defaultProtocol = Protocol.getProtocol("http");
+ Protocol defaultProtocol;
+ if (this.params.getParameter(HttpClientParams.PROTOCOL_FACTORY) != null)
+ defaultProtocol = ((ProtocolFactory)this.params.getParameter(HttpClientParams.PROTOCOL_FACTORY)).getProtocol("http");
+ else
+ defaultProtocol = Protocol.getProtocol("http");
setHost(host, defaultProtocol.getDefaultPort(), defaultProtocol);
}
Index: src/java/org/apache/commons/httpclient/protocol/ProtocolFactory.java
===================================================================
--- src/java/org/apache/commons/httpclient/protocol/ProtocolFactory.java (revision 0)
+++ src/java/org/apache/commons/httpclient/protocol/ProtocolFactory.java (revision 0)
@@ -0,0 +1,150 @@
+/*
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/protocol/Protocol.java,v 1.10 2004/04/18 23:51:38 jsdever Exp $
+ * $Revision: 157457 $
+ * $Date: 2005-03-14 15:23:16 -0500 (Mon, 14 Mar 2005) $
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed 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
+ *
One use case for modifying the default set of protocols would be to set a + * custom SSL socket factory. This would look something like the following: + *
+ * Protocol myHTTPS = new Protocol( "https", new MySSLSocketFactory(), 443 ); + * + * Protocol.registerProtocol( "https", myHTTPS ); + *+ * + * @author Michael Becke + * @author Jeff Dever + * @author Mike Bowler + * + * @since 2.0 + */ +public class ProtocolFactory { + + /** The available protocols */ + private Map PROTOCOLS = Collections.synchronizedMap(new HashMap()); + + /** + * Registers a new protocol with the given identifier. If a protocol with + * the given ID already exists it will be overridden. This ID is the same + * one used to retrieve the protocol from getProtocol(String). + * + * @param id the identifier for this protocol + * @param protocol the protocol to register + * + * @see #getProtocol(String) + */ + public void registerProtocol(String id, Protocol protocol) { + + if (id == null) { + throw new IllegalArgumentException("id is null"); + } + if (protocol == null) { + throw new IllegalArgumentException("protocol is null"); + } + + PROTOCOLS.put(id, protocol); + } + + /** + * Unregisters the protocol with the given ID. + * + * @param id the ID of the protocol to remove + */ + public void unregisterProtocol(String id) { + + if (id == null) { + throw new IllegalArgumentException("id is null"); + } + + PROTOCOLS.remove(id); + } + + /** + * Gets the protocol with the given ID. + * + * @param id the protocol ID + * + * @return Protocol a protocol + * + * @throws IllegalStateException if a protocol with the ID cannot be found + */ + public Protocol getProtocol(String id) + throws IllegalStateException { + + if (id == null) { + throw new IllegalArgumentException("id is null"); + } + + Protocol protocol = (Protocol) PROTOCOLS.get(id); + + if (protocol == null) { + protocol = lazyRegisterProtocol(id); + } + + return protocol; + } + + /** + * Lazily registers the protocol with the given id. + * + * @param id the protocol ID + * + * @return the lazily registered protocol + * + * @throws IllegalStateException if the protocol with id is not recognized + */ + private Protocol lazyRegisterProtocol(String id) + throws IllegalStateException { + + if ("http".equals(id)) { + final Protocol http + = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(), 80); + Protocol.registerProtocol("http", http); + return http; + } + + if ("https".equals(id)) { + final Protocol https + = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443); + Protocol.registerProtocol("https", https); + return https; + } + + throw new IllegalStateException("unsupported protocol: '" + id + "'"); + } +} Index: src/java/org/apache/commons/httpclient/params/HttpClientParams.java =================================================================== --- src/java/org/apache/commons/httpclient/params/HttpClientParams.java (revision 915514) +++ src/java/org/apache/commons/httpclient/params/HttpClientParams.java (working copy) @@ -81,6 +81,14 @@ */ public static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect"; + /** + * Supplies a ProtocolFactory object, for custom protocol support even across redirections. + *
+ * This parameter expects a value of type {@link ProtocolFactory}. + *
+ */ + public static final String PROTOCOL_FACTORY = "http.protocol.factory"; + /** * Defines the maximum number of redirects to be followed. * The limit on number of redirects is intended to prevent infinite loops.