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