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();
}
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();
}
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();
}
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();
}
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"));
}
Aggregations