FileDocCategorySizeDatePackage
TestHttpServer.javaAPI DocAndroid 1.5 API8003Wed May 06 22:42:02 BST 2009com.android.unit_tests

TestHttpServer

public class TestHttpServer extends Object

Fields Summary
private final HttpParams
params
private final BasicHttpProcessor
httpproc
private final ConnectionReuseStrategy
connStrategy
private final HttpResponseFactory
responseFactory
private final HttpRequestHandlerRegistry
reqistry
private final ServerSocket
serversocket
private HttpExpectationVerifier
expectationVerifier
private Thread
listener
private volatile boolean
shutdown
Constructors Summary
public TestHttpServer()

        super();
        this.params = new BasicHttpParams();
        this.params
            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 20000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "TEST-SERVER/1.1");
        this.httpproc = new BasicHttpProcessor();
        this.httpproc.addInterceptor(new ResponseDate());
        this.httpproc.addInterceptor(new ResponseServer());
        this.httpproc.addInterceptor(new ResponseContent());
        this.httpproc.addInterceptor(new ResponseConnControl());
        this.connStrategy = new DefaultConnectionReuseStrategy();
        this.responseFactory = new DefaultHttpResponseFactory();
        this.reqistry = new HttpRequestHandlerRegistry();
        this.serversocket = new ServerSocket(0);
    
Methods Summary
private org.apache.http.HttpServerConnectionacceptConnection()

        Socket socket = this.serversocket.accept();
        DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
        conn.bind(socket, this.params);
        return conn;
    
public java.net.InetAddressgetInetAddress()

        return this.serversocket.getInetAddress();
    
public intgetPort()

        return this.serversocket.getLocalPort();
    
public voidregisterHandler(java.lang.String pattern, org.apache.http.protocol.HttpRequestHandler handler)

        this.reqistry.register(pattern, handler);
    
public voidsetExpectationVerifier(org.apache.http.protocol.HttpExpectationVerifier expectationVerifier)

        this.expectationVerifier = expectationVerifier;
    
public voidshutdown()

        if (this.shutdown) {
            return;
        }
        this.shutdown = true;
        try {
            this.serversocket.close();
        } catch (IOException ignore) {}
        this.listener.interrupt();
        try {
            this.listener.join(1000);
        } catch (InterruptedException ignore) {}
    
public voidstart()

        if (this.listener != null) {
            throw new IllegalStateException("Listener already running");
        }
        this.listener = new Thread(new Runnable() {
           
            public void run() {
                while (!shutdown && !Thread.interrupted()) {
                    try {
                        // Set up HTTP connection
                        HttpServerConnection conn = acceptConnection();
                        // Set up the HTTP service
                        HttpService httpService = new HttpService(
                                httpproc, 
                                connStrategy,
                                responseFactory);
                        httpService.setParams(params);
                        httpService.setExpectationVerifier(expectationVerifier);
                        httpService.setHandlerResolver(reqistry);
                        
                        // Start worker thread
                        Thread t = new WorkerThread(httpService, conn);
                        t.setDaemon(true);
                        t.start();
                    } catch (InterruptedIOException ex) {
                        break;
                    } catch (IOException e) {
                        break;
                    }
                }
            }
            
        });
        this.listener.start();