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

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

Webmaster