1 31 package org.apache.commons.httpclient.protocol; 32 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 56 public class Protocol { 57 58 59 private static final Map PROTOCOLS = Collections.synchronizedMap(new HashMap ()); 60 61 71 public static void registerProtocol(String id, Protocol protocol) { 72 73 if (id == null) { 74 throw new IllegalArgumentException ("id is null"); 75 } 76 if (protocol == null) { 77 throw new IllegalArgumentException ("protocol is null"); 78 } 79 80 PROTOCOLS.put(id, protocol); 81 } 82 83 88 public static void unregisterProtocol(String id) { 89 90 if (id == null) { 91 throw new IllegalArgumentException ("id is null"); 92 } 93 94 PROTOCOLS.remove(id); 95 } 96 97 106 public static Protocol getProtocol(String id) 107 throws IllegalStateException { 108 109 if (id == null) { 110 throw new IllegalArgumentException ("id is null"); 111 } 112 113 Protocol protocol = (Protocol) PROTOCOLS.get(id); 114 115 if (protocol == null) { 116 protocol = lazyRegisterProtocol(id); 117 } 118 119 return protocol; 120 } 121 122 131 private static Protocol lazyRegisterProtocol(String id) 132 throws IllegalStateException { 133 134 if ("http".equals(id)) { 135 final Protocol http 136 = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(), 80); 137 Protocol.registerProtocol("http", http); 138 return http; 139 } 140 141 if ("https".equals(id)) { 142 final Protocol https 143 = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443); 144 Protocol.registerProtocol("https", https); 145 return https; 146 } 147 148 throw new IllegalStateException ("unsupported protocol: '" + id + "'"); 149 } 150 151 152 153 private String scheme; 154 155 156 private ProtocolSocketFactory socketFactory; 157 158 159 private int defaultPort; 160 161 162 private boolean secure; 163 164 172 public Protocol(String scheme, ProtocolSocketFactory factory, int defaultPort) { 173 174 if (scheme == null) { 175 throw new IllegalArgumentException ("scheme is null"); 176 } 177 if (factory == null) { 178 throw new IllegalArgumentException ("socketFactory is null"); 179 } 180 if (defaultPort <= 0) { 181 throw new IllegalArgumentException ("port is invalid: " + defaultPort); 182 } 183 184 this.scheme = scheme; 185 this.socketFactory = factory; 186 this.defaultPort = defaultPort; 187 this.secure = false; 188 } 189 190 198 public Protocol(String scheme, 199 SecureProtocolSocketFactory factory, int defaultPort) { 200 201 if (scheme == null) { 202 throw new IllegalArgumentException ("scheme is null"); 203 } 204 if (factory == null) { 205 throw new IllegalArgumentException ("socketFactory is null"); 206 } 207 if (defaultPort <= 0) { 208 throw new IllegalArgumentException ("port is invalid: " + defaultPort); 209 } 210 211 this.scheme = scheme; 212 this.socketFactory = factory; 213 this.defaultPort = defaultPort; 214 this.secure = true; 215 } 216 217 221 public int getDefaultPort() { 222 return defaultPort; 223 } 224 225 230 public ProtocolSocketFactory getSocketFactory() { 231 return socketFactory; 232 } 233 234 238 public String getScheme() { 239 return scheme; 240 } 241 242 246 public boolean isSecure() { 247 return secure; 248 } 249 250 258 public int resolvePort(int port) { 259 return port <= 0 ? getDefaultPort() : port; 260 } 261 262 266 public String toString() { 267 return scheme + ":" + defaultPort; 268 } 269 270 275 public boolean equals(Object obj) { 276 277 if (obj instanceof Protocol) { 278 279 Protocol p = (Protocol) obj; 280 281 return ( 282 defaultPort == p.getDefaultPort() 283 && scheme.equalsIgnoreCase(p.getScheme()) 284 && secure == p.isSecure() 285 && socketFactory.equals(p.getSocketFactory())); 286 287 } else { 288 return false; 289 } 290 291 } 292 293 297 public int hashCode() { 298 return scheme.hashCode(); 299 } 300 } 301 | Popular Tags |