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

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

Webmaster