Search in sources :

Example 1 with ResourceHandler

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

the class RestfulServer method buildResourceHandler.

private ResourceHandler buildResourceHandler(final String resourcePath) throws Exception {
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setBaseResource(Resource.newClassPathResource(resourcePath));
    return resourceHandler;
}
Also used : ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 2 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler 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 3 with ResourceHandler

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

the class DispatcherTest method init.

@Before
public void init() throws Exception {
    _server = new Server();
    _connector = new LocalConnector(_server);
    _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
    _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
    _contextCollection = new ContextHandlerCollection();
    _contextHandler = new ServletContextHandler();
    _contextHandler.setContextPath("/context");
    _contextCollection.addHandler(_contextHandler);
    _resourceHandler = new ResourceHandler();
    _resourceHandler.setResourceBase(MavenTestingUtils.getTestResourceDir("dispatchResourceTest").getAbsolutePath());
    _resourceHandler.setPathInfoOnly(true);
    ContextHandler resourceContextHandler = new ContextHandler("/resource");
    resourceContextHandler.setHandler(_resourceHandler);
    _contextCollection.addHandler(resourceContextHandler);
    _server.setHandler(_contextCollection);
    _server.addConnector(_connector);
    _server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) Before(org.junit.Before)

Example 4 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project buck by facebook.

the class WebServerTest method testCreateHandlersCoversExpectedContextPaths.

@Test
public void testCreateHandlersCoversExpectedContextPaths() {
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    WebServer webServer = new WebServer(/* port */
    9999, projectFilesystem, "/static/", ObjectMappers.newDefaultInstance());
    ImmutableList<ContextHandler> handlers = webServer.createHandlers();
    final Map<String, ContextHandler> contextPathToHandler = Maps.newHashMap();
    for (ContextHandler handler : handlers) {
        contextPathToHandler.put(handler.getContextPath(), handler);
    }
    Function<String, TemplateHandlerDelegate> getDelegate = contextPath -> ((TemplateHandler) contextPathToHandler.get(contextPath).getHandler()).getDelegate();
    assertTrue(getDelegate.apply("/") instanceof IndexHandlerDelegate);
    assertTrue(contextPathToHandler.get("/static").getHandler() instanceof ResourceHandler);
    assertTrue(getDelegate.apply("/trace") instanceof TraceHandlerDelegate);
    assertTrue(getDelegate.apply("/traces") instanceof TracesHandlerDelegate);
    assertTrue(contextPathToHandler.get("/tracedata").getHandler() instanceof TraceDataHandler);
}
Also used : ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) Function(com.google.common.base.Function) ImmutableList(com.google.common.collect.ImmutableList) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Map(java.util.Map) Assert.assertTrue(org.junit.Assert.assertTrue) ObjectMappers(com.facebook.buck.util.ObjectMappers) Test(org.junit.Test) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Maps(com.google.common.collect.Maps) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Example 5 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project nifi by apache.

the class JettyServer method createDocsWebApp.

private ContextHandler createDocsWebApp(final String contextPath) {
    try {
        final ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        final File docsDir = getDocsDir("docs");
        final Resource docsResource = Resource.newResource(docsDir);
        // load the component documentation working directory
        final File componentDocsDirPath = props.getComponentDocumentationWorkingDirectory();
        final File workingDocsDirectory = getWorkingDocsDirectory(componentDocsDirPath);
        final Resource workingDocsResource = Resource.newResource(workingDocsDirectory);
        final File webApiDocsDir = getWebApiDocsDir();
        final Resource webApiDocsResource = Resource.newResource(webApiDocsDir);
        // create resources for both docs locations
        final ResourceCollection resources = new ResourceCollection(docsResource, workingDocsResource, webApiDocsResource);
        resourceHandler.setBaseResource(resources);
        // create the context handler
        final ContextHandler handler = new ContextHandler(contextPath);
        handler.setHandler(resourceHandler);
        logger.info("Loading documents web app with context path set to " + contextPath);
        return handler;
    } catch (Exception ex) {
        logger.error("Unhandled Exception in createDocsWebApp: " + ex.getMessage());
        startUpFailure(ex);
        // required by compiler, though never be executed.
        return null;
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Resource(org.eclipse.jetty.util.resource.Resource) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) JarFile(java.util.jar.JarFile) File(java.io.File) ServletException(javax.servlet.ServletException) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) SocketException(java.net.SocketException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Aggregations

ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)53 Server (org.eclipse.jetty.server.Server)31 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)20 HandlerList (org.eclipse.jetty.server.handler.HandlerList)20 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)16 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)15 ServerConnector (org.eclipse.jetty.server.ServerConnector)14 File (java.io.File)13 IOException (java.io.IOException)8 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 HashLoginService (org.eclipse.jetty.security.HashLoginService)5 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)5 Constraint (org.eclipse.jetty.util.security.Constraint)5 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)4 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)4 Handler (org.eclipse.jetty.server.Handler)4 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)4 Path (java.nio.file.Path)3