org.webbitserver
Interface WebServer

All Superinterfaces:
Endpoint<WebServer>
All Known Implementing Classes:
NettyWebServer

public interface WebServer
extends Endpoint<WebServer>

Configures an event based webserver.

To create an instance, use WebServers.createWebServer(int).

As with many of the interfaces in webbitserver, setter style methods return a reference to this, to allow for simple initialization using method chaining.

Hello World Example

 class HelloWorldHandler implements HttpHandler {
   void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) {
     response.header("Content-Type", "text/html")
             .content("Hello World")
             .end();
   }
 }
 WebServer webServer = WebServers.createWebServer(8080)
                                 .add(new HelloWorldHandler());
 webServer.start();
 print("Point your browser to " + webServer.getUri());
 

Serving Static Files

 WebServer webServer = WebServers.createWebServer(8080)
                                 .add(new StaticFileHandler("./wwwdata"));
 webServer.start();
 

Author:
Joe Walnes
See Also:
WebServers, HttpHandler, WebSocketConnection, EventSourceConnection

Method Summary
 WebServer add(HttpHandler handler)
          Add an HttpHandler.
 WebServer add(String path, EventSourceHandler handler)
          Add a WebSocketHandler for dealing with WebSockets.
 WebServer add(String path, HttpHandler handler)
          Add an HttpHandler that will only respond to a certain path (e.g "/some/page").
 WebServer add(String path, WebSocketHandler handler)
          Add a WebSocketHandler for dealing with WebSockets.
 int getPort()
          Get base port that webserver is serving on.
 WebServer setupSsl(InputStream keyStore, String storePass, String keyPass)
          Setup SSL/TLS handler
 WebServer staleConnectionTimeout(long millis)
          Number of milliseconds before a stale HTTP keep-alive connection is closed by the server.
 
Methods inherited from interface org.webbitserver.Endpoint
connectionExceptionHandler, getExecutor, getUri, setupSsl, start, stop, uncaughtExceptionHandler
 

Method Detail

add

WebServer add(HttpHandler handler)
Add an HttpHandler. When a request comes in the first HttpHandler will be invoked. The HttpHandler should either handle the request, or pass the request onto the next HttpHandler (using HttpControl.nextHandler()). This is repeated until a HttpHandler returns a response. If there are no remaining handlers, the webserver shall return 404 NOT FOUND to the browser.

HttpHandlers are attempted in the order in which they are added to the WebServer.

See Also:
HttpHandler

add

WebServer add(String path,
              HttpHandler handler)
Add an HttpHandler that will only respond to a certain path (e.g "/some/page").

This is shortcut for add(newPathMatchHandler(path, handler)).

See Also:
HttpHandler, add(HttpHandler), PathMatchHandler

add

WebServer add(String path,
              WebSocketHandler handler)
Add a WebSocketHandler for dealing with WebSockets.

This is shortcut for add(new PathMatchHandler(path, newHttpToWebSocketHandler(handler))).

See Also:
WebSocketHandler, HttpHandler, add(HttpHandler), HttpToWebSocketHandler, PathMatchHandler

add

WebServer add(String path,
              EventSourceHandler handler)
Add a WebSocketHandler for dealing with WebSockets.

This is shortcut for add(new PathMatchHandler(path, newHttpToEventSourceHandler(handler))).

See Also:
HttpHandler, add(HttpHandler), HttpToEventSourceHandler, PathMatchHandler

getPort

int getPort()
Get base port that webserver is serving on.


staleConnectionTimeout

WebServer staleConnectionTimeout(long millis)
Number of milliseconds before a stale HTTP keep-alive connection is closed by the server. A HTTP connection is considered stale if it remains open without sending more data within the timeout window.


setupSsl

WebServer setupSsl(InputStream keyStore,
                   String storePass,
                   String keyPass)
                   throws WebbitException
Setup SSL/TLS handler

Parameters:
keyStore - Keystore InputStream
storePass - Store password
keyPass - Key password
Returns:
current WebServer instance
Throws:
WebbitException - A problem loading the keystore
See Also:
#setupSsl(String, String, String)


Copyright © 2012. All Rights Reserved.