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