Search in sources :

Example 16 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project http-request by kevinsawicki.

the class ServerTestCase method setUp.

/**
   * Set up server with handler
   *
   * @param handler
   * @return port
   * @throws Exception
   */
public static String setUp(final Handler handler) throws Exception {
    server = new Server();
    if (handler != null)
        server.setHandler(handler);
    Connector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    server.start();
    proxy = new Server();
    Connector proxyConnector = new SelectChannelConnector();
    proxyConnector.setPort(0);
    proxy.setConnectors(new Connector[] { proxyConnector });
    ServletHandler proxyHandler = new ServletHandler();
    RequestHandler proxyCountingHandler = new RequestHandler() {

        @Override
        public void handle(Request request, HttpServletResponse response) {
            proxyHitCount.incrementAndGet();
            String auth = request.getHeader("Proxy-Authorization");
            auth = auth.substring(auth.indexOf(' ') + 1);
            try {
                auth = B64Code.decode(auth, CHARSET_UTF8);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            int colon = auth.indexOf(':');
            proxyUser.set(auth.substring(0, colon));
            proxyPassword.set(auth.substring(colon + 1));
            request.setHandled(false);
        }
    };
    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(proxyCountingHandler);
    handlerList.addHandler(proxyHandler);
    proxy.setHandler(handlerList);
    ServletHolder proxyHolder = proxyHandler.addServletWithMapping("org.eclipse.jetty.servlets.ProxyServlet", "/");
    proxyHolder.setAsyncSupported(true);
    proxy.start();
    proxyPort = proxyConnector.getLocalPort();
    return "http://localhost:" + connector.getLocalPort();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector)

Example 17 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project saga by timurstrekalov.

the class InstrumentingProxyServer method start.

public int start() {
    final SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.addConnector(connector);
    final HandlerCollection handlers = new HandlerCollection();
    // Setup proxy servlet
    final ServletContextHandler context = new ServletContextHandler(handlers, "/");
    context.setAttribute(InstrumentingProxyServlet.INSTRUMENTER, instrumenter);
    context.addServlet(new ServletHolder(InstrumentingProxyServlet.class), "/*");
    // Setup proxy handler to handle CONNECT methods
    final ConnectHandler connectProxy = new ConnectHandler();
    handlers.addHandler(connectProxy);
    server.setHandler(handlers);
    try {
        server.start();
        logger.info("Proxy server started on port {}", connector.getLocalPort());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    return connector.getLocalPort();
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) ConnectHandler(org.eclipse.jetty.server.handler.ConnectHandler) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 18 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project saga by timurstrekalov.

the class FileServer method start.

public int start() {
    final SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.addConnector(connector);
    final HandlerCollection handlers = new HandlerCollection();
    final ResourceHandler resourceHandler = new ResourceHandler() {

        @Override
        protected void doResponseHeaders(final HttpServletResponse response, final Resource resource, final String mimeType) {
            response.addDateHeader("Expires", 0L);
        }
    };
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setResourceBase(resourceBase);
    handlers.addHandler(resourceHandler);
    server.setHandler(handlers);
    try {
        server.start();
        logger.info("File server started on port {}", connector.getLocalPort());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    return connector.getLocalPort();
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Resource(org.eclipse.jetty.util.resource.Resource) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler)

Example 19 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector 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

SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)19 Server (org.eclipse.jetty.server.Server)14 Connector (org.eclipse.jetty.server.Connector)8 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)5 IOException (java.io.IOException)4 SslSelectChannelConnector (org.eclipse.jetty.server.ssl.SslSelectChannelConnector)4 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)3 UnknownHostException (java.net.UnknownHostException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)2 ServiceBindException (co.cask.cdap.common.ServiceBindException)1 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)1 AgentException (com.axway.ats.agentapp.standalone.exceptions.AgentException)1 Injector (com.google.inject.Injector)1 GuiceServletContextListener (com.google.inject.servlet.GuiceServletContextListener)1