1 13 14 package com.go.teaservlet; 15 16 import java.io.*; 17 import java.net.*; 18 import java.util.*; 19 import com.go.trove.net.*; 20 import com.go.trove.util.*; 21 import com.go.trove.log.*; 22 23 31 class HttpResource { 32 private static long DEFAULT_TIMEOUT = 10000; 34 35 private static Map cSocketFactories; 37 38 private static Map cHttpResources; 40 41 static { 42 cSocketFactories = new Cache(10); 43 cHttpResources = new Cache(100); 44 } 45 46 public static HttpResource get(URL url) { 47 synchronized (cHttpResources) { 48 HttpResource res = (HttpResource)cHttpResources.get(url); 49 if (res == null) { 50 HostPort key = (HostPort)Utils.intern(new HostPort(url)); 51 52 SocketFactory factory = 53 (SocketFactory)cSocketFactories.get(key); 54 if (factory == null) { 55 String [] hosts = {key.mHost}; 56 int[] ports = {key.mPort}; 57 factory = new MultiPooledSocketFactory 58 (hosts, ports, DEFAULT_TIMEOUT); 59 cSocketFactories.put(key, factory); 60 } 61 62 res = new HttpResource(key, url.getFile(), factory); 63 cHttpResources.put(url, res); 64 } 65 return res; 66 } 67 } 68 69 private HostPort mHostPort; 70 private String mURI; 71 private SocketFactory mFactory; 72 73 private int mHeadState; 77 78 private HttpResource(HostPort hostPort, String uri, 79 SocketFactory factory) { 80 mHostPort = hostPort; 81 mURI = uri; 82 mFactory = factory; 83 } 84 85 public boolean exists() throws IOException { 86 return exists(mFactory.getDefaultTimeout()); 87 } 88 89 public boolean exists(long timeout) throws IOException { 90 HttpClient client = new HttpClient(mFactory, timeout); 91 client.setURI(mURI); 92 93 if (mHeadState == 0) { 94 client.setMethod("HEAD"); 95 } 96 else if (mHeadState == 1) { 97 client.setMethod("HEAD").setPersistent(true); 98 } 99 else { 100 } 103 104 try { 105 HttpClient.Response response = client.getResponse(); 106 107 InputStream in = response.getInputStream(); 109 while (in.read() >= 0); 110 111 try { 112 int statusCode = response.getStatusCode(); 113 switch (statusCode) { 114 case 200: if (mHeadState == 0) { 116 mHeadState = 1; 117 } 118 return true; 119 120 case 404: if (mHeadState == 0) { 122 mHeadState = 1; 123 } 124 return false; 125 126 case 301: if (mHeadState == 0) { 128 mHeadState = 1; 129 } 130 mURI = response.getHeaders().getString("Location"); 131 client.setURI(mURI); 132 return client.getResponse().getStatusCode() == 200; 133 134 case 302: if (mHeadState == 0) { 136 mHeadState = 1; 137 } 138 client.setURI(response.getHeaders().getString("Location")); 139 return client.getResponse().getStatusCode() == 200; 140 141 case 501: break; 143 144 case 503: if (mHeadState == 0) { 146 mHeadState = 1; 147 } 148 Syslog.debug 149 ("Response from " + mHostPort + mURI + ": " + 150 statusCode + ' ' + response.getStatusMessage()); 151 return false; 152 153 default: 154 throw new IOException 155 ("Response from " + mHostPort + mURI + ": " + 156 statusCode + ' ' + response.getStatusMessage()); 157 } 158 } 159 finally { 160 if (mHeadState == 2) { 161 try { 162 response.getInputStream().close(); 163 } 164 catch (IOException e) { 165 } 166 } 167 } 168 } 169 catch (IOException e) { 170 if (mHeadState != 0) { 171 throw e; 172 } 173 } 174 175 if (mHeadState == 0) { 176 Syslog.warn("HEAD request not supported by " + mHostPort + mURI); 177 mHeadState = 2; 178 return exists(timeout); 179 } 180 else { 181 return false; 182 } 183 } 184 185 188 public HttpClient.Response getResponse() throws IOException { 189 return getResponse(mFactory.getDefaultTimeout()); 190 } 191 192 195 public HttpClient.Response getResponse(long timeout) throws IOException { 196 HttpClient client = new HttpClient(mFactory, timeout); 197 client.setURI(mURI); 198 client.setPersistent(true); 199 200 HttpClient.Response response = client.getResponse(); 201 202 int statusCode = response.getStatusCode(); 203 switch (statusCode) { 204 case 200: return response; 206 207 case 404: return null; 209 210 case 301: mURI = response.getHeaders().getString("Location"); 212 client.setURI(mURI); 213 response = client.getResponse(); 214 statusCode = response.getStatusCode(); 215 if (statusCode == 200) { 216 return response; 217 } 218 else { 219 return null; 220 } 221 222 case 302: client.setURI(response.getHeaders().getString("Location")); 224 response = client.getResponse(); 225 statusCode = response.getStatusCode(); 226 if (statusCode == 200) { 227 return response; 228 } 229 else { 230 return null; 231 } 232 } 233 234 throw new IOException 235 ("Response from " + mHostPort + mURI + ": " + 236 statusCode + ' ' + response.getStatusMessage()); 237 } 238 239 private static class HostPort { 240 public final String mHost; 241 public final int mPort; 242 243 public HostPort(String host, int port) { 244 mHost = host; 245 mPort = port < 0 ? 80 : port; 246 } 247 248 public HostPort(URL url) { 249 this(url.getHost(), url.getPort()); 250 } 251 252 public int hashCode() { 253 return mHost.hashCode() + mPort; 254 } 255 256 public boolean equals(Object obj) { 257 if (obj == this) { 258 return true; 259 } 260 261 if (!(obj instanceof HostPort)) { 262 return false; 263 } 264 265 HostPort other = (HostPort)obj; 266 return mPort == other.mPort && mHost.equals(other.mHost); 267 } 268 269 public String toString() { 270 return mHost + ':' + mPort; 271 } 272 } 273 } 274
| Popular Tags
|