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

1.1       abaird      1: // HttpManager.java
1.4     ! abaird      2: // $Id: HttpManager.java,v 1.3 1996/07/26 23:24:58 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.2       abaird     47: 
1.1       abaird     48:     private static HttpManager manager = null;
                     49:     
                     50:     /**
                     51:      * The server this manager knows about, indexed by FQDN of target servers.
                     52:      */
                     53:     protected Hashtable servers = null;
                     54:     /**
                     55:      * The template request (the request we will clone to create new requests)
                     56:      */
1.4     ! abaird     57:     protected Request template = null ;
        !            58:     /**
        !            59:      * The LRU list of created servers.
        !            60:      */
        !            61:     protected LRUList serversLru = null;
        !            62:     /**
        !            63:      * The currrent number of opened connections.
        !            64:      */
        !            65:     protected int conn_count = 0;
        !            66:     /**
        !            67:      * The maximum number of allowed connections.
        !            68:      */
        !            69:     protected int conn_max = 32;
1.1       abaird     70:     /**
                     71:      * The filter engine attached to this manager.
                     72:      */
                     73:     FilterEngine filteng = null;
                     74:      
                     75: 
                     76:     /**
1.4     ! abaird     77:      * Allow the manager to interact with the user if needed.
        !            78:      * This will, for example, allow prompting for paswords, etc.
        !            79:      * @param onoff Turn interaction on or off.
        !            80:      */
        !            81: 
        !            82:     public void setAllowUserInteraction(boolean onoff) {
        !            83:        template.setAllowUserInteraction(onoff);
        !            84:     }
        !            85: 
        !            86:     /**
1.1       abaird     87:      * Get an instance of the HTTP manager.
                     88:      * This method returns an actual instance of the HTTP manager. It may
                     89:      * return different managers, if it decides to distribute the load on
                     90:      * different managers (avoid the HttpManager being a bottleneck).
                     91:      * @return An application wide instance of the HTTP manager.
                     92:      */
                     93: 
                     94:     public static synchronized HttpManager getManager() {
                     95:        if ( manager == null ) {
                     96:            manager = new HttpManager() ;
1.2       abaird     97:            // Initialize this new manager:
                     98:            String filters = System.getProperty(FILTERS_PROP);
                     99:            if ( filters == null )
                    100:                return manager;
                    101:            StringTokenizer st = new StringTokenizer(filters, "|");
                    102:            while (st.hasMoreTokens() ) {
                    103:                String cls = (String) st.nextElement();
                    104:                try {
                    105:                    Class c = Class.forName(cls);
                    106:                    PropRequestFilter f = (PropRequestFilter) c.newInstance();
                    107:                    f.initialize(manager);
                    108:                } catch (Exception ex) {
                    109:                    System.err.println("Error initializing prop filters:");
                    110:                    System.err.println("Coulnd't initialize ["
                    111:                                       + cls
                    112:                                       + "]: " + ex.getMessage());
                    113:                    ex.printStackTrace();
                    114:                }
                    115:            }
1.1       abaird    116:        }
                    117:        return manager;
                    118:     }
                    119: 
                    120: 
                    121:     /**
                    122:      * Get the appropriate server object for handling request to given target.
                    123:      * @param key The server's identifier encoded as a <code>host:port</code>
                    124:      * String.
                    125:      * @return An object complying to the HttpServer interface.
                    126:      * @exception HttpException If the given host name couldn't be resolved.
                    127:      */
                    128: 
                    129:     protected synchronized HttpServer lookupServer(String host, int port) 
                    130:        throws HttpException
                    131:     {
                    132:        String id = ((port == 80) ? host : (host +":"+port));
                    133:        // Check for an existing server:
                    134:        HttpServer server = (HttpServer) servers.get(id);
                    135:        if ( server != null )
                    136:            return server;
                    137:        // Create and register a new server:
                    138:        server = new HttpBasicServer();
                    139:        server.initialize(this, host, port);
                    140:        servers.put(id, server);
1.4     ! abaird    141:        serversLru.toHead(server);
1.1       abaird    142:        return server;
                    143:     }
                    144:      
                    145:     /**
1.4     ! abaird    146:      * The given server is about to be used.
        !           147:      * Update our list of available servers.
        !           148:      * @param server The server that is being used.
        !           149:      */
        !           150: 
        !           151:     public void notifyUse(HttpServer server) {
        !           152:        serversLru.toHead(server);
        !           153:     }
        !           154: 
        !           155:     /**
        !           156:      * Close all the connections that this server has established.
        !           157:      * This server has remain idle for some time, we can now try to close
        !           158:      * all its connections.
        !           159:      * @param server The server whose connections are to be closed.
        !           160:      * @return A boolean, <strong>true</strong> if we can establish more 
        !           161:      * connections, <strong>false</strong> otherwise.
        !           162:      */
        !           163: 
        !           164:     public boolean closeServer(HttpServer server) {
        !           165:        server.closeConnections();
        !           166:        return (conn_count < conn_max);
        !           167:     }
        !           168: 
        !           169:     /**
        !           170:      * A new client connection has been established.
        !           171:      * This method will try to maintain a maximum number of established
        !           172:      * connections, by closing idle connections when possible.
        !           173:      * @param server The server that has established a new connection.
        !           174:      */
        !           175: 
        !           176:     public synchronized void incrConnCount(HttpServer server) {
        !           177: System.out.println("incrConnCount: "+(conn_count+1));
        !           178:        if ( ++conn_count > conn_max ) {
        !           179:            // Do some clean-up:
        !           180:            HttpServer s = (HttpServer) serversLru.getTail();
        !           181:            while (s != null) {
        !           182:                if ((s != server) && closeServer(s))
        !           183:                    break;
        !           184:                s = (HttpServer) s.getPrev();
        !           185:            }
        !           186:        } 
        !           187:        return;
        !           188:     }
        !           189: 
        !           190:     /**
        !           191:      * Decrement the number of established connections.
        !           192:      * @param server The server that has closed one connection to its target.
        !           193:      */
        !           194: 
        !           195:     public synchronized void decrConnCount(HttpServer server) {
        !           196: System.out.println("decrConnCount: "+(conn_count-1));
        !           197:        --conn_count;
        !           198:     }
        !           199: 
        !           200:     /**
1.1       abaird    201:      * One of our server handler wants to open a connection.
                    202:      * @param block A boolean indicating whether we should block the calling
                    203:      * thread until a token is available (otherwise, the method will just
                    204:      * peek at the connection count, and return the appropriate result).
                    205:      * @return A boolean, <strong>true</strong> if the connection can be
                    206:      * opened straight, <strong>false</strong> otherwise.
                    207:      */
                    208: 
1.4     ! abaird    209:     protected boolean negotiateConnection(HttpServer server, int ccount) {
        !           210:        if ( ccount <= 1 ) {
        !           211:            // Always allow at least two connections per server (deadlocks)
        !           212:            return true;
        !           213:        } else if ( conn_count+1 < conn_max ) {
        !           214:            // We can use more connections:
        !           215:            return true;
        !           216:        } 
        !           217:        // Try to close some other server:
        !           218:        if ( servers.size() > conn_max ) {
        !           219:            HttpServer last = (HttpServer) serversLru.getTail();
        !           220:            return (last != server) && closeServer(last);
        !           221:        } else {
        !           222:            return false;
        !           223:        }
1.1       abaird    224:     }
                    225: 
                    226:     /**
                    227:      * Run the given request, in synchronous mode.
                    228:      * This method will launch the given request, and block the calling thread
                    229:      * until the response headers are available.
                    230:      * @param request The request to run.
                    231:      * @return An instance of Reply, containing all the reply 
                    232:      * informations.
                    233:      * @exception HTTPException If something failed during request processing.
                    234:      */
                    235:     
                    236:     public Reply runRequest(Request request)
                    237:        throws HttpException
                    238:     {
                    239:        URL target = request.getURL();
                    240:        // Locate the appropriate target server:
                    241:        int port = target.getPort();
                    242:        HttpServer server = lookupServer(target.getHost()
                    243:                                         , (port == -1) ? 80 : port);
                    244:        // Now run through the ingoing filters:
                    245:        RequestFilter filters[] = filteng.run(request);
                    246:        if ( filters != null ) {
                    247:            for (int i = 0 ; i < filters.length ; i++) {
1.3       abaird    248:                Reply fr = filters[i].ingoingFilter(request);
                    249:                if ( fr != null )
                    250:                    return fr;
1.1       abaird    251:            }
                    252:        }
                    253:        // Get the server to give back a reply:
                    254:        Reply reply = server.runRequest(request);
                    255:        // Apply the filters on the way back:
                    256:        if ( filters != null ) {
1.3       abaird    257:            for (int i = 0 ; i < filters.length ; i++) {
                    258:                Reply fr = filters[i].outgoingFilter(request, reply);
                    259:                if ( fr != null )
                    260:                    return fr;
                    261:            }
1.1       abaird    262:        }
                    263:        return reply;
                    264:     }
                    265: 
                    266:     /**
                    267:      * Get this manager's reply factory.
                    268:      * The Reply factory is used when prsing incomming reply from servers, it
                    269:      * decides what object will be created to hold the actual reply from the 
                    270:      * server.
                    271:      * @return An object compatible with the MimeParserFactory interface.
                    272:      */
                    273: 
                    274:     MimeParserFactory factory = null ;
                    275: 
                    276:     public synchronized MimeParserFactory getReplyFactory() {
                    277:        if ( factory == null )
                    278:            factory = new ReplyFactory();
                    279:        return factory;
                    280:     }
                    281: 
                    282:     /**
                    283:      * Add a new request filter.
                    284:      * Request filters are called <em>before</em> a request is launched, and
                    285:      * <em>after</em> the reply headers are available. They allow applications
                    286:      * to setup specific request headers (such as PICS, or PEP stuff) on the
                    287:      * way in, and check the reply on the way out.
                    288:      * <p>Request filters are application wide: if their scope matches
                    289:      * the current request, then they will always be aplied.
                    290:      * <p>Filter scopes are defined inclusively and exclusively
                    291:      * @param filter The request filter to add.
                    292:      */
                    293: 
                    294:     public void setFilter(URL incs[], URL exs[], RequestFilter filter) {
                    295:        if ( incs != null ) {
                    296:            for (int i = 0 ; i < incs.length ; i++)
                    297:                filteng.setFilter(incs[i], true, filter);
                    298:        }
                    299:        if ( exs != null ) {
                    300:            for (int i = 0 ; i < exs.length ; i++)
                    301:                filteng.setFilter(exs[i], false, filter);
                    302:        }
                    303:        return;
                    304:     }
                    305: 
                    306:     public void setFilter(RequestFilter filter) {
                    307:        filteng.setFilter(filter);
                    308:     }
                    309: 
                    310:     /**
                    311:      * Add a request processor.
                    312:      * Request processors are application wide hooks, able to answer request
                    313:      * by querying a local cache. An application can set as many request
                    314:      * processor as it wants, each of them will be called in trun (in the order
                    315:      * they were registered), if any of them returns a reply, the request
                    316:      * processing will be halted, and the generated reply returned.
                    317:      * <p>Request processors can also be used to query distant cache, through
                    318:      * some home-brew protocols.
                    319:      * @param processor The request processor to be added.
                    320:      */
                    321: 
                    322:     public void addProcessor(RequestProcessor processor) {
                    323:     }
                    324: 
                    325:     /**
                    326:      * Remove a request processor.
                    327:      * Remove the given request processor.
                    328:      * @return A boolean, <strong>true</strong> if the processor was found
                    329:      * and removed, <strong>false</strong> otherwise.
                    330:      */
                    331: 
                    332:     public boolean removeProcessor(RequestProcessor processor) {
                    333:        return false;
                    334:     }
                    335: 
                    336:     /**
                    337:      * Create a new default outgoing request.
                    338:      * This method should <em>always</em> be used to create outgoing requests.
                    339:      * It will initialize the request with appropriate default values for 
                    340:      * the various headers, and make sure that the request is enhanced by
                    341:      * the registered request filters.
                    342:      * @return An instance of Request, suitable to be launched.
                    343:      */
                    344: 
                    345:     public Request createRequest() {
                    346:        return (Request) template.getClone() ;
                    347:     }
                    348: 
                    349:     /**
                    350:      * Global settings - Set the max number of allowed connections.
                    351:      * Set the maximum number of simultaneous connections that can remain
                    352:      * opened. The manager will take care of queuing requests if this number
                    353:      * is reached.
                    354:      * <p>This value defaults to the value of the 
                    355:      * <code>w3c.www.http.maxConnections</code> property.
                    356:      * @param max_conn The allowed maximum simultaneous open connections.
                    357:      */
                    358: 
                    359:     public void setMaxConnections(int max_conn) {
                    360:     }
                    361: 
                    362:     /**
                    363:      * Global settings - Set an optional proxy to use.
                    364:      * Set the proxy to which all requests should be targeted. If the
                    365:      * <code>w3c.www.http.proxy</code> property is defined, it will be
                    366:      * used as the default value.
                    367:      * @param proxy The URL for the proxy to use.
                    368:      */
                    369: 
                    370:     public void setProxy(URL proxy) {
                    371:     }
                    372: 
                    373:     /**
                    374:      * Global settings - Set the request timeout.
                    375:      * Once a request has been emited, the HttpManager will sit for this 
                    376:      * given number of milliseconds before the request is declared to have
                    377:      * timed-out.
                    378:      * <p>This timeout value defaults to the value of the
                    379:      * <code>w3c.www.http.requestTimeout</code> property value.
                    380:      * @param ms The timeout value in milliseconds.
                    381:      */
                    382: 
                    383:     public void setRequestTimeout(int ms) {
                    384:     }
                    385: 
                    386:     /**
                    387:      * Global settings - Define a global request header.
                    388:      * Set a default value for some request header. Once defined, the
                    389:      * header will automatically be defined on <em>all</em> outgoing requests
                    390:      * created through the <code>createRequest</code> request.
                    391:      * @param name The name of the header, case insensitive.
                    392:      * @param value It's default value.
                    393:      */
                    394:     
                    395:     public void setGlobalHeader(String name, String value) {
                    396:        template.setValue(name, value);
                    397:     }
                    398: 
                    399:     public String getGlobalHeader(String name) {
                    400:        return template.getValue(name);
                    401:     }
                    402: 
                    403:     /**
                    404:      * Create a new HttpManager.
                    405:      * This can only be called from this package. The caller must rather
                    406:      * use the <code>getManager</code> method.
                    407:      * @param props The properties from which the manager should initialize 
                    408:      * itself, or <strong>null</strong> if none are available.
                    409:      */
                    410: 
                    411:     HttpManager(Properties props) {
1.4     ! abaird    412:        this.template   = new Request(this);
        !           413:        this.servers    = new Hashtable();
        !           414:        this.filteng    = new FilterEngine();
        !           415:        this.serversLru = new SyncLRUList();
1.1       abaird    416:     }
                    417: 
                    418:     HttpManager() {
                    419:        this(System.getProperties());
                    420:     }
                    421: 
                    422:     /**
                    423:      * DEBUGGING !
                    424:      */
                    425: 
                    426:     public static void main(String args[]) {
                    427:        try {
                    428:            // Get the manager, and define some global headers:
                    429:            HttpManager manager = HttpManager.getManager();
                    430:            manager.setGlobalHeader("User-Agent", "Jigsaw/1.0a");
                    431:            manager.setGlobalHeader("Accept", "*/*;q=1.0");
                    432:            manager.setGlobalHeader("Accept-Encoding", "gzip");
                    433:            Request request = manager.createRequest();
                    434:            request.setURL(new URL(args[0]));
                    435:            request.setMethod("GET");
                    436:            Reply       reply   = manager.runRequest(request);
                    437:            // Display some infos:
                    438:            System.out.println("last-modified: "+reply.getLastModified());
                    439:            System.out.println("length       : "+reply.getContentLength());
                    440:            // Display the returned body:
                    441:            InputStream in = reply.getInputStream();
                    442:            byte buf[] = new byte[4096];
                    443:            int  cnt   = 0;
                    444:            while ((cnt = in.read(buf)) > 0) 
                    445:                System.out.print(new String(buf, 0, 0, cnt));
                    446:            System.out.println("-");
                    447:            in.close();
                    448:        } catch (Exception ex) {
                    449:            ex.printStackTrace();
                    450:        }
                    451:        System.exit(1);
                    452:     }
                    453: }

Webmaster