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