com.twitter.common.net.http
Class HttpServerDispatch

java.lang.Object
  extended by com.twitter.common.net.http.HttpServerDispatch

public class HttpServerDispatch
extends Object

A simple multi-threaded HTTP server dispatcher. Supports registering handlers for different URI paths, which will be called when a request is received.


Constructor Summary
HttpServerDispatch()
           
 
Method Summary
 int getPort()
           
 org.mortbay.jetty.servlet.Context getRootContext()
           
 boolean isStarted()
           
 boolean listen(int port)
          Opens the HTTP server on the given port.
 boolean listen(int minPort, int maxPort)
          Opens the HTTP server on random port within the given range.
 void registerHandler(String path, javax.servlet.http.HttpServlet handler, Map<String,String> initParams, boolean silent)
          Registers a URI handler, replacing the existing handler if it exists.
 void stop()
          Stops the HTTP server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpServerDispatch

public HttpServerDispatch()
Method Detail

listen

public boolean listen(int port)
Opens the HTTP server on the given port.

Parameters:
port - The port to listen on.
Returns:
true if the server started successfully on the port, false otherwise.

listen

public boolean listen(int minPort,
                      int maxPort)
Opens the HTTP server on random port within the given range.

Parameters:
minPort - The minimum port number to listen on.
maxPort - The maximum port number to listen on.
Returns:
true if the server started successfully on the port, false otherwise.

isStarted

public boolean isStarted()

getPort

public int getPort()

stop

public void stop()
Stops the HTTP server.


registerHandler

public void registerHandler(String path,
                            javax.servlet.http.HttpServlet handler,
                            @Nullable
                            Map<String,String> initParams,
                            boolean silent)
Registers a URI handler, replacing the existing handler if it exists.

Parameters:
path - The URI path that the handler should be called for.
handler - The handler to call.
initParams - An optional map of servlet init parameter names and their values.
silent - Whether to display the registered handler in the root "/" response. Useful for handlers that you want to avoid accidental clicks on.

getRootContext

public org.mortbay.jetty.servlet.Context getRootContext()