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

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

Webmaster