Search in sources :

Example 6 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project elastic-job by dangdangdotcom.

the class RestfulServer method start.

/**
     * 启动内嵌的RESTful服务器.
     * 
     * @param packages RESTful实现类所在包
     * @param resourcePath 资源路径
     * @throws Exception 启动服务器异常
     */
public void start(final String packages, final Optional<String> resourcePath) throws Exception {
    log.info("Elastic Job: Start RESTful server");
    HandlerList handlers = new HandlerList();
    if (resourcePath.isPresent()) {
        handlers.addHandler(buildResourceHandler(resourcePath.get()));
    }
    handlers.addHandler(buildServletContextHandler(packages));
    server.setHandler(handlers);
    server.start();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList)

Example 7 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class FileServer method main.

public static void main(String[] args) throws Exception {
    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);
    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase(".");
    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);
    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 8 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class JarServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler();
    Resource.setDefaultUseCaches(true);
    Resource base = Resource.newResource("jar:file:src/main/resources/content.jar!/");
    context.setBaseResource(base);
    context.addServlet(new ServletHolder(new DefaultServlet()), "/");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Resource(org.eclipse.jetty.util.resource.Resource) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 9 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class ManyHandlers method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    // create the handlers
    Handler param = new ParamHandler();
    HandlerWrapper wrapper = new WelcomeWrapHandler();
    Handler hello = new HelloHandler();
    Handler dft = new DefaultHandler();
    RequestLogHandler requestLog = new RequestLogHandler();
    // configure request logging
    File requestLogFile = File.createTempFile("demo", "log");
    NCSARequestLog ncsaLog = new NCSARequestLog(requestLogFile.getAbsolutePath());
    requestLog.setRequestLog(ncsaLog);
    // create the handler collections
    HandlerCollection handlers = new HandlerCollection();
    HandlerList list = new HandlerList();
    // link them all together
    wrapper.setHandler(hello);
    list.setHandlers(new Handler[] { param, new GzipHandler(), dft });
    handlers.setHandlers(new Handler[] { list, requestLog });
    // Handler tree looks like the following
    // <pre>
    // Server
    // + HandlerCollection
    // . + HandlerList
    // . | + param (ParamHandler)
    // . | + wrapper (WelcomeWrapHandler)
    // . | | \ hello (HelloHandler)
    // . | \ dft (DefaultHandler)
    // . \ requestLog (RequestLogHandler)
    // </pre>
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) File(java.io.File)

Example 10 with HandlerList

use of org.eclipse.jetty.server.handler.HandlerList in project jetty.project by eclipse.

the class ServletContextHandlerTest method testFallThrough.

@Test
public void testFallThrough() throws Exception {
    HandlerList list = new HandlerList();
    _server.setHandler(list);
    ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS);
    ServletHandler servlet = root.getServletHandler();
    servlet.setEnsureDefaultServlet(false);
    servlet.addServletWithMapping(HelloServlet.class, "/hello/*");
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.sendError(404, "Fell Through");
        }
    });
    _server.start();
    String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("200 OK"));
    response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Aggregations

HandlerList (org.eclipse.jetty.server.handler.HandlerList)35 Server (org.eclipse.jetty.server.Server)19 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)17 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)13 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)11 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)8 Handler (org.eclipse.jetty.server.Handler)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 File (java.io.File)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3