Annotation of java/classes/org/w3c/www/protocol/http/HttpManager.java, revision 1.82
1.1 abaird 1: // HttpManager.java
1.82 ! ylafon 2: // $Id: HttpManager.java,v 1.81 2003/11/27 12:39:48 ylafon Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
1.38 bmahe 6: package org.w3c.www.protocol.http ;
1.1 abaird 7:
1.65 ylafon 8: import java.util.Enumeration;
9: import java.util.Hashtable;
10: import java.util.Properties;
11:
12: import java.net.URL;
13:
14: import java.io.InputStream;
15: import java.io.PrintStream;
16:
17: import org.w3c.www.mime.MimeHeaderHolder;
18: import org.w3c.www.mime.MimeParser;
19: import org.w3c.www.mime.MimeParserFactory;
20:
21: import org.w3c.util.LRUList;
22: import org.w3c.util.ObservableProperties;
23: import org.w3c.util.PropertyMonitoring;
24: import org.w3c.util.SyncLRUList;
1.1 abaird 25:
1.25 abaird 26: class ManagerDescription {
27: HttpManager manager = null;
28: Properties properties = null;
29:
30: final HttpManager getManager() {
31: return manager;
32: }
33:
34: final boolean sameProperties(Properties props) {
1.54 bmahe 35: if (props.size() != properties.size())
36: return false;
37: Enumeration enum = props.propertyNames();
38: while (enum.hasMoreElements()) {
39: String name = (String) enum.nextElement();
40: String prop = properties.getProperty(name);
41: if ((prop == null) || (! prop.equals(props.getProperty(name))))
42: return false;
43: }
44: return true;
1.25 abaird 45: }
46:
47: ManagerDescription(HttpManager manager, Properties props) {
48: this.manager = manager;
1.54 bmahe 49: this.properties = (Properties)props.clone();
1.25 abaird 50: }
51: }
52:
1.1 abaird 53: class ReplyFactory implements MimeParserFactory {
1.65 ylafon 54:
1.1 abaird 55: public MimeHeaderHolder createHeaderHolder(MimeParser parser) {
56: return new Reply(parser);
57: }
58:
59: }
60:
61: /**
62: * The client side HTTP request manager.
63: * This class is the user interface (along with the other public classes of
64: * this package) for the W3C client side library implementing HTTP.
65: * A typicall request is launched though the following sequence:
66: * <pre>
67: * HttpManager manager = HttpManager.getManager() ;
1.36 ylafon 68: * Request request = manager.createRequest() ;
1.1 abaird 69: * request.setMethod(HTTP.GET) ;
70: * request.setURL(new URL("http://www.w3.org/pub/WWW/"));
71: * Reply reply = manager.runRequest(request) ;
72: * // Get the reply input stream that contains the actual data:
73: * InputStream in = reply.getInputStream() ;
74: * ...
75: * </pre>
76: */
77:
1.13 abaird 78: public class HttpManager implements PropertyMonitoring {
1.45 ylafon 79:
80: private static final boolean debug = false;
81:
1.17 abaird 82: private static final
1.38 bmahe 83: String DEFAULT_SERVER_CLASS = "org.w3c.www.protocol.http.HttpBasicServer";
1.17 abaird 84:
85: /**
86: * The name of the property indicating the class of HttpServer to use.
87: */
88: public static final
1.38 bmahe 89: String SERVER_CLASS_P = "org.w3c.www.protocol.http.server";
1.17 abaird 90:
1.2 abaird 91: /**
92: * The name of the property containing the ProprequestFilter to launch.
93: */
1.12 abaird 94: public static final
1.38 bmahe 95: String FILTERS_PROP_P = "org.w3c.www.protocol.http.filters";
1.5 abaird 96: /**
1.43 ylafon 97: * The maximum number of simultaneous connectionlrus.
1.11 abaird 98: */
99: public static final
1.38 bmahe 100: String CONN_MAX_P = "org.w3c.www.protocol.http.connections.max";
1.11 abaird 101: /**
1.35 ylafon 102: * The SO_TIMEOUT of the client socket.
103: */
104: public static final
1.38 bmahe 105: String TIMEOUT_P = "org.w3c.www.protocol.http.connections.timeout";
1.35 ylafon 106: /**
1.82 ! ylafon 107: * The connection timeout of the client socket.
! 108: */
! 109: public static final
! 110: String CONN_TIMEOUT_P ="org.w3c.www.protocol.http.connections.connTimeout";
! 111: /**
1.5 abaird 112: * Header properties - The allowed drift for getting cached resources.
113: */
114: public static final
1.38 bmahe 115: String MAX_STALE_P = "org.w3c.www.protocol.http.cacheControl.maxStale";
1.5 abaird 116: /**
117: * Header properties - The minium freshness required on cached resources.
118: */
119: public static final
1.38 bmahe 120: String MIN_FRESH_P = "org.w3c.www.protocol.http.cacheControl.minFresh";
1.5 abaird 121: /**
122: * Header properties - Set the only if cached flag on requests.
123: */
124: public static final
1.39 ylafon 125: String ONLY_IF_CACHED_P=
126: "org.w3c.www.protocol.http.cacheControl.onlyIfCached";
1.5 abaird 127: /**
128: * Header properties - Set the user agent.
129: */
130: public static final
1.38 bmahe 131: String USER_AGENT_P = "org.w3c.www.protocol.http.userAgent";
1.5 abaird 132: /**
133: * Header properties - Set the accept header.
134: */
135: public static final
1.38 bmahe 136: String ACCEPT_P = "org.w3c.www.protocol.http.accept";
1.5 abaird 137: /**
138: * Header properties - Set the accept language.
139: */
140: public static final
1.38 bmahe 141: String ACCEPT_LANGUAGE_P = "org.w3c.www.protocol.http.acceptLanguage";
1.5 abaird 142: /**
143: * Header properties - Set the accept encodings.
144: */
145: public static final
1.38 bmahe 146: String ACCEPT_ENCODING_P = "org.w3c.www.protocol.http.acceptEncoding";
1.5 abaird 147: /**
1.62 ylafon 148: * Header properties - are we parsing answers in a lenient way?
149: */
150: public static final
151: String LENIENT_P = "org.w3c.www.protocol.http.lenient";
152: /**
1.79 ylafon 153: * Header properties - should we reuse a connection for POST?
154: */
155: public static final
156: String KEEPBODY_P = "org.w3c.www.protocol.http.keepbody";
157: /**
1.5 abaird 158: * Header properties - Should we use a proxy ?
159: */
160: public static final
1.12 abaird 161: String PROXY_SET_P = "proxySet";
1.5 abaird 162: /**
163: * Header properties - What is the proxy host name.
164: */
165: public static final
1.12 abaird 166: String PROXY_HOST_P = "proxyHost";
1.5 abaird 167: /**
168: * Header properties - What is the proxy port number.
169: */
170: public static final
1.12 abaird 171: String PROXY_PORT_P = "proxyPort";
1.2 abaird 172:
1.7 abaird 173: /**
174: * The default value for the <code>Accept</code> header.
175: */
176: public static final
177: String DEFAULT_ACCEPT = "*/*";
178: /**
179: * The default value for the <code>User-Agent</code> header.
180: */
181: public static final
1.81 ylafon 182: String DEFAULT_USER_AGENT = "Jigsaw/2.2.3";
1.7 abaird 183:
1.24 abaird 184: /**
185: * This array keeps track of all the created managers.
186: * A new manager (kind of HTTP client side context) is created for each
187: * diffferent set of properties.
188: */
1.25 abaird 189: private static ManagerDescription managers[] = new ManagerDescription[4];
1.65 ylafon 190:
1.1 abaird 191: /**
1.17 abaird 192: * The class to instantiate to create new HttpServer instances.
193: */
194: protected Class serverclass = null;
195: /**
1.13 abaird 196: * The properties we initialized from.
197: */
198: ObservableProperties props = null;
199: /**
1.1 abaird 200: * The server this manager knows about, indexed by FQDN of target servers.
201: */
202: protected Hashtable servers = null;
203: /**
204: * The template request (the request we will clone to create new requests)
205: */
1.4 abaird 206: protected Request template = null ;
207: /**
1.9 abaird 208: * The LRU list of connections.
1.4 abaird 209: */
1.9 abaird 210: protected LRUList connectionsLru = null;
1.1 abaird 211: /**
212: * The filter engine attached to this manager.
213: */
214: FilterEngine filteng = null;
215:
1.35 ylafon 216: protected int timeout = 300000;
1.82 ! ylafon 217: protected int conn_timeout = 1000;
1.9 abaird 218: protected int conn_count = 0;
219: protected int conn_max = 5;
1.62 ylafon 220: protected boolean lenient = true;
1.79 ylafon 221: protected boolean keepbody = false;
1.9 abaird 222:
1.1 abaird 223: /**
1.13 abaird 224: * Update the proxy configuration to match current properties setting.
225: * @return A boolean, <strong>true</strong> if change was done,
226: * <strong>false</strong> otherwise.
227: */
228:
229: protected boolean updateProxy() {
230: boolean set = props.getBoolean(PROXY_SET_P, false);
231: if ( set ) {
232: // Wow using a proxy now !
233: String host = props.getString(PROXY_HOST_P, null);
234: int port = props.getInteger(PROXY_PORT_P, -1);
235: URL proxy = null;
236: try {
237: proxy = new URL("http", host, port, "/");
238: } catch (Exception ex) {
239: return false;
240: }
241: // Now if a proxy...
1.55 bmahe 242: if (( proxy != null ) && (proxy.getHost() != null))
1.24 abaird 243: template.setProxy(proxy);
1.13 abaird 244: } else {
1.24 abaird 245: template.setProxy(null);
1.13 abaird 246: }
247: return true;
248: }
249:
250: /**
1.24 abaird 251: * Get this manager properties.
252: * @return An ObservableProperties instance.
253: */
254:
255: public final ObservableProperties getProperties() {
256: return props;
257: }
258:
259: /**
1.13 abaird 260: * PropertyMonitoring implementation - Update properties on the fly !
261: * @param name The name of the property that has changed.
262: * @return A boolean, <strong>true</strong> if change is accepted,
263: * <strong>false</strong> otherwise.
264: */
265:
266: public boolean propertyChanged(String name) {
1.24 abaird 267: Request tpl = template;
1.13 abaird 268: if ( name.equals(FILTERS_PROP_P) ) {
1.37 ylafon 269: // FIXME
270: return true;
271: // return false;
1.35 ylafon 272: } else if ( name.equals(TIMEOUT_P) ) {
273: setTimeout(props.getInteger(TIMEOUT_P, timeout));
274: return true;
1.82 ! ylafon 275: } else if ( name.equals(CONN_TIMEOUT_P) ) {
! 276: setConnTimeout(props.getInteger(CONN_TIMEOUT_P, conn_timeout));
! 277: return true;
1.13 abaird 278: } else if ( name.equals(CONN_MAX_P) ) {
279: setMaxConnections(props.getInteger(CONN_MAX_P, conn_max));
280: return true;
281: } else if ( name.equals(MAX_STALE_P) ) {
282: int ival = props.getInteger(MAX_STALE_P, -1);
283: if ( ival >= 0 )
284: tpl.setMaxStale(ival);
285: return true;
286: } else if ( name.equals(MIN_FRESH_P) ) {
287: int ival = props.getInteger(MIN_FRESH_P, -1);
288: if ( ival >= 0 )
289: tpl.setMinFresh(ival);
290: return true;
1.62 ylafon 291: } else if ( name.equals(LENIENT_P) ) {
1.78 ylafon 292: lenient = props.getBoolean(LENIENT_P, lenient);
1.62 ylafon 293: return true;
1.79 ylafon 294: } else if ( name.equals(KEEPBODY_P) ) {
295: keepbody = props.getBoolean(KEEPBODY_P, keepbody);
296: return true;
1.62 ylafon 297: } if ( name.equals(ONLY_IF_CACHED_P) ) {
1.13 abaird 298: tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
299: return true;
300: } else if ( name.equals(USER_AGENT_P) ) {
301: tpl.setValue("user-agent"
302: , props.getString(USER_AGENT_P
303: , DEFAULT_USER_AGENT));
304: return true;
305: } else if ( name.equals(ACCEPT_P) ) {
306: tpl.setValue("accept"
307: , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
308: return true;
309: } else if ( name.equals(ACCEPT_LANGUAGE_P) ) {
310: String sval = props.getString(ACCEPT_LANGUAGE_P, null);
311: if ( sval != null )
312: tpl.setValue("accept-language", sval);
313: return true;
314: } else if ( name.equals(ACCEPT_ENCODING_P) ) {
315: String sval = props.getString(ACCEPT_ENCODING_P, null);
316: if ( sval != null )
317: tpl.setValue("accept-encoding", sval);
318: return true;
319: } else if ( name.equals(PROXY_SET_P)
320: || name.equals(PROXY_HOST_P)
321: || name.equals(PROXY_PORT_P) ) {
322: return updateProxy();
323: } else {
324: return true;
325: }
326: }
327:
328: /**
1.4 abaird 329: * Allow the manager to interact with the user if needed.
330: * This will, for example, allow prompting for paswords, etc.
331: * @param onoff Turn interaction on or off.
332: */
333:
334: public void setAllowUserInteraction(boolean onoff) {
335: template.setAllowUserInteraction(onoff);
336: }
337:
1.66 bmahe 338: protected static synchronized
339: HttpManager getManager(Class managerclass, Properties p)
340: {
1.24 abaird 341: // Does such a manager exists already ?
342: for (int i = 0 ; i < managers.length ; i++) {
343: if ( managers[i] == null )
344: continue;
1.54 bmahe 345: if ( managers[i].sameProperties(p) )
1.25 abaird 346: return managers[i].getManager();
1.24 abaird 347: }
348: // Get the props we will initialize from:
1.28 abaird 349: ObservableProperties props = null;
1.24 abaird 350: if ( p instanceof ObservableProperties )
1.28 abaird 351: props = (ObservableProperties) p;
1.24 abaird 352: else
1.28 abaird 353: props = new ObservableProperties(p);
354: // Create a new manager for this set of properties:
1.66 bmahe 355: HttpManager manager = null;;
356: try {
357: Object o = managerclass.newInstance();
358: if (o instanceof HttpManager) {
359: manager = (HttpManager) o;
360: } else { // default value
361: manager = new HttpManager();
362: }
363: } catch (Exception ex) {
364: ex.printStackTrace();
365: manager = new HttpManager();
366: }
1.28 abaird 367: manager.props = props;
1.24 abaird 368: // Initialize this new manager filters:
369: String filters[] = props.getStringArray(FILTERS_PROP_P, null);
370: if ( filters != null ) {
371: for (int i = 0 ; i < filters.length ; i++) {
372: try {
373: Class c = Class.forName(filters[i]);
374: PropRequestFilter f = null;
375: f = (PropRequestFilter) c.newInstance();
376: f.initialize(manager);
377: } catch (PropRequestFilterException ex) {
378: System.out.println("Couldn't initialize filter \""
379: + filters[i]
380: + "\" init failed: "
381: + ex.getMessage());
382: } catch (Exception ex) {
383: System.err.println("Error initializing prop filters:");
384: System.err.println("Coulnd't initialize ["
385: + filters[i]
386: + "]: " + ex.getMessage());
387: ex.printStackTrace();
388: System.exit(1);
1.2 abaird 389: }
390: }
1.24 abaird 391: }
392: // The factory to create MIME reply holders:
1.66 bmahe 393: manager.factory = manager.getReplyFactory();
1.24 abaird 394: // The class to create HttpServer instances from
395: String c = props.getString(SERVER_CLASS_P, DEFAULT_SERVER_CLASS);
396: try {
1.34 bmahe 397: manager.serverclass = Class.forName(c);
1.24 abaird 398: } catch (Exception ex) {
399: System.err.println("Unable to initialize HttpManager: ");
400: System.err.println("Class \""+c+"\" not found, from property "
401: + SERVER_CLASS_P);
402: ex.printStackTrace();
403: System.exit(1);
404: }
405: // Setup the template request:
406: Request tpl = manager.template;
407: // Set some default headers value (from props)
408: // Check for a proxy ?
409: manager.updateProxy();
410: // CacheControl, only-if-cached
411: tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
412: // CacheControl, maxstale
413: int ival = props.getInteger(MAX_STALE_P, -1);
414: if ( ival >= 0 )
415: tpl.setMaxStale(ival);
416: // CacheControl, minfresh:
417: ival = props.getInteger(MIN_FRESH_P, -1);
418: if ( ival >= 0 )
419: tpl.setMinFresh(ival);
1.62 ylafon 420: // general, lenient
421: manager.lenient = props.getBoolean(LENIENT_P, true);
1.79 ylafon 422: manager.keepbody = props.getBoolean(KEEPBODY_P, false);
1.24 abaird 423: // General, User agent
1.68 ylafon 424: String sval;
1.24 abaird 425: tpl.setValue("user-agent"
426: , props.getString(USER_AGENT_P
427: , DEFAULT_USER_AGENT));
428: // General, Accept
429: tpl.setValue("accept"
430: , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
431: // General, Accept-Language
1.68 ylafon 432: sval = props.getString(ACCEPT_LANGUAGE_P, null);
1.67 ylafon 433: if ( sval != null ) {
434: if (sval.trim().length() > 0) {
435: tpl.setValue("accept-language", sval);
436: }
437: }
1.24 abaird 438: // General, Accept-Encoding
439: sval = props.getString(ACCEPT_ENCODING_P, null);
1.68 ylafon 440: if ( sval != null ) {
441: if (sval.trim().length() > 0) {
442: tpl.setValue("accept-encoding", sval);
443: }
444: }
1.24 abaird 445: // Maximum number of allowed connections:
446: manager.conn_max = props.getInteger(CONN_MAX_P, 5);
1.58 ylafon 447: // timeout value
1.59 ylafon 448: manager.timeout = props.getInteger(TIMEOUT_P, manager.timeout);
1.82 ! ylafon 449: // connection timeout
! 450: manager.conn_timeout = props.getInteger(CONN_TIMEOUT_P,
! 451: manager.conn_timeout);
1.24 abaird 452: // Register ourself as a property observer:
453: props.registerObserver(manager);
454: // Register that manager in our knwon managers:
455: for (int i = 0 ; i < managers.length ; i++) {
456: if ( managers[i] == null ) {
1.25 abaird 457: managers[i] = new ManagerDescription(manager, p);
1.24 abaird 458: return manager;
1.17 abaird 459: }
1.1 abaird 460: }
1.25 abaird 461: ManagerDescription nm[] = new ManagerDescription[managers.length << 1];
462: System.arraycopy(managers, 0, nm, 0, managers.length);
463: nm[managers.length] = new ManagerDescription(manager, p);
464: managers = nm;
1.1 abaird 465: return manager;
466: }
1.66 bmahe 467:
468:
469: /**
470: * Get an instance of the HTTP manager.
471: * This method returns an actual instance of the HTTP manager. It may
472: * return different managers, if it decides to distribute the load on
473: * different managers (avoid the HttpManager being a bottleneck).
474: * @return An application wide instance of the HTTP manager.
475: */
476:
477: public static synchronized HttpManager getManager(Properties p) {
478: return getManager(HttpManager.class, p);
479: }
1.1 abaird 480:
1.24 abaird 481: public static HttpManager getManager() {
482: return getManager(System.getProperties());
483: }
1.1 abaird 484:
485: /**
1.32 abaird 486: * Get the String key for the server instance handling that request.
487: * This method takes care of any proxy setting (it will return the key
488: * to the proxy when required.)
489: * @return A uniq identifier for the handling server, as a String.
490: */
491:
492: public final String getServerKey(Request request) {
493: URL proxy = request.getProxy();
494: URL target = request.getURL();
495: String key = null;
496: if ( proxy != null ) {
497: return ((proxy.getPort() == 80)
498: ? proxy.getHost().toLowerCase()
499: : (proxy.getHost().toLowerCase()+":"+proxy.getPort()));
500: } else {
501: return ((target.getPort() == 80)
502: ? target.getHost().toLowerCase()
503: : (target.getHost().toLowerCase()+":"+target.getPort()));
504: }
505: }
506:
507: /**
1.1 abaird 508: * Get the appropriate server object for handling request to given target.
1.32 abaird 509: * @param key The server's key, as returned by <code>getServerKey</code>.
1.1 abaird 510: * @return An object complying to the HttpServer interface.
511: * @exception HttpException If the given host name couldn't be resolved.
512: */
513:
1.76 ylafon 514: protected HttpServer lookupServer(String host, int port)
1.1 abaird 515: throws HttpException
516: {
1.5 abaird 517: int p = (port == -1) ? 80 : port;
1.32 abaird 518: String id = ((p == 80)
519: ? host.toLowerCase()
520: : (host.toLowerCase() +":"+p));
1.1 abaird 521: // Check for an existing server:
522: HttpServer server = (HttpServer) servers.get(id);
523: if ( server != null )
524: return server;
525: // Create and register a new server:
1.17 abaird 526: try {
527: server = (HttpServer) serverclass.newInstance();
528: } catch (Exception ex) {
529: String msg = ("Unable to create an instance of \""
530: + serverclass.getName()
531: + "\", invalid config, check the "
532: + SERVER_CLASS_P + " property.");
1.21 abaird 533: throw new HttpException(ex, msg);
1.17 abaird 534: }
1.35 ylafon 535: server.initialize(this, new HttpServerState(server), host, p, timeout);
1.71 ylafon 536: // FIXME for long running servers, keeping the hastable growing is
537: // a potential leak. This is a hard way of taking care of that
1.72 ylafon 538: if (servers.size() > conn_max) {
1.71 ylafon 539: closeAnyConnection();
1.73 ylafon 540: servers = new Hashtable(conn_max);
1.71 ylafon 541: }
1.1 abaird 542: servers.put(id, server);
543: return server;
544: }
1.5 abaird 545:
1.1 abaird 546: /**
1.60 ylafon 547: * The given connection is about to be used.
548: * Update our list of available servers.
549: * @param conn The idle connection.
550: */
551:
552: public void notifyUse(HttpConnection conn) {
553: if (debug)
554: System.out.println("+++ connection used");
555: connectionsLru.remove(conn);
556: }
557:
558: /**
1.9 abaird 559: * The given connection can be reused, but is now idle.
560: * @param conn The connection that is now idle.
1.4 abaird 561: */
562:
1.60 ylafon 563: public synchronized void notifyIdle(HttpConnection conn) {
1.45 ylafon 564: if (debug)
565: System.out.println("+++ connection idle");
1.9 abaird 566: connectionsLru.toHead(conn);
1.43 ylafon 567: HttpServerState ss = conn.getServer().getState();
568: ss.registerConnection(conn);
1.63 ylafon 569: notifyAll();
1.4 abaird 570: }
571:
572: /**
1.9 abaird 573: * The given connection has just been created.
574: * @param conn The newly created connection.
575: */
576:
577: protected synchronized void notifyConnection(HttpConnection conn) {
1.45 ylafon 578: if (debug)
579: System.out.println("+++ notify conn_count " + (conn_count+1)
580: + " / " + conn_max);
1.9 abaird 581: if ( ++conn_count > conn_max )
582: closeAnyConnection();
583: }
584:
585: /**
586: * The given connection has been deleted.
587: * @param conn The deleted connection.
588: */
589:
1.60 ylafon 590: protected void deleteConnection(HttpConnection conn) {
1.9 abaird 591: HttpServerState ss = conn.getServer().getState();
592: ss.deleteConnection(conn);
1.60 ylafon 593: synchronized(this) {
594: --conn_count;
595: if (debug)
596: System.out.println("+++ delete conn_count: " + conn_count);
1.63 ylafon 597: notifyAll();
1.60 ylafon 598: }
1.9 abaird 599: }
600:
601: protected synchronized boolean tooManyConnections() {
1.51 ylafon 602: return conn_count >= conn_max;
1.9 abaird 603: }
604:
605: /**
606: * Try reusing one of the idle connection of that server, if any.
607: * @param server The target server.
608: * @return An currently idle connection to the given server.
1.4 abaird 609: */
610:
1.60 ylafon 611: protected HttpConnection getConnection(HttpServer server) {
1.9 abaird 612: HttpServerState ss = server.getState();
1.60 ylafon 613: return ss.getConnection();
1.9 abaird 614: }
615:
1.47 ylafon 616: /**
617: * Wait for a connection to come up.
618: * @param server, the target server.
1.48 bmahe 619: * @exception InterruptedException If interrupted..
1.47 ylafon 620: */
621:
1.9 abaird 622: protected synchronized void waitForConnection(HttpServer server)
623: throws InterruptedException
624: {
1.74 ylafon 625: wait(30000); // FIXME should be tunable, now set to 30s
1.4 abaird 626: }
627:
628: /**
1.60 ylafon 629: * Close some connections, but pickling the least recently used ones.
1.45 ylafon 630: * One third of the max number of connection is cut. This is done to
631: * eliminate the old connections that should be broken already.
632: * (no Socket.isAlive());
1.9 abaird 633: * @return A boolean, <strong>true</strong> if a connection was closed
634: * <strong>false</strong> otherwise.
1.4 abaird 635: */
636:
1.75 ylafon 637: protected boolean closeAnyConnection() {
1.45 ylafon 638: boolean saved = false;
1.47 ylafon 639: int max = Math.max(conn_max/3, 1);
1.45 ylafon 640: for (int i=0; i < max; i++) {
641: HttpConnection conn = (HttpConnection) connectionsLru.removeTail();
642: if ( conn != null ) {
643: conn.close();
644: deleteConnection(conn);
645: if (debug)
646: System.out.println("+++ close request");
1.60 ylafon 647: saved = true;
1.47 ylafon 648: } else
649: break;
1.9 abaird 650: }
1.45 ylafon 651: return saved;
1.4 abaird 652: }
653:
654: /**
1.1 abaird 655: * One of our server handler wants to open a connection.
656: * @param block A boolean indicating whether we should block the calling
657: * thread until a token is available (otherwise, the method will just
658: * peek at the connection count, and return the appropriate result).
659: * @return A boolean, <strong>true</strong> if the connection can be
660: * opened straight, <strong>false</strong> otherwise.
661: */
662:
1.9 abaird 663: protected boolean negotiateConnection(HttpServer server) {
664: HttpServerState ss = server.getState();
1.10 abaird 665: if ( ! tooManyConnections() ) {
1.4 abaird 666: return true;
1.10 abaird 667: } else if ( ss.notEnoughConnections() ) {
668: return closeAnyConnection();
1.9 abaird 669: } else if ( servers.size() > conn_max ) {
670: return closeAnyConnection();
1.4 abaird 671: }
1.9 abaird 672: return false;
1.60 ylafon 673: }
674:
675: /**
676: * A new client connection has been established.
677: * This method will try to maintain a maximum number of established
678: * connections, by closing idle connections when possible.
679: * @param server The server that has established a new connection.
680: */
681:
682: protected final synchronized void incrConnCount(HttpServer server) {
683: if ( ++conn_count > conn_max )
684: closeAnyConnection();
685: if (debug)
686: System.out.println("+++ incr conn_count: " + conn_count);
687: }
688:
689: /**
690: * Decrement the number of established connections.
691: * @param server The server that has closed one connection to its target.
692: */
693:
694: protected final synchronized void decrConnCount(HttpServer server) {
695: --conn_count;
696: if (debug)
697: System.out.println("+++ decr conn_count: " + conn_count);
1.1 abaird 698: }
699:
700: /**
701: * Run the given request, in synchronous mode.
702: * This method will launch the given request, and block the calling thread
703: * until the response headers are available.
704: * @param request The request to run.
705: * @return An instance of Reply, containing all the reply
706: * informations.
1.42 bmahe 707: * @exception HttpException If something failed during request processing.
1.1 abaird 708: */
1.65 ylafon 709:
1.1 abaird 710: public Reply runRequest(Request request)
711: throws HttpException
712: {
1.19 abaird 713: Reply reply = null;
714: int fcalls = 0;
1.1 abaird 715: // Now run through the ingoing filters:
716: RequestFilter filters[] = filteng.run(request);
717: if ( filters != null ) {
718: for (int i = 0 ; i < filters.length ; i++) {
1.19 abaird 719: if ((reply = filters[fcalls].ingoingFilter(request)) != null)
720: break;
721: fcalls++;
1.1 abaird 722: }
723: }
1.16 abaird 724: // Locate the appropriate target server:
1.31 abaird 725: URL target = request.getURL();
1.19 abaird 726: if ( reply == null ) {
1.32 abaird 727: HttpServer srv = null;
1.22 ylafon 728: boolean rtry ;
1.21 abaird 729: do {
1.22 ylafon 730: rtry = false;
1.21 abaird 731: try {
1.32 abaird 732: URL proxy = request.getProxy();
733: if ( proxy != null )
734: srv = lookupServer(proxy.getHost(), proxy.getPort());
735: else
736: srv = lookupServer(target.getHost(), target.getPort());
1.30 abaird 737: request.setServer(srv);
1.21 abaird 738: reply = srv.runRequest(request);
739: } catch (HttpException ex) {
740: for (int i = 0; i < fcalls; i++)
741: rtry = rtry || filters[i].exceptionFilter(request, ex);
1.23 abaird 742: if ( ! rtry )
1.22 ylafon 743: throw ex;
1.30 abaird 744: } finally {
745: request.unsetServer();
1.21 abaird 746: }
747: } while (rtry);
1.16 abaird 748: }
1.1 abaird 749: // Apply the filters on the way back:
750: if ( filters != null ) {
1.19 abaird 751: while (--fcalls >= 0) {
752: Reply frep = filters[fcalls].outgoingFilter(request, reply);
753: if ( frep != null ) {
754: reply = frep;
755: break;
756: }
1.3 abaird 757: }
1.1 abaird 758: }
759: return reply;
760: }
761:
762: /**
763: * Get this manager's reply factory.
764: * The Reply factory is used when prsing incomming reply from servers, it
765: * decides what object will be created to hold the actual reply from the
766: * server.
767: * @return An object compatible with the MimeParserFactory interface.
768: */
769:
770: MimeParserFactory factory = null ;
771:
1.9 abaird 772: public MimeParserFactory getReplyFactory() {
1.66 bmahe 773: if (factory == null) {
774: factory = new ReplyFactory();
775: }
1.1 abaird 776: return factory;
777: }
778:
779: /**
780: * Add a new request filter.
781: * Request filters are called <em>before</em> a request is launched, and
782: * <em>after</em> the reply headers are available. They allow applications
783: * to setup specific request headers (such as PICS, or PEP stuff) on the
784: * way in, and check the reply on the way out.
785: * <p>Request filters are application wide: if their scope matches
786: * the current request, then they will always be aplied.
787: * <p>Filter scopes are defined inclusively and exclusively
1.15 abaird 788: * @param incs The URL domains for which the filter should be triggered.
789: * @param exs The URL domains for which the filter should not be triggered.
1.1 abaird 790: * @param filter The request filter to add.
791: */
792:
793: public void setFilter(URL incs[], URL exs[], RequestFilter filter) {
794: if ( incs != null ) {
795: for (int i = 0 ; i < incs.length ; i++)
796: filteng.setFilter(incs[i], true, filter);
797: }
798: if ( exs != null ) {
799: for (int i = 0 ; i < exs.length ; i++)
800: filteng.setFilter(exs[i], false, filter);
801: }
802: return;
803: }
804:
1.15 abaird 805: /**
806: * Add a global filter.
807: * The given filter will <em>always</em> be invoked.
808: * @param filter The filter to install.
809: */
1.65 ylafon 810:
1.1 abaird 811: public void setFilter(RequestFilter filter) {
812: filteng.setFilter(filter);
813: }
814:
815: /**
1.15 abaird 816: * Find back an instance of a global filter.
817: * This methods allow external classes to get a pointer to installed
818: * filters of a given class.
819: * @param cls The class of the filter to look for.
820: * @return A RequestFilter instance, or <strong>null</strong> if not
821: * found.
1.1 abaird 822: */
823:
1.15 abaird 824: public RequestFilter getGlobalFilter(Class cls) {
825: return filteng.getGlobalFilter(cls);
1.1 abaird 826: }
827:
828: /**
829: * Create a new default outgoing request.
830: * This method should <em>always</em> be used to create outgoing requests.
831: * It will initialize the request with appropriate default values for
832: * the various headers, and make sure that the request is enhanced by
833: * the registered request filters.
834: * @return An instance of Request, suitable to be launched.
835: */
836:
837: public Request createRequest() {
838: return (Request) template.getClone() ;
839: }
840:
841: /**
842: * Global settings - Set the max number of allowed connections.
843: * Set the maximum number of simultaneous connections that can remain
844: * opened. The manager will take care of queuing requests if this number
845: * is reached.
846: * <p>This value defaults to the value of the
1.82 ! ylafon 847: * <code>org.w3c.www.http.connections.max</code> property.
1.1 abaird 848: * @param max_conn The allowed maximum simultaneous open connections.
849: */
850:
1.13 abaird 851: public synchronized void setMaxConnections(int max_conn) {
852: this.conn_max = max_conn;
1.35 ylafon 853: }
854:
855: /**
856: * Global settings - Set the timeout on the socket
857: *
858: * <p>This value defaults to the value of the
1.82 ! ylafon 859: * <code>org.w3c.www.http.connections.timeout</code> property.
1.35 ylafon 860: * @param timeout The allowed maximum microsecond before a timeout.
861: */
862:
863: public synchronized void setTimeout(int timeout) {
864: this.timeout = timeout;
865: Enumeration e = servers.elements();
866: while (e.hasMoreElements()) {
867: ((HttpServer) e.nextElement()).setTimeout(timeout);
868: }
1.62 ylafon 869: }
870:
871: /**
1.82 ! ylafon 872: * Global settings - Set the connection timeout for the socket
! 873: *
! 874: * <p>This value defaults to the value of the
! 875: * <code>org.w3c.www.protocol.http.connections.connTimeout</code> property
! 876: * @param timeout The allowed maximum microsecond before a timeout.
! 877: */
! 878:
! 879: public synchronized void setConnTimeout(int conn_timeout) {
! 880: this.conn_timeout = conn_timeout;
! 881: Enumeration e = servers.elements();
! 882: while (e.hasMoreElements()) {
! 883: ((HttpServer) e.nextElement()).setConnTimeout(conn_timeout);
! 884: }
! 885: }
! 886:
! 887: /**
1.62 ylafon 888: * Global settings - set the HTTP parsing lenient or not.
889: * @param lenient, true by default, false to detect wrong servers
890: */
891: public void setLenient(boolean lenient) {
892: this.lenient = lenient;
893: }
894:
895: /**
896: * Is this manager parsing headers in a lenient way?
897: * @return A boolean.
898: */
899: public boolean isLenient() {
900: return lenient;
1.1 abaird 901: }
902:
903: /**
904: * Global settings - Set an optional proxy to use.
905: * Set the proxy to which all requests should be targeted. If the
1.38 bmahe 906: * <code>org.w3c.www.http.proxy</code> property is defined, it will be
1.1 abaird 907: * used as the default value.
908: * @param proxy The URL for the proxy to use.
909: */
910:
911: public void setProxy(URL proxy) {
1.5 abaird 912: template.setProxy(proxy);
1.30 abaird 913: }
914:
915: /**
916: * Does this manager uses a proxy to fulfill requests ?
917: * @return A boolean.
918: */
919:
920: public boolean usingProxy() {
921: return template.hasProxy();
1.1 abaird 922: }
923:
924: /**
925: * Global settings - Set the request timeout.
926: * Once a request has been emited, the HttpManager will sit for this
927: * given number of milliseconds before the request is declared to have
928: * timed-out.
929: * <p>This timeout value defaults to the value of the
1.38 bmahe 930: * <code>org.w3c.www.http.requestTimeout</code> property value.
1.1 abaird 931: * @param ms The timeout value in milliseconds.
932: */
933:
934: public void setRequestTimeout(int ms) {
935: }
936:
937: /**
938: * Global settings - Define a global request header.
939: * Set a default value for some request header. Once defined, the
940: * header will automatically be defined on <em>all</em> outgoing requests
941: * created through the <code>createRequest</code> request.
942: * @param name The name of the header, case insensitive.
943: * @param value It's default value.
944: */
1.65 ylafon 945:
1.1 abaird 946: public void setGlobalHeader(String name, String value) {
947: template.setValue(name, value);
948: }
949:
1.18 abaird 950: /**
951: * Global settings - Get a global request header default value.
952: * @param name The name of the header to get.
953: * @return The value for that header, as a String, or <strong>
954: * null</strong> if undefined.
955: */
956:
1.1 abaird 957: public String getGlobalHeader(String name) {
958: return template.getValue(name);
1.18 abaird 959: }
960:
1.65 ylafon 961:
1.18 abaird 962: /**
963: * Dump all in-memory cached state to persistent storage.
964: */
965:
966: public void sync() {
967: filteng.sync();
1.1 abaird 968: }
969:
970: /**
971: * Create a new HttpManager.
1.33 abaird 972: * FIXME Making this method protected breaks the static method
973: * to create HttpManager instances (should use a factory here)
1.1 abaird 974: * @param props The properties from which the manager should initialize
975: * itself, or <strong>null</strong> if none are available.
976: */
977:
1.33 abaird 978: protected HttpManager() {
1.9 abaird 979: this.template = new Request(this);
980: this.servers = new Hashtable();
981: this.filteng = new FilterEngine();
982: this.connectionsLru = new SyncLRUList();
1.1 abaird 983: }
984:
985: /**
986: * DEBUGGING !
987: */
988:
989: public static void main(String args[]) {
990: try {
991: // Get the manager, and define some global headers:
992: HttpManager manager = HttpManager.getManager();
1.80 ylafon 993: manager.setGlobalHeader("User-Agent", "Jigsaw/2.2.2");
1.1 abaird 994: manager.setGlobalHeader("Accept", "*/*;q=1.0");
995: manager.setGlobalHeader("Accept-Encoding", "gzip");
1.34 bmahe 996: PropRequestFilter filter =
1.38 bmahe 997: new org.w3c.www.protocol.http.cookies.CookieFilter();
1.34 bmahe 998: filter.initialize(manager);
1.45 ylafon 999: PropRequestFilter pdebug =
1.38 bmahe 1000: new org.w3c.www.protocol.http.DebugFilter();
1.45 ylafon 1001: pdebug.initialize(manager);
1.1 abaird 1002: Request request = manager.createRequest();
1003: request.setURL(new URL(args[0]));
1004: request.setMethod("GET");
1005: Reply reply = manager.runRequest(request);
1.34 bmahe 1006: //Display some infos:
1.1 abaird 1007: System.out.println("last-modified: "+reply.getLastModified());
1008: System.out.println("length : "+reply.getContentLength());
1009: // Display the returned body:
1010: InputStream in = reply.getInputStream();
1011: byte buf[] = new byte[4096];
1012: int cnt = 0;
1013: while ((cnt = in.read(buf)) > 0)
1.56 ylafon 1014: System.out.print(new String(buf, 0, cnt));
1.1 abaird 1015: System.out.println("-");
1016: in.close();
1.34 bmahe 1017: manager.sync();
1.1 abaird 1018: } catch (Exception ex) {
1019: ex.printStackTrace();
1.80 ylafon 1020: if (ex instanceof HttpException) {
1021: ((HttpException) ex).getException().printStackTrace();
1022: }
1.1 abaird 1023: }
1024: System.exit(1);
1025: }
1026: }
1.34 bmahe 1027:
1028:
Webmaster