Search in sources :

Example 31 with ResourceHandler

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

the class WebService method addStaticResources.

public void addStaticResources(String basePath, String resourcePath) {
    ContextHandler capHandler = new ContextHandler();
    capHandler.setContextPath(basePath);
    ResourceHandler resHandler = new ResourceHandler();
    resHandler.setBaseResource(Resource.newClassPathResource(resourcePath));
    resHandler.setEtags(true);
    resHandler.setCacheControl(WebService.HANDLER_CACHE_CONTROL);
    capHandler.setHandler(resHandler);
    handlers.add(capHandler);
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 32 with ResourceHandler

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

the class SLSWebApp method start.

public void start() throws Exception {
    // static files
    final ResourceHandler staticHandler = new ResourceHandler();
    staticHandler.setMimeTypes(new MimeTypes());
    staticHandler.setResourceBase("html");
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                // timeunit
                // second, divide millionsecond / 1000
                int timeunit = 1000;
                String timeunitLabel = "second";
                if (request.getParameter("u") != null && request.getParameter("u").equalsIgnoreCase("m")) {
                    timeunit = 1000 * 60;
                    timeunitLabel = "minute";
                }
                // http request
                if (target.equals("/")) {
                    printPageIndex(request, response);
                } else if (target.equals("/simulate")) {
                    printPageSimulate(request, response, timeunit, timeunitLabel);
                } else if (target.equals("/track")) {
                    printPageTrack(request, response, timeunit, timeunitLabel);
                } else // js/css request
                if (target.startsWith("/js") || target.startsWith("/css")) {
                    response.setCharacterEncoding("utf-8");
                    staticHandler.handle(target, baseRequest, request, response);
                } else // json request
                if (target.equals("/simulateMetrics")) {
                    printJsonMetrics(request, response);
                } else if (target.equals("/trackMetrics")) {
                    printJsonTrack(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    server = new Server(port);
    server.setHandler(handler);
    server.start();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) MimeTypes(org.eclipse.jetty.http.MimeTypes) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 33 with ResourceHandler

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

the class HttpConnectorTest method startJetty.

private static Server startJetty() throws Exception {
    Server server = new Server();
    // Create the login service
    String REQUIRED_ROLE = "users";
    HashLoginService loginSvc = new HashLoginService(REQUIRED_ROLE, USER_ROLE_FILE);
    server.addBean(loginSvc);
    // Start HTTP and HTTPS connectors
    SelectChannelConnector httpConnector = new SelectChannelConnector();
    httpConnector.setPort(0);
    httpConnector.setHost(LOCALHOST);
    server.addConnector(httpConnector);
    SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
    sslConnector.setPort(0);
    sslConnector.setHost(LOCALHOST);
    SslContextFactory contextFactory = sslConnector.getSslContextFactory();
    contextFactory.setKeyStorePath(KEYSTORE_PATH);
    contextFactory.setKeyStorePassword(KEYSTORE_PASS);
    server.addConnector(sslConnector);
    // Create the resource handler to serve files
    ResourceHandler resourceHandler = new ETaggingResourceHandler();
    resourceHandler.setResourceBase(RESOURCE_BASE);
    resourceHandler.setDirectoriesListed(true);
    // Setup user role constraints
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(new String[] { REQUIRED_ROLE });
    constraint.setAuthenticate(true);
    // Map constraints to the secured directory
    ConstraintMapping cm = new ConstraintMapping();
    cm.setConstraint(constraint);
    cm.setPathSpec(SECURED_PATH);
    // Setup the constraint handler
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    securityHandler.setAuthMethod("BASIC");
    securityHandler.setHandler(resourceHandler);
    securityHandler.setLoginService(loginSvc);
    securityHandler.setConstraintMappings(new ConstraintMapping[] { cm });
    // Finally!! Start the server
    server.setHandler(securityHandler);
    server.start();
    while (!server.isRunning()) {
        Thread.sleep(10);
    }
    HTTP_PORT = httpConnector.getLocalPort();
    HTTPS_PORT = sslConnector.getLocalPort();
    assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTP_PORT));
    assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTP_PORT));
    assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTPS_PORT));
    assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTPS_PORT));
    assertNotSame(Integer.valueOf(HTTP_PORT), Integer.valueOf(HTTPS_PORT));
    return server;
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Server(org.eclipse.jetty.server.Server) ETaggingResourceHandler(test.http.ETaggingResourceHandler) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ETaggingResourceHandler(test.http.ETaggingResourceHandler) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector)

Example 34 with ResourceHandler

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

the class AtlasAPIV2ServerEmulator method createServer.

private void createServer() throws Exception {
    server = new Server();
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
    final ServletContextHandler staticContext = new ServletContextHandler();
    staticContext.setContextPath("/");
    final ServletContextHandler atlasApiV2Context = new ServletContextHandler();
    atlasApiV2Context.setContextPath("/api/atlas/v2/");
    handlerCollection.setHandlers(new Handler[] { staticContext, atlasApiV2Context });
    server.setHandler(handlerCollection);
    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setBaseResource(Resource.newClassPathResource("public", false, false));
    staticContext.setHandler(resourceHandler);
    final ServletHandler servletHandler = new ServletHandler();
    atlasApiV2Context.insertHandler(servletHandler);
    httpConnector = new ServerConnector(server);
    httpConnector.setPort(21000);
    server.setConnectors(new Connector[] { httpConnector });
    servletHandler.addServletWithMapping(TypeDefsServlet.class, "/types/typedefs/");
    servletHandler.addServletWithMapping(EntityBulkServlet.class, "/entity/bulk/");
    servletHandler.addServletWithMapping(EntityGuidServlet.class, "/entity/guid/*");
    servletHandler.addServletWithMapping(SearchByUniqueAttributeServlet.class, "/entity/uniqueAttribute/type/*");
    servletHandler.addServletWithMapping(SearchBasicServlet.class, "/search/basic/");
    servletHandler.addServletWithMapping(LineageServlet.class, "/debug/lineage/");
    notificationServerEmulator = new AtlasNotificationServerEmulator();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 35 with ResourceHandler

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

the class WebServer method initWebApp.

private void initWebApp() {
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(config.getString("web.path"));
    if (config.getBoolean("web.debug")) {
        resourceHandler.setWelcomeFiles(new String[] { "debug.html", "index.html" });
        // avoid locking files on Windows
        resourceHandler.setMinMemoryMappedContentLength(-1);
    } else {
        String cache = config.getString("web.cacheControl");
        if (cache != null && !cache.isEmpty()) {
            resourceHandler.setCacheControl(cache);
        }
        resourceHandler.setWelcomeFiles(new String[] { "release.html", "index.html" });
    }
    handlers.addHandler(resourceHandler);
}
Also used : ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

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