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