Search in sources :

Example 1 with ETaggingResourceHandler

use of test.http.ETaggingResourceHandler 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)

Aggregations

ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)1 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)1 HashLoginService (org.eclipse.jetty.security.HashLoginService)1 Server (org.eclipse.jetty.server.Server)1 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)1 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)1 SslSelectChannelConnector (org.eclipse.jetty.server.ssl.SslSelectChannelConnector)1 Constraint (org.eclipse.jetty.util.security.Constraint)1 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)1 ETaggingResourceHandler (test.http.ETaggingResourceHandler)1