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