001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.protocol;
031:
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.apache.commons.httpclient.util.LangUtils;
037:
038: /**
039: * A class to encapsulate the specifics of a protocol. This class class also
040: * provides the ability to customize the set and characteristics of the
041: * protocols used.
042: *
043: * <p>One use case for modifying the default set of protocols would be to set a
044: * custom SSL socket factory. This would look something like the following:
045: * <pre>
046: * Protocol myHTTPS = new Protocol( "https", new MySSLSocketFactory(), 443 );
047: *
048: * Protocol.registerProtocol( "https", myHTTPS );
049: * </pre>
050: *
051: * @author Michael Becke
052: * @author Jeff Dever
053: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
054: *
055: * @since 2.0
056: */
057: public class Protocol {
058:
059: /** The available protocols */
060: private static final Map PROTOCOLS = Collections
061: .synchronizedMap(new HashMap());
062:
063: /**
064: * Registers a new protocol with the given identifier. If a protocol with
065: * the given ID already exists it will be overridden. This ID is the same
066: * one used to retrieve the protocol from getProtocol(String).
067: *
068: * @param id the identifier for this protocol
069: * @param protocol the protocol to register
070: *
071: * @see #getProtocol(String)
072: */
073: public static void registerProtocol(String id, Protocol protocol) {
074:
075: if (id == null) {
076: throw new IllegalArgumentException("id is null");
077: }
078: if (protocol == null) {
079: throw new IllegalArgumentException("protocol is null");
080: }
081:
082: PROTOCOLS.put(id, protocol);
083: }
084:
085: /**
086: * Unregisters the protocol with the given ID.
087: *
088: * @param id the ID of the protocol to remove
089: */
090: public static void unregisterProtocol(String id) {
091:
092: if (id == null) {
093: throw new IllegalArgumentException("id is null");
094: }
095:
096: PROTOCOLS.remove(id);
097: }
098:
099: /**
100: * Gets the protocol with the given ID.
101: *
102: * @param id the protocol ID
103: *
104: * @return Protocol a protocol
105: *
106: * @throws IllegalStateException if a protocol with the ID cannot be found
107: */
108: public static Protocol getProtocol(String id)
109: throws IllegalStateException {
110:
111: if (id == null) {
112: throw new IllegalArgumentException("id is null");
113: }
114:
115: Protocol protocol = (Protocol) PROTOCOLS.get(id);
116:
117: if (protocol == null) {
118: protocol = lazyRegisterProtocol(id);
119: }
120:
121: return protocol;
122: }
123:
124: /**
125: * Lazily registers the protocol with the given id.
126: *
127: * @param id the protocol ID
128: *
129: * @return the lazily registered protocol
130: *
131: * @throws IllegalStateException if the protocol with id is not recognized
132: */
133: private static Protocol lazyRegisterProtocol(String id)
134: throws IllegalStateException {
135:
136: if ("http".equals(id)) {
137: final Protocol http = new Protocol("http",
138: DefaultProtocolSocketFactory.getSocketFactory(), 80);
139: Protocol.registerProtocol("http", http);
140: return http;
141: }
142:
143: if ("https".equals(id)) {
144: final Protocol https = new Protocol("https",
145: SSLProtocolSocketFactory.getSocketFactory(), 443);
146: Protocol.registerProtocol("https", https);
147: return https;
148: }
149:
150: throw new IllegalStateException("unsupported protocol: '" + id
151: + "'");
152: }
153:
154: /** the scheme of this protocol (e.g. http, https) */
155: private String scheme;
156:
157: /** The socket factory for this protocol */
158: private ProtocolSocketFactory socketFactory;
159:
160: /** The default port for this protocol */
161: private int defaultPort;
162:
163: /** True if this protocol is secure */
164: private boolean secure;
165:
166: /**
167: * Constructs a new Protocol. Whether the created protocol is secure depends on
168: * the class of <code>factory</code>.
169: *
170: * @param scheme the scheme (e.g. http, https)
171: * @param factory the factory for creating sockets for communication using
172: * this protocol
173: * @param defaultPort the port this protocol defaults to
174: */
175: public Protocol(String scheme, ProtocolSocketFactory factory,
176: int defaultPort) {
177:
178: if (scheme == null) {
179: throw new IllegalArgumentException("scheme is null");
180: }
181: if (factory == null) {
182: throw new IllegalArgumentException("socketFactory is null");
183: }
184: if (defaultPort <= 0) {
185: throw new IllegalArgumentException("port is invalid: "
186: + defaultPort);
187: }
188:
189: this .scheme = scheme;
190: this .socketFactory = factory;
191: this .defaultPort = defaultPort;
192: this .secure = (factory instanceof SecureProtocolSocketFactory);
193: }
194:
195: /**
196: * Constructs a new Protocol. Whether the created protocol is secure depends on
197: * the class of <code>factory</code>.
198: *
199: * @param scheme the scheme (e.g. http, https)
200: * @param factory the factory for creating sockets for communication using
201: * this protocol
202: * @param defaultPort the port this protocol defaults to
203: * @deprecated Use the constructor that uses ProtocolSocketFactory, this version of
204: * the constructor is only kept for backwards API compatibility.
205: */
206: public Protocol(String scheme, SecureProtocolSocketFactory factory,
207: int defaultPort) {
208: this (scheme, (ProtocolSocketFactory) factory, defaultPort);
209: }
210:
211: /**
212: * Returns the defaultPort.
213: * @return int
214: */
215: public int getDefaultPort() {
216: return defaultPort;
217: }
218:
219: /**
220: * Returns the socketFactory. If secure the factory is a
221: * SecureProtocolSocketFactory.
222: * @return SocketFactory
223: */
224: public ProtocolSocketFactory getSocketFactory() {
225: return socketFactory;
226: }
227:
228: /**
229: * Returns the scheme.
230: * @return The scheme
231: */
232: public String getScheme() {
233: return scheme;
234: }
235:
236: /**
237: * Returns true if this protocol is secure
238: * @return true if this protocol is secure
239: */
240: public boolean isSecure() {
241: return secure;
242: }
243:
244: /**
245: * Resolves the correct port for this protocol. Returns the given port if
246: * valid or the default port otherwise.
247: *
248: * @param port the port to be resolved
249: *
250: * @return the given port or the defaultPort
251: */
252: public int resolvePort(int port) {
253: return port <= 0 ? getDefaultPort() : port;
254: }
255:
256: /**
257: * Return a string representation of this object.
258: * @return a string representation of this object.
259: */
260: public String toString() {
261: return scheme + ":" + defaultPort;
262: }
263:
264: /**
265: * Return true if the specified object equals this object.
266: * @param obj The object to compare against.
267: * @return true if the objects are equal.
268: */
269: public boolean equals(Object obj) {
270:
271: if (obj instanceof Protocol) {
272:
273: Protocol p = (Protocol) obj;
274:
275: return (defaultPort == p.getDefaultPort()
276: && scheme.equalsIgnoreCase(p.getScheme())
277: && secure == p.isSecure() && socketFactory.equals(p
278: .getSocketFactory()));
279:
280: } else {
281: return false;
282: }
283:
284: }
285:
286: /**
287: * Return a hash code for this object
288: * @return The hash code.
289: */
290: public int hashCode() {
291: int hash = LangUtils.HASH_SEED;
292: hash = LangUtils.hashCode(hash, this.defaultPort);
293: hash = LangUtils.hashCode(hash, this.scheme.toLowerCase());
294: hash = LangUtils.hashCode(hash, this.secure);
295: hash = LangUtils.hashCode(hash, this.socketFactory);
296: return hash;
297: }
298: }
|