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

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

Webmaster