1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/Protocol.java,v 1.5.2.1 2004/01/30 19:23:15 mbecke Exp $
3 * $Revision: 1.5.2.1 $
4 * $Date: 2004/01/30 19:23:15 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63 package org.apache.commons.httpclient.protocol;
64
65 import java.util.Collections;
66 import java.util.HashMap;
67 import java.util.Map;
68
69 /***
70 * A class to encapsulate the specifics of a protocol. This class class also
71 * provides the ability to customize the set and characteristics of the
72 * protocols used.
73 *
74 * <p>One use case for modifying the default set of protocols would be to set a
75 * custom SSL socket factory. This would look something like the following:
76 * <pre>
77 * Protocol myHTTPS = new Protocol( "https", new MySSLSocketFactory(), 443 );
78 *
79 * Protocol.registerProtocol( "https", myHTTPS );
80 * </pre>
81 *
82 * @author Michael Becke
83 * @author Jeff Dever
84 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
85 *
86 * @since 2.0
87 */
88 public class Protocol {
89
90 /*** The available protocols */
91 private static final Map PROTOCOLS = Collections.synchronizedMap(new HashMap());
92
93 /***
94 * Registers a new protocol with the given identifier. If a protocol with
95 * the given ID already exists it will be overridden. This ID is the same
96 * one used to retrieve the protocol from getProtocol(String).
97 *
98 * @param id the identifier for this protocol
99 * @param protocol the protocol to register
100 *
101 * @see #getProtocol(String)
102 */
103 public static void registerProtocol(String id, Protocol protocol) {
104
105 if (id == null) {
106 throw new IllegalArgumentException("id is null");
107 }
108 if (protocol == null) {
109 throw new IllegalArgumentException("protocol is null");
110 }
111
112 PROTOCOLS.put(id, protocol);
113 }
114
115 /***
116 * Unregisters the protocol with the given ID.
117 *
118 * @param id the ID of the protocol to remove
119 */
120 public static void unregisterProtocol(String id) {
121
122 if (id == null) {
123 throw new IllegalArgumentException("id is null");
124 }
125
126 PROTOCOLS.remove(id);
127 }
128
129 /***
130 * Gets the protocol with the given ID.
131 *
132 * @param id the protocol ID
133 *
134 * @return Protocol a protocol
135 *
136 * @throws IllegalStateException if a protocol with the ID cannot be found
137 */
138 public static Protocol getProtocol(String id)
139 throws IllegalStateException {
140
141 if (id == null) {
142 throw new IllegalArgumentException("id is null");
143 }
144
145 Protocol protocol = (Protocol) PROTOCOLS.get(id);
146
147 if (protocol == null) {
148 protocol = lazyRegisterProtocol(id);
149 }
150
151 return protocol;
152 }
153
154 /***
155 * Lazily registers the protocol with the given id.
156 *
157 * @param id the protocol ID
158 *
159 * @return the lazily registered protocol
160 *
161 * @throws IllegalStateException if the protocol with id is not recognized
162 */
163 private static Protocol lazyRegisterProtocol(String id)
164 throws IllegalStateException {
165
166 if ("http".equals(id)) {
167 final Protocol http
168 = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(), 80);
169 Protocol.registerProtocol("http", http);
170 return http;
171 }
172
173 if ("https".equals(id)) {
174 final Protocol https
175 = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443);
176 Protocol.registerProtocol("https", https);
177 return https;
178 }
179
180 throw new IllegalStateException("unsupported protocol: '" + id + "'");
181 }
182
183
184 /*** the scheme of this protocol (e.g. http, https) */
185 private String scheme;
186
187 /*** The socket factory for this protocol */
188 private ProtocolSocketFactory socketFactory;
189
190 /*** The default port for this protocol */
191 private int defaultPort;
192
193 /*** True if this protocol is secure */
194 private boolean secure;
195
196 /***
197 * Constructs a new Protocol. The created protcol is insecure.
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 */
204 public Protocol(String scheme, ProtocolSocketFactory factory, int defaultPort) {
205
206 if (scheme == null) {
207 throw new IllegalArgumentException("scheme is null");
208 }
209 if (factory == null) {
210 throw new IllegalArgumentException("socketFactory is null");
211 }
212 if (defaultPort <= 0) {
213 throw new IllegalArgumentException("port is invalid: " + defaultPort);
214 }
215
216 this.scheme = scheme;
217 this.socketFactory = factory;
218 this.defaultPort = defaultPort;
219 this.secure = false;
220 }
221
222 /***
223 * Constructs a new Protocol. The created protcol is secure.
224 *
225 * @param scheme the scheme (e.g. http, https)
226 * @param factory the factory for creating sockets for communication using
227 * this protocol
228 * @param defaultPort the port this protocol defaults to
229 */
230 public Protocol(String scheme,
231 SecureProtocolSocketFactory factory, int defaultPort) {
232
233 if (scheme == null) {
234 throw new IllegalArgumentException("scheme is null");
235 }
236 if (factory == null) {
237 throw new IllegalArgumentException("socketFactory is null");
238 }
239 if (defaultPort <= 0) {
240 throw new IllegalArgumentException("port is invalid: " + defaultPort);
241 }
242
243 this.scheme = scheme;
244 this.socketFactory = factory;
245 this.defaultPort = defaultPort;
246 this.secure = true;
247 }
248
249 /***
250 * Returns the defaultPort.
251 * @return int
252 */
253 public int getDefaultPort() {
254 return defaultPort;
255 }
256
257 /***
258 * Returns the socketFactory. If secure the factory is a
259 * SecureProtocolSocketFactory.
260 * @return SocketFactory
261 */
262 public ProtocolSocketFactory getSocketFactory() {
263 return socketFactory;
264 }
265
266 /***
267 * Returns the scheme.
268 * @return The scheme
269 */
270 public String getScheme() {
271 return scheme;
272 }
273
274 /***
275 * Returns true if this protocol is secure
276 * @return true if this protocol is secure
277 */
278 public boolean isSecure() {
279 return secure;
280 }
281
282 /***
283 * Resolves the correct port for this protocol. Returns the given port if
284 * valid or the default port otherwise.
285 *
286 * @param port the port to be resolved
287 *
288 * @return the given port or the defaultPort
289 */
290 public int resolvePort(int port) {
291 return port <= 0 ? getDefaultPort() : port;
292 }
293
294 /***
295 * Return a string representation of this object.
296 * @return a string representation of this object.
297 */
298 public String toString() {
299 return scheme + ":" + defaultPort;
300 }
301
302 /***
303 * Return true if the specified object equals this object.
304 * @param obj The object to compare against.
305 * @return true if the objects are equal.
306 */
307 public boolean equals(Object obj) {
308
309 if (obj instanceof Protocol) {
310
311 Protocol p = (Protocol) obj;
312
313 return (
314 defaultPort == p.getDefaultPort()
315 && scheme.equalsIgnoreCase(p.getScheme())
316 && secure == p.isSecure()
317 && socketFactory.equals(p.getSocketFactory()));
318
319 } else {
320 return false;
321 }
322
323 }
324
325 /***
326 * Return a hash code for this object
327 * @return The hash code.
328 */
329 public int hashCode() {
330 return scheme.hashCode();
331 }
332 }
This page was automatically generated by Maven