Annotation of java/classes/org/w3c/www/protocol/http/HttpManager.java, revision 1.13
1.1 abaird 1: // HttpManager.java
1.13 ! abaird 2: // $Id: HttpManager.java,v 1.12 1996/09/23 14:01:44 abaird Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package w3c.www.protocol.http ;
7:
8: import java.util.*;
9: import java.net.*;
10: import java.io.*; // FIXME - DEBUG
11:
12: import w3c.www.mime.*;
1.4 abaird 13: import w3c.util.*;
1.1 abaird 14:
15: class ReplyFactory implements MimeParserFactory {
16:
17: public MimeHeaderHolder createHeaderHolder(MimeParser parser) {
18: return new Reply(parser);
19: }
20:
21: }
22:
1.9 abaird 23: class HttpServerState {
24: HttpServer server = null;
25: Vector conns = null;
26:
27: final HttpServer getServer() {
28: return server;
29: }
30:
31: synchronized boolean notEnoughConnections() {
32: return (conns == null) || (conns.size() == 1);
33: }
34:
35: void registerConnection(HttpConnection conn) {
36: if ( conns == null )
37: conns = new Vector(4);
38: conns.addElement(conn);
39: }
40:
41: void unregisterConnection(HttpConnection conn) {
42: if ( conns != null )
43: conns.removeElement(conn);
44: }
45:
46: void deleteConnection(HttpConnection conn) {
47: // conn.close();
48: if ( conns != null )
49: conns.removeElement(conn);
50: }
51:
52: synchronized HttpConnection getConnection() {
53: if ((conns != null) && (conns.size() > 0)) {
54: HttpConnection conn = (HttpConnection) conns.elementAt(0);
55: conns.removeElementAt(0);
56: return conn;
57: }
58: return null;
59: }
60:
61: HttpServerState(HttpServer server) {
62: this.server = server;
63: }
64: }
1.1 abaird 65:
66: /**
67: * The client side HTTP request manager.
68: * This class is the user interface (along with the other public classes of
69: * this package) for the W3C client side library implementing HTTP.
70: * A typicall request is launched though the following sequence:
71: * <pre>
72: * HttpManager manager = HttpManager.getManager() ;
73: * Request request = manager.makeRequest() ;
74: * request.setMethod(HTTP.GET) ;
75: * request.setURL(new URL("http://www.w3.org/pub/WWW/"));
76: * Reply reply = manager.runRequest(request) ;
77: * // Get the reply input stream that contains the actual data:
78: * InputStream in = reply.getInputStream() ;
79: * ...
80: * </pre>
81: */
82:
1.13 ! abaird 83: public class HttpManager implements PropertyMonitoring {
1.4 abaird 84:
1.2 abaird 85: /**
86: * The name of the property containing the ProprequestFilter to launch.
87: */
1.12 abaird 88: public static final
89: String FILTERS_PROP_P = "w3c.www.protocol.http.filters";
1.5 abaird 90: /**
1.11 abaird 91: * The maximum number of simultaneous connections.
92: */
93: public static final
1.12 abaird 94: String CONN_MAX_P = "w3c.www.protocol.http.connections.max";
1.11 abaird 95: /**
1.5 abaird 96: * Header properties - The allowed drift for getting cached resources.
97: */
98: public static final
1.12 abaird 99: String MAX_STALE_P = "w3c.www.protocol.http.cacheControl.maxStale";
1.5 abaird 100: /**
101: * Header properties - The minium freshness required on cached resources.
102: */
103: public static final
1.12 abaird 104: String MIN_FRESH_P = "w3c.www.protocol.http.cacheControl.minFresh";
1.5 abaird 105: /**
106: * Header properties - Set the only if cached flag on requests.
107: */
108: public static final
1.12 abaird 109: String ONLY_IF_CACHED_P="w3c.www.protocol.http.cacheControl.onlyIfCached";
1.5 abaird 110: /**
111: * Header properties - Set the user agent.
112: */
113: public static final
1.12 abaird 114: String USER_AGENT_P = "w3c.www.protocol.http.userAgent";
1.5 abaird 115: /**
116: * Header properties - Set the accept header.
117: */
118: public static final
1.12 abaird 119: String ACCEPT_P = "w3c.www.protocol.http.accept";
1.5 abaird 120: /**
121: * Header properties - Set the accept language.
122: */
123: public static final
1.12 abaird 124: String ACCEPT_LANGUAGE_P = "w3c.www.protocol.http.acceptLanguage";
1.5 abaird 125: /**
126: * Header properties - Set the accept encodings.
127: */
128: public static final
1.12 abaird 129: String ACCEPT_ENCODING_P = "w3c.www.protocol.http.acceptEncoding";
1.5 abaird 130: /**
131: * Header properties - Should we use a proxy ?
132: */
133: public static final
1.12 abaird 134: String PROXY_SET_P = "proxySet";
1.5 abaird 135: /**
136: * Header properties - What is the proxy host name.
137: */
138: public static final
1.12 abaird 139: String PROXY_HOST_P = "proxyHost";
1.5 abaird 140: /**
141: * Header properties - What is the proxy port number.
142: */
143: public static final
1.12 abaird 144: String PROXY_PORT_P = "proxyPort";
1.2 abaird 145:
1.7 abaird 146: /**
147: * The default value for the <code>Accept</code> header.
148: */
149: public static final
150: String DEFAULT_ACCEPT = "*/*";
151: /**
152: * The default value for the <code>User-Agent</code> header.
153: */
154: public static final
155: String DEFAULT_USER_AGENT = "Jigsaw/1.0a2";
156:
1.1 abaird 157: private static HttpManager manager = null;
158:
159: /**
1.13 ! abaird 160: * The properties we initialized from.
! 161: */
! 162: ObservableProperties props = null;
! 163: /**
1.1 abaird 164: * The server this manager knows about, indexed by FQDN of target servers.
165: */
166: protected Hashtable servers = null;
167: /**
168: * The template request (the request we will clone to create new requests)
169: */
1.4 abaird 170: protected Request template = null ;
171: /**
1.9 abaird 172: * The LRU list of connections.
1.4 abaird 173: */
1.9 abaird 174: protected LRUList connectionsLru = null;
1.1 abaird 175: /**
176: * The filter engine attached to this manager.
177: */
178: FilterEngine filteng = null;
179:
180:
1.9 abaird 181: protected int conn_count = 0;
182: protected int conn_max = 5;
183:
1.1 abaird 184: /**
1.13 ! abaird 185: * Update the proxy configuration to match current properties setting.
! 186: * @return A boolean, <strong>true</strong> if change was done,
! 187: * <strong>false</strong> otherwise.
! 188: */
! 189:
! 190: protected boolean updateProxy() {
! 191: boolean set = props.getBoolean(PROXY_SET_P, false);
! 192: if ( set ) {
! 193: System.out.println("Using proxy !");
! 194: // Wow using a proxy now !
! 195: String host = props.getString(PROXY_HOST_P, null);
! 196: int port = props.getInteger(PROXY_PORT_P, -1);
! 197: URL proxy = null;
! 198: try {
! 199: proxy = new URL("http", host, port, "/");
! 200: } catch (Exception ex) {
! 201: return false;
! 202: }
! 203: // Now if a proxy...
! 204: if ( proxy != null )
! 205: getManager().template.setProxy(proxy);
! 206: } else {
! 207: getManager().template.setProxy(null);
! 208: }
! 209: return true;
! 210: }
! 211:
! 212: /**
! 213: * PropertyMonitoring implementation - Update properties on the fly !
! 214: * @param name The name of the property that has changed.
! 215: * @return A boolean, <strong>true</strong> if change is accepted,
! 216: * <strong>false</strong> otherwise.
! 217: */
! 218:
! 219: public boolean propertyChanged(String name) {
! 220: Request tpl = getManager().template;
! 221: if ( name.equals(FILTERS_PROP_P) ) {
! 222: return false;
! 223: } else if ( name.equals(CONN_MAX_P) ) {
! 224: setMaxConnections(props.getInteger(CONN_MAX_P, conn_max));
! 225: return true;
! 226: } else if ( name.equals(MAX_STALE_P) ) {
! 227: int ival = props.getInteger(MAX_STALE_P, -1);
! 228: if ( ival >= 0 )
! 229: tpl.setMaxStale(ival);
! 230: return true;
! 231: } else if ( name.equals(MIN_FRESH_P) ) {
! 232: int ival = props.getInteger(MIN_FRESH_P, -1);
! 233: if ( ival >= 0 )
! 234: tpl.setMinFresh(ival);
! 235: return true;
! 236: } else if ( name.equals(ONLY_IF_CACHED_P) ) {
! 237: tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
! 238: return true;
! 239: } else if ( name.equals(USER_AGENT_P) ) {
! 240: tpl.setValue("user-agent"
! 241: , props.getString(USER_AGENT_P
! 242: , DEFAULT_USER_AGENT));
! 243: return true;
! 244: } else if ( name.equals(ACCEPT_P) ) {
! 245: tpl.setValue("accept"
! 246: , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
! 247: return true;
! 248: } else if ( name.equals(ACCEPT_LANGUAGE_P) ) {
! 249: String sval = props.getString(ACCEPT_LANGUAGE_P, null);
! 250: if ( sval != null )
! 251: tpl.setValue("accept-language", sval);
! 252: return true;
! 253: } else if ( name.equals(ACCEPT_ENCODING_P) ) {
! 254: String sval = props.getString(ACCEPT_ENCODING_P, null);
! 255: if ( sval != null )
! 256: tpl.setValue("accept-encoding", sval);
! 257: return true;
! 258: } else if ( name.equals(PROXY_SET_P)
! 259: || name.equals(PROXY_HOST_P)
! 260: || name.equals(PROXY_PORT_P) ) {
! 261: return updateProxy();
! 262: } else {
! 263: return true;
! 264: }
! 265: }
! 266:
! 267: /**
1.4 abaird 268: * Allow the manager to interact with the user if needed.
269: * This will, for example, allow prompting for paswords, etc.
270: * @param onoff Turn interaction on or off.
271: */
272:
273: public void setAllowUserInteraction(boolean onoff) {
274: template.setAllowUserInteraction(onoff);
275: }
276:
277: /**
1.1 abaird 278: * Get an instance of the HTTP manager.
279: * This method returns an actual instance of the HTTP manager. It may
280: * return different managers, if it decides to distribute the load on
281: * different managers (avoid the HttpManager being a bottleneck).
282: * @return An application wide instance of the HTTP manager.
283: */
284:
285: public static synchronized HttpManager getManager() {
286: if ( manager == null ) {
287: manager = new HttpManager() ;
1.11 abaird 288: // Get the props we will initialize from:
289: Properties p = System.getProperties();
290: if ( p instanceof ObservableProperties )
1.13 ! abaird 291: manager.props = (ObservableProperties) p;
1.11 abaird 292: else
1.13 ! abaird 293: manager.props = new ObservableProperties(p);
! 294: ObservableProperties props = manager.props;
1.9 abaird 295: // Initialize this new manager filters:
1.12 abaird 296: String filters[] = props.getStringArray(FILTERS_PROP_P, null);
1.11 abaird 297: if ( filters != null ) {
298: for (int i = 0 ; i < filters.length ; i++) {
299: try {
300: Class c = Class.forName(filters[i]);
301: PropRequestFilter f = null;
302: f = (PropRequestFilter) c.newInstance();
303: f.initialize(manager);
304: } catch (Exception ex) {
305: System.err.println("Error initializing prop filters:");
306: System.err.println("Coulnd't initialize ["
307: + filters[i]
308: + "]: " + ex.getMessage());
309: ex.printStackTrace();
310: }
1.2 abaird 311: }
312: }
1.9 abaird 313: // The factory to create MIME reply holders:
314: manager.factory = new ReplyFactory();
315: // Setup the template request:
1.5 abaird 316: Request tpl = manager.template;
317: // Set some default headers value (from props)
318: // Check for a proxy ?
1.13 ! abaird 319: manager.updateProxy();
1.5 abaird 320: // CacheControl, only-if-cached
1.12 abaird 321: tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
1.5 abaird 322: // CacheControl, maxstale
1.12 abaird 323: int ival = props.getInteger(MAX_STALE_P, -1);
1.11 abaird 324: if ( ival >= 0 )
325: tpl.setMaxStale(ival);
1.5 abaird 326: // CacheControl, minfresh:
1.12 abaird 327: ival = props.getInteger(MIN_FRESH_P, -1);
1.11 abaird 328: if ( ival >= 0 )
329: tpl.setMinFresh(ival);
1.5 abaird 330: // General, User agent
1.11 abaird 331: tpl.setValue("user-agent"
1.12 abaird 332: , props.getString(USER_AGENT_P
1.11 abaird 333: , DEFAULT_USER_AGENT));
1.5 abaird 334: // General, Accept
1.11 abaird 335: tpl.setValue("accept"
1.12 abaird 336: , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
1.5 abaird 337: // General, Accept-Language
1.12 abaird 338: String sval = props.getString(ACCEPT_LANGUAGE_P, null);
1.11 abaird 339: if ( sval != null )
340: tpl.setValue("accept-language", sval);
1.5 abaird 341: // General, Accept-Encoding
1.12 abaird 342: sval = props.getString(ACCEPT_ENCODING_P, null);
1.11 abaird 343: if ( sval != null )
344: tpl.setValue("accept-encoding", sval);
345: // Maximum number of allowed connections:
1.12 abaird 346: manager.conn_max = props.getInteger(CONN_MAX_P, 5);
1.13 ! abaird 347: // Register ourself as a property observer:
! 348: props.registerObserver(manager);
1.1 abaird 349: }
350: return manager;
351: }
352:
353:
354: /**
355: * Get the appropriate server object for handling request to given target.
356: * @param key The server's identifier encoded as a <code>host:port</code>
357: * String.
358: * @return An object complying to the HttpServer interface.
359: * @exception HttpException If the given host name couldn't be resolved.
360: */
361:
1.5 abaird 362: protected synchronized HttpServer lookupServer(String host, int port)
1.1 abaird 363: throws HttpException
364: {
1.5 abaird 365: int p = (port == -1) ? 80 : port;
366: String id = (p == 80) ? host : (host +":"+p);
1.1 abaird 367: // Check for an existing server:
368: HttpServer server = (HttpServer) servers.get(id);
369: if ( server != null )
370: return server;
371: // Create and register a new server:
372: server = new HttpBasicServer();
1.9 abaird 373: server.initialize(this, new HttpServerState(server), host, p);
1.1 abaird 374: servers.put(id, server);
375: return server;
376: }
1.5 abaird 377:
1.1 abaird 378: /**
1.9 abaird 379: * The given connection is about to be used.
1.4 abaird 380: * Update our list of available servers.
1.9 abaird 381: * @param conn The idle connection.
1.4 abaird 382: */
383:
1.9 abaird 384: public void notifyUse(HttpConnection conn) {
385: connectionsLru.remove(conn);
1.4 abaird 386: }
387:
388: /**
1.9 abaird 389: * The given connection can be reused, but is now idle.
390: * @param conn The connection that is now idle.
1.4 abaird 391: */
392:
1.9 abaird 393: public void notifyIdle(HttpConnection conn) {
394: connectionsLru.toHead(conn);
1.4 abaird 395: }
396:
397: /**
1.9 abaird 398: * The given connection has just been created.
399: * @param conn The newly created connection.
400: */
401:
402: protected synchronized void notifyConnection(HttpConnection conn) {
403: if ( ++conn_count > conn_max )
404: closeAnyConnection();
1.11 abaird 405: System.out.println("conn-count="+conn_count);
1.9 abaird 406: }
407:
408: /**
409: * The given connection has been deleted.
410: * @param conn The deleted connection.
411: */
412:
413: protected void deleteConnection(HttpConnection conn) {
414: HttpServerState ss = conn.getServer().getState();
415: ss.deleteConnection(conn);
416: synchronized(this) {
417: --conn_count;
418: notifyAll();
419: }
420: }
421:
422: protected synchronized boolean tooManyConnections() {
423: return conn_count > conn_max;
424: }
425:
426: /**
427: * Try reusing one of the idle connection of that server, if any.
428: * @param server The target server.
429: * @return An currently idle connection to the given server.
1.4 abaird 430: */
431:
1.9 abaird 432: protected HttpConnection getConnection(HttpServer server) {
433: HttpServerState ss = server.getState();
434: return ss.getConnection();
435: }
436:
437: protected synchronized void waitForConnection(HttpServer server)
438: throws InterruptedException
439: {
440: wait();
1.4 abaird 441: }
442:
443: /**
1.9 abaird 444: * Close one connection, but pickling the least recently used one.
445: * @return A boolean, <strong>true</strong> if a connection was closed
446: * <strong>false</strong> otherwise.
1.4 abaird 447: */
448:
1.9 abaird 449: protected boolean closeAnyConnection() {
1.11 abaird 450: System.out.println("*** closeAnyConnection.");
1.9 abaird 451: HttpConnection conn = (HttpConnection) connectionsLru.removeTail();
452: if ( conn != null ) {
1.11 abaird 453: System.out.println("*** closeAnyConnection: closing "+conn);
1.9 abaird 454: conn.close();
455: deleteConnection(conn);
456: return true;
457: } else {
458: return false;
459: }
1.4 abaird 460: }
461:
462: /**
1.1 abaird 463: * One of our server handler wants to open a connection.
464: * @param block A boolean indicating whether we should block the calling
465: * thread until a token is available (otherwise, the method will just
466: * peek at the connection count, and return the appropriate result).
467: * @return A boolean, <strong>true</strong> if the connection can be
468: * opened straight, <strong>false</strong> otherwise.
469: */
470:
1.9 abaird 471: protected boolean negotiateConnection(HttpServer server) {
472: HttpServerState ss = server.getState();
1.10 abaird 473: if ( ! tooManyConnections() ) {
1.4 abaird 474: return true;
1.10 abaird 475: } else if ( ss.notEnoughConnections() ) {
476: return closeAnyConnection();
1.9 abaird 477: } else if ( servers.size() > conn_max ) {
478: return closeAnyConnection();
1.4 abaird 479: }
1.9 abaird 480: return false;
481: }
482:
483: /**
484: * A new client connection has been established.
485: * This method will try to maintain a maximum number of established
486: * connections, by closing idle connections when possible.
487: * @param server The server that has established a new connection.
488: */
489:
490: protected final synchronized void incrConnCount(HttpServer server) {
491: if ( ++conn_count > conn_max )
492: closeAnyConnection();
493: }
494:
495: /**
496: * Decrement the number of established connections.
497: * @param server The server that has closed one connection to its target.
498: */
499:
500: protected final synchronized void decrConnCount(HttpServer server) {
501: --conn_count;
1.1 abaird 502: }
503:
504: /**
505: * Run the given request, in synchronous mode.
506: * This method will launch the given request, and block the calling thread
507: * until the response headers are available.
508: * @param request The request to run.
509: * @return An instance of Reply, containing all the reply
510: * informations.
511: * @exception HTTPException If something failed during request processing.
512: */
513:
514: public Reply runRequest(Request request)
515: throws HttpException
516: {
517: URL target = request.getURL();
518: // Locate the appropriate target server:
1.5 abaird 519: HttpServer server = null;
520: URL proxy = request.getProxy();
521: if ( proxy != null ) {
522: server = lookupServer(proxy.getHost(), proxy.getPort());
523: } else {
524: server = lookupServer(target.getHost(), target.getPort());
525: }
1.1 abaird 526: // Now run through the ingoing filters:
527: RequestFilter filters[] = filteng.run(request);
528: if ( filters != null ) {
529: for (int i = 0 ; i < filters.length ; i++) {
1.3 abaird 530: Reply fr = filters[i].ingoingFilter(request);
531: if ( fr != null )
532: return fr;
1.1 abaird 533: }
534: }
535: // Get the server to give back a reply:
536: Reply reply = server.runRequest(request);
537: // Apply the filters on the way back:
538: if ( filters != null ) {
1.3 abaird 539: for (int i = 0 ; i < filters.length ; i++) {
540: Reply fr = filters[i].outgoingFilter(request, reply);
541: if ( fr != null )
542: return fr;
543: }
1.1 abaird 544: }
545: return reply;
546: }
547:
548: /**
549: * Get this manager's reply factory.
550: * The Reply factory is used when prsing incomming reply from servers, it
551: * decides what object will be created to hold the actual reply from the
552: * server.
553: * @return An object compatible with the MimeParserFactory interface.
554: */
555:
556: MimeParserFactory factory = null ;
557:
1.9 abaird 558: public MimeParserFactory getReplyFactory() {
1.1 abaird 559: return factory;
560: }
561:
562: /**
563: * Add a new request filter.
564: * Request filters are called <em>before</em> a request is launched, and
565: * <em>after</em> the reply headers are available. They allow applications
566: * to setup specific request headers (such as PICS, or PEP stuff) on the
567: * way in, and check the reply on the way out.
568: * <p>Request filters are application wide: if their scope matches
569: * the current request, then they will always be aplied.
570: * <p>Filter scopes are defined inclusively and exclusively
571: * @param filter The request filter to add.
572: */
573:
574: public void setFilter(URL incs[], URL exs[], RequestFilter filter) {
575: if ( incs != null ) {
576: for (int i = 0 ; i < incs.length ; i++)
577: filteng.setFilter(incs[i], true, filter);
578: }
579: if ( exs != null ) {
580: for (int i = 0 ; i < exs.length ; i++)
581: filteng.setFilter(exs[i], false, filter);
582: }
583: return;
584: }
585:
586: public void setFilter(RequestFilter filter) {
587: filteng.setFilter(filter);
588: }
589:
590: /**
591: * Add a request processor.
592: * Request processors are application wide hooks, able to answer request
593: * by querying a local cache. An application can set as many request
594: * processor as it wants, each of them will be called in trun (in the order
595: * they were registered), if any of them returns a reply, the request
596: * processing will be halted, and the generated reply returned.
597: * <p>Request processors can also be used to query distant cache, through
598: * some home-brew protocols.
599: * @param processor The request processor to be added.
600: */
601:
602: public void addProcessor(RequestProcessor processor) {
603: }
604:
605: /**
606: * Remove a request processor.
607: * Remove the given request processor.
608: * @return A boolean, <strong>true</strong> if the processor was found
609: * and removed, <strong>false</strong> otherwise.
610: */
611:
612: public boolean removeProcessor(RequestProcessor processor) {
613: return false;
614: }
615:
616: /**
617: * Create a new default outgoing request.
618: * This method should <em>always</em> be used to create outgoing requests.
619: * It will initialize the request with appropriate default values for
620: * the various headers, and make sure that the request is enhanced by
621: * the registered request filters.
622: * @return An instance of Request, suitable to be launched.
623: */
624:
625: public Request createRequest() {
626: return (Request) template.getClone() ;
627: }
628:
629: /**
630: * Global settings - Set the max number of allowed connections.
631: * Set the maximum number of simultaneous connections that can remain
632: * opened. The manager will take care of queuing requests if this number
633: * is reached.
634: * <p>This value defaults to the value of the
635: * <code>w3c.www.http.maxConnections</code> property.
636: * @param max_conn The allowed maximum simultaneous open connections.
637: */
638:
1.13 ! abaird 639: public synchronized void setMaxConnections(int max_conn) {
! 640: this.conn_max = max_conn;
1.1 abaird 641: }
642:
643: /**
644: * Global settings - Set an optional proxy to use.
645: * Set the proxy to which all requests should be targeted. If the
646: * <code>w3c.www.http.proxy</code> property is defined, it will be
647: * used as the default value.
648: * @param proxy The URL for the proxy to use.
649: */
650:
651: public void setProxy(URL proxy) {
1.5 abaird 652: template.setProxy(proxy);
1.1 abaird 653: }
654:
655: /**
656: * Global settings - Set the request timeout.
657: * Once a request has been emited, the HttpManager will sit for this
658: * given number of milliseconds before the request is declared to have
659: * timed-out.
660: * <p>This timeout value defaults to the value of the
661: * <code>w3c.www.http.requestTimeout</code> property value.
662: * @param ms The timeout value in milliseconds.
663: */
664:
665: public void setRequestTimeout(int ms) {
666: }
667:
668: /**
669: * Global settings - Define a global request header.
670: * Set a default value for some request header. Once defined, the
671: * header will automatically be defined on <em>all</em> outgoing requests
672: * created through the <code>createRequest</code> request.
673: * @param name The name of the header, case insensitive.
674: * @param value It's default value.
675: */
676:
677: public void setGlobalHeader(String name, String value) {
678: template.setValue(name, value);
679: }
680:
681: public String getGlobalHeader(String name) {
682: return template.getValue(name);
683: }
684:
685: /**
686: * Create a new HttpManager.
687: * This can only be called from this package. The caller must rather
688: * use the <code>getManager</code> method.
689: * @param props The properties from which the manager should initialize
690: * itself, or <strong>null</strong> if none are available.
691: */
692:
693: HttpManager(Properties props) {
1.9 abaird 694: this.template = new Request(this);
695: this.servers = new Hashtable();
696: this.filteng = new FilterEngine();
697: this.connectionsLru = new SyncLRUList();
1.1 abaird 698: }
699:
700: HttpManager() {
701: this(System.getProperties());
702: }
703:
704: /**
705: * DEBUGGING !
706: */
707:
708: public static void main(String args[]) {
709: try {
710: // Get the manager, and define some global headers:
711: HttpManager manager = HttpManager.getManager();
712: manager.setGlobalHeader("User-Agent", "Jigsaw/1.0a");
713: manager.setGlobalHeader("Accept", "*/*;q=1.0");
714: manager.setGlobalHeader("Accept-Encoding", "gzip");
715: Request request = manager.createRequest();
716: request.setURL(new URL(args[0]));
717: request.setMethod("GET");
718: Reply reply = manager.runRequest(request);
719: // Display some infos:
720: System.out.println("last-modified: "+reply.getLastModified());
721: System.out.println("length : "+reply.getContentLength());
722: // Display the returned body:
723: InputStream in = reply.getInputStream();
724: byte buf[] = new byte[4096];
725: int cnt = 0;
726: while ((cnt = in.read(buf)) > 0)
727: System.out.print(new String(buf, 0, 0, cnt));
728: System.out.println("-");
729: in.close();
730: } catch (Exception ex) {
731: ex.printStackTrace();
732: }
733: System.exit(1);
734: }
735: }
Webmaster