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