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

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

Webmaster