Search in sources :

Example 6 with ResourceHandler

use of org.mortbay.jetty.handler.ResourceHandler in project aws-doc-sdk-examples by awsdocs.

the class WebHandler method main.

public static void main(String[] args) throws Exception {
    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(8080);
    server.addConnector(connector);
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setWelcomeFiles(new String[] { "homepage.html" });
    resource_handler.setResourceBase("static/web/");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new WebHandler(new DummyRecommender()) });
    server.setHandler(handlers);
    server.start();
    if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
        Desktop.getDesktop().browse(new URI("http://localhost:8080"));
    }
    server.join();
}
Also used : HandlerList(org.mortbay.jetty.handler.HandlerList) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) Server(org.mortbay.jetty.Server) ResourceHandler(org.mortbay.jetty.handler.ResourceHandler) URI(java.net.URI)

Example 7 with ResourceHandler

use of org.mortbay.jetty.handler.ResourceHandler in project ambrose by twitter.

the class ScriptStatusServer method run.

/**
 * Run the server in the current thread.
 */
@Override
public void run() {
    // override newServerSocket to log local port once bound
    Connector connector = new SocketConnector() {

        @Override
        protected ServerSocket newServerSocket(String host, int port, int backlog) throws IOException {
            ServerSocket ss = super.newServerSocket(host, port, backlog);
            int localPort = ss.getLocalPort();
            LOG.info("Ambrose web server listening on port {}", localPort);
            LOG.info("Browse to http://localhost:{}/ to see job progress", localPort);
            return ss;
        }
    };
    connector.setPort(port);
    server = new Server();
    server.setConnectors(new Connector[] { connector });
    // this needs to be loaded via the jar'ed resources, not the relative dir
    String resourcePath = "com/twitter/ambrose/server/web";
    URL resourceUrl = checkNotNull(APIHandler.class.getClassLoader().getResource(resourcePath), "Failed to find resource '%s'", resourcePath);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setWelcomeFiles(new String[] { "workflow.html" });
    resourceHandler.setResourceBase(resourceUrl.toExternalForm());
    HandlerList handler = new HandlerList();
    handler.setHandlers(new Handler[] { resourceHandler, new APIHandler(workflowIndexReadService, statsReadService), new DefaultHandler() });
    server.setHandler(handler);
    server.setStopAtShutdown(false);
    try {
        server.start();
        server.join();
    } catch (Exception e) {
        LOG.error("Error launching ScriptStatusServer", e);
    }
}
Also used : HandlerList(org.mortbay.jetty.handler.HandlerList) Connector(org.mortbay.jetty.Connector) SocketConnector(org.mortbay.jetty.bio.SocketConnector) Server(org.mortbay.jetty.Server) ServerSocket(java.net.ServerSocket) ResourceHandler(org.mortbay.jetty.handler.ResourceHandler) SocketConnector(org.mortbay.jetty.bio.SocketConnector) URL(java.net.URL) IOException(java.io.IOException) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler)

Aggregations

Server (org.mortbay.jetty.Server)7 ResourceHandler (org.mortbay.jetty.handler.ResourceHandler)7 HandlerList (org.mortbay.jetty.handler.HandlerList)5 SocketConnector (org.mortbay.jetty.bio.SocketConnector)3 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URL (java.net.URL)2 Connector (org.mortbay.jetty.Connector)2 DefaultHandler (org.mortbay.jetty.handler.DefaultHandler)2 BrowserLaunchingInitializingException (edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException)1 UnsupportedOperatingSystemException (edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException)1 AWTException (java.awt.AWTException)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 CookieHandler (java.net.CookieHandler)1 MalformedURLException (java.net.MalformedURLException)1 ServerSocket (java.net.ServerSocket)1