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