Annotation of java/classes/org/w3c/www/protocol/http/HttpManager.java, revision 1.14

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

Webmaster