KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teaservlet > HttpResource


1 /*
2  * HttpResource.java
3  *
4  * Copyright (c) 2000 Walt Disney Internet Group. All Rights Reserved.
5  *
6  * Original author: Brian S O'Neill
7  *
8  * $Workfile:: HttpResource.java $
9  * $Author:: Briano $
10  * $Revision:: 10 $
11  * $Date:: 01/01/31 4:54p $
12  */

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 /******************************************************************************
24  * Very simple HTTP connection implementation, suitable for use by
25  * HttpContextImpl.
26  *
27  * @author Brian S O'Neill
28  * @version
29  * <!--$$Revision:--> 10 <!-- $-->, <!--$$JustDate:--> 01/01/31 <!-- $-->
30  */

31 class HttpResource {
32     // Default is 10,000 milliseconds.
33
private static long DEFAULT_TIMEOUT = 10000;
34
35     // Maps HostPorts to SocketFactories.
36
private static Map cSocketFactories;
37
38     // Maps URLs to HttpResources.
39
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 JavaDoc[] 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 JavaDoc mURI;
71     private SocketFactory mFactory;
72
73     // 0 = HEAD might be supported
74
// 1 = HEAD is supported
75
// 2 = HEAD is not supported
76
private int mHeadState;
77
78     private HttpResource(HostPort hostPort, String JavaDoc 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             // Default to GET request, so shutdown socket after receiving
101
// response.
102
}
103
104         try {
105             HttpClient.Response response = client.getResponse();
106
107             // Make sure the input is read, but there shouldn't be any.
108
InputStream in = response.getInputStream();
109             while (in.read() >= 0);
110             
111             try {
112                 int statusCode = response.getStatusCode();
113                 switch (statusCode) {
114                 case 200: // OK
115
if (mHeadState == 0) {
116                         mHeadState = 1;
117                     }
118                     return true;
119
120                 case 404: // Not Found
121
if (mHeadState == 0) {
122                         mHeadState = 1;
123                     }
124                     return false;
125
126                 case 301: // Moved Permanently
127
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: // Moved Temporarily
135
if (mHeadState == 0) {
136                         mHeadState = 1;
137                     }
138                     client.setURI(response.getHeaders().getString("Location"));
139                     return client.getResponse().getStatusCode() == 200;
140
141                 case 501: // Not Implemented
142
break;
143
144                 case 503: // Service Unavailable
145
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     /**
186      * @return null if no data
187      */

188     public HttpClient.Response getResponse() throws IOException {
189         return getResponse(mFactory.getDefaultTimeout());
190     }
191
192     /**
193      * @return null if no data
194      */

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: // OK
205
return response;
206
207         case 404: // Not Found
208
return null;
209             
210         case 301: // Moved Permanently
211
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: // Moved Temporarily
223
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 JavaDoc mHost;
241         public final int mPort;
242
243         public HostPort(String JavaDoc 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 JavaDoc 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 JavaDoc toString() {
270             return mHost + ':' + mPort;
271         }
272     }
273 }
274
Popular Tags