PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/ApacheHttpClient/src/java/org/apache/commons/httpclient/protocol/Protocol.java

http://google-enterprise-connector-sharepoint.googlecode.com/
Java | 296 lines | 111 code | 41 blank | 144 comment | 24 complexity | d0f2b8043d39505d54ff9287b99430b9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, CPL-1.0, Apache-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. /*
  2. * $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 $
  3. * $Revision: 480424 $
  4. * $Date: 2006-11-29 11:26:49 +0530 (Wed, 29 Nov 2006) $
  5. *
  6. * ====================================================================
  7. *
  8. * Licensed to the Apache Software Foundation (ASF) under one or more
  9. * contributor license agreements. See the NOTICE file distributed with
  10. * this work for additional information regarding copyright ownership.
  11. * The ASF licenses this file to You under the Apache License, Version 2.0
  12. * (the "License"); you may not use this file except in compliance with
  13. * the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ====================================================================
  23. *
  24. * This software consists of voluntary contributions made by many
  25. * individuals on behalf of the Apache Software Foundation. For more
  26. * information on the Apache Software Foundation, please see
  27. * <http://www.apache.org/>.
  28. *
  29. */
  30. package org.apache.commons.httpclient.protocol;
  31. import java.util.Collections;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import org.apache.commons.httpclient.util.LangUtils;
  35. /**
  36. * A class to encapsulate the specifics of a protocol. This class class also
  37. * provides the ability to customize the set and characteristics of the
  38. * protocols used.
  39. *
  40. * <p>One use case for modifying the default set of protocols would be to set a
  41. * custom SSL socket factory. This would look something like the following:
  42. * <pre>
  43. * Protocol myHTTPS = new Protocol( "https", new MySSLSocketFactory(), 443 );
  44. *
  45. * Protocol.registerProtocol( "https", myHTTPS );
  46. * </pre>
  47. *
  48. * @author Michael Becke
  49. * @author Jeff Dever
  50. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  51. *
  52. * @since 2.0
  53. */
  54. public class Protocol {
  55. /** The available protocols */
  56. private static final Map PROTOCOLS = Collections.synchronizedMap(new HashMap());
  57. /**
  58. * Registers a new protocol with the given identifier. If a protocol with
  59. * the given ID already exists it will be overridden. This ID is the same
  60. * one used to retrieve the protocol from getProtocol(String).
  61. *
  62. * @param id the identifier for this protocol
  63. * @param protocol the protocol to register
  64. *
  65. * @see #getProtocol(String)
  66. */
  67. public static void registerProtocol(String id, Protocol protocol) {
  68. if (id == null) {
  69. throw new IllegalArgumentException("id is null");
  70. }
  71. if (protocol == null) {
  72. throw new IllegalArgumentException("protocol is null");
  73. }
  74. PROTOCOLS.put(id, protocol);
  75. }
  76. /**
  77. * Unregisters the protocol with the given ID.
  78. *
  79. * @param id the ID of the protocol to remove
  80. */
  81. public static void unregisterProtocol(String id) {
  82. if (id == null) {
  83. throw new IllegalArgumentException("id is null");
  84. }
  85. PROTOCOLS.remove(id);
  86. }
  87. /**
  88. * Gets the protocol with the given ID.
  89. *
  90. * @param id the protocol ID
  91. *
  92. * @return Protocol a protocol
  93. *
  94. * @throws IllegalStateException if a protocol with the ID cannot be found
  95. */
  96. public static Protocol getProtocol(String id)
  97. throws IllegalStateException {
  98. if (id == null) {
  99. throw new IllegalArgumentException("id is null");
  100. }
  101. Protocol protocol = (Protocol) PROTOCOLS.get(id);
  102. if (protocol == null) {
  103. protocol = lazyRegisterProtocol(id);
  104. }
  105. return protocol;
  106. }
  107. /**
  108. * Lazily registers the protocol with the given id.
  109. *
  110. * @param id the protocol ID
  111. *
  112. * @return the lazily registered protocol
  113. *
  114. * @throws IllegalStateException if the protocol with id is not recognized
  115. */
  116. private static Protocol lazyRegisterProtocol(String id)
  117. throws IllegalStateException {
  118. if ("http".equals(id)) {
  119. final Protocol http
  120. = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(), 80);
  121. Protocol.registerProtocol("http", http);
  122. return http;
  123. }
  124. if ("https".equals(id)) {
  125. final Protocol https
  126. = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443);
  127. Protocol.registerProtocol("https", https);
  128. return https;
  129. }
  130. throw new IllegalStateException("unsupported protocol: '" + id + "'");
  131. }
  132. /** the scheme of this protocol (e.g. http, https) */
  133. private String scheme;
  134. /** The socket factory for this protocol */
  135. private ProtocolSocketFactory socketFactory;
  136. /** The default port for this protocol */
  137. private int defaultPort;
  138. /** True if this protocol is secure */
  139. private boolean secure;
  140. /**
  141. * Constructs a new Protocol. Whether the created protocol is secure depends on
  142. * the class of <code>factory</code>.
  143. *
  144. * @param scheme the scheme (e.g. http, https)
  145. * @param factory the factory for creating sockets for communication using
  146. * this protocol
  147. * @param defaultPort the port this protocol defaults to
  148. */
  149. public Protocol(String scheme, ProtocolSocketFactory factory, int defaultPort) {
  150. if (scheme == null) {
  151. throw new IllegalArgumentException("scheme is null");
  152. }
  153. if (factory == null) {
  154. throw new IllegalArgumentException("socketFactory is null");
  155. }
  156. if (defaultPort <= 0) {
  157. throw new IllegalArgumentException("port is invalid: " + defaultPort);
  158. }
  159. this.scheme = scheme;
  160. this.socketFactory = factory;
  161. this.defaultPort = defaultPort;
  162. this.secure = (factory instanceof SecureProtocolSocketFactory);
  163. }
  164. /**
  165. * Constructs a new Protocol. Whether the created protocol is secure depends on
  166. * the class of <code>factory</code>.
  167. *
  168. * @param scheme the scheme (e.g. http, https)
  169. * @param factory the factory for creating sockets for communication using
  170. * this protocol
  171. * @param defaultPort the port this protocol defaults to
  172. * @deprecated Use the constructor that uses ProtocolSocketFactory, this version of
  173. * the constructor is only kept for backwards API compatibility.
  174. */
  175. public Protocol(String scheme,
  176. SecureProtocolSocketFactory factory, int defaultPort) {
  177. this(scheme, (ProtocolSocketFactory) factory, defaultPort);
  178. }
  179. /**
  180. * Returns the defaultPort.
  181. * @return int
  182. */
  183. public int getDefaultPort() {
  184. return defaultPort;
  185. }
  186. /**
  187. * Returns the socketFactory. If secure the factory is a
  188. * SecureProtocolSocketFactory.
  189. * @return SocketFactory
  190. */
  191. public ProtocolSocketFactory getSocketFactory() {
  192. return socketFactory;
  193. }
  194. /**
  195. * Returns the scheme.
  196. * @return The scheme
  197. */
  198. public String getScheme() {
  199. return scheme;
  200. }
  201. /**
  202. * Returns true if this protocol is secure
  203. * @return true if this protocol is secure
  204. */
  205. public boolean isSecure() {
  206. return secure;
  207. }
  208. /**
  209. * Resolves the correct port for this protocol. Returns the given port if
  210. * valid or the default port otherwise.
  211. *
  212. * @param port the port to be resolved
  213. *
  214. * @return the given port or the defaultPort
  215. */
  216. public int resolvePort(int port) {
  217. return port <= 0 ? getDefaultPort() : port;
  218. }
  219. /**
  220. * Return a string representation of this object.
  221. * @return a string representation of this object.
  222. */
  223. public String toString() {
  224. return scheme + ":" + defaultPort;
  225. }
  226. /**
  227. * Return true if the specified object equals this object.
  228. * @param obj The object to compare against.
  229. * @return true if the objects are equal.
  230. */
  231. public boolean equals(Object obj) {
  232. if (obj instanceof Protocol) {
  233. Protocol p = (Protocol) obj;
  234. return (
  235. defaultPort == p.getDefaultPort()
  236. && scheme.equalsIgnoreCase(p.getScheme())
  237. && secure == p.isSecure()
  238. && socketFactory.equals(p.getSocketFactory()));
  239. } else {
  240. return false;
  241. }
  242. }
  243. /**
  244. * Return a hash code for this object
  245. * @return The hash code.
  246. */
  247. public int hashCode() {
  248. int hash = LangUtils.HASH_SEED;
  249. hash = LangUtils.hashCode(hash, this.defaultPort);
  250. hash = LangUtils.hashCode(hash, this.scheme.toLowerCase());
  251. hash = LangUtils.hashCode(hash, this.secure);
  252. hash = LangUtils.hashCode(hash, this.socketFactory);
  253. return hash;
  254. }
  255. }