Search in sources :

Example 91 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class CometdComponent method createServletForConnector.

protected CometDServlet createServletForConnector(Server server, Connector connector, CometdEndpoint endpoint) throws Exception {
    CometDServlet servlet = new CometDServlet();
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
    ServletHolder holder = new ServletHolder();
    holder.setServlet(servlet);
    holder.setAsyncSupported(true);
    // pointing to by example classpath:webapp
    if (endpoint.getBaseResource() != null) {
        String[] resources = endpoint.getBaseResource().split(":");
        if (LOG.isDebugEnabled()) {
            LOG.debug(">>> Protocol found: " + resources[0] + ", and resource: " + resources[1]);
        }
        if (resources[0].equals("file")) {
            context.setBaseResource(Resource.newResource(resources[1]));
        } else if (resources[0].equals("classpath")) {
            // Create a URL handler using classpath protocol
            URL url = this.getCamelContext().getClassResolver().loadResourceAsURL(resources[1]);
            context.setBaseResource(Resource.newResource(url));
        }
    }
    applyCrossOriginFiltering(endpoint, context);
    context.addServlet(holder, "/cometd/*");
    context.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/");
    context.setSessionHandler(new SessionHandler());
    holder.setInitParameter("timeout", Integer.toString(endpoint.getTimeout()));
    holder.setInitParameter("interval", Integer.toString(endpoint.getInterval()));
    holder.setInitParameter("maxInterval", Integer.toString(endpoint.getMaxInterval()));
    holder.setInitParameter("multiFrameInterval", Integer.toString(endpoint.getMultiFrameInterval()));
    holder.setInitParameter("JSONCommented", Boolean.toString(endpoint.isJsonCommented()));
    holder.setInitParameter("logLevel", Integer.toString(endpoint.getLogLevel()));
    return servlet;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CometDServlet(org.cometd.server.CometDServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URL(java.net.URL)

Example 92 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class CometdCrossOriginConsumerTest method testCrossOriginFilterNotAddedWhenOff.

@Test
public void testCrossOriginFilterNotAddedWhenOff() throws Exception {
    // setup
    CometdComponent component = context.getComponent("cometd", CometdComponent.class);
    Server server = new Server();
    when(endpoint.isCrossOriginFilterOn()).thenReturn(false);
    // act
    component.createServletForConnector(server, connector, endpoint);
    // assert
    ServletContextHandler handler = (ServletContextHandler) server.getHandler();
    assertEquals(0, handler.getServletHandler().getFilters().length);
}
Also used : Server(org.eclipse.jetty.server.Server) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 93 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class CometdCrossOriginConsumerTest method testCrossOriginFilterAddedWhenOn.

@Test
public void testCrossOriginFilterAddedWhenOn() throws Exception {
    // setup
    CometdComponent component = context.getComponent("cometd", CometdComponent.class);
    Server server = new Server();
    when(endpoint.isCrossOriginFilterOn()).thenReturn(true);
    when(endpoint.getFilterPath()).thenReturn(FILTER_PATH);
    when(endpoint.getAllowedOrigins()).thenReturn(ALLOWED_ORIGINS);
    // act
    component.createServletForConnector(server, connector, endpoint);
    // assert
    ServletContextHandler handler = (ServletContextHandler) server.getHandler();
    assertEquals(1, handler.getServletHandler().getFilters().length);
    FilterHolder filterHolder = handler.getServletHandler().getFilters()[0];
    Filter filter = filterHolder.getFilter();
    assertTrue(filter instanceof CrossOriginFilter);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) Filter(javax.servlet.Filter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) Test(org.junit.Test)

Example 94 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project coprhd-controller by CoprHD.

the class AuthenticationServerImpl method initServer.

@Override
protected void initServer() throws Exception {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String authDocumentRoot = loader.getResource(AUTH_DOCUMENT_ROOT).toString();
    _server = new Server();
    initConnectors();
    // Static Pages
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setWelcomeFiles(new String[] { "*" });
    resourceHandler.setResourceBase(authDocumentRoot);
    // AuthN servlet filters
    ServletContextHandler rootHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    rootHandler.setContextPath("/");
    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.setHandlers(new Handler[] { resourceHandler, rootHandler });
    _server.setHandler(handlerCollection);
    ((AbstractSessionManager) rootHandler.getSessionHandler().getSessionManager()).setUsingCookies(false);
    final FilterHolder securityFilterHolder = new FilterHolder(new DelegatingFilterProxy(_secFilters));
    rootHandler.addFilter(securityFilterHolder, "/*", FilterMapping.REQUEST);
    // Add the REST resources
    if (_app != null) {
        ResourceConfig config = new DefaultResourceConfig();
        config.add(_app);
        Map<String, MediaType> type = config.getMediaTypeMappings();
        type.put("json", MediaType.APPLICATION_JSON_TYPE);
        type.put("xml", MediaType.APPLICATION_XML_TYPE);
        rootHandler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
    }
    // load trust store from file to zk. must do it before authmgr started, who holds the connection with ad.
    loadTrustStoreFromLocalFiles();
    _dbClient.start();
    _tokenManager.init();
    _authManager.init();
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) AbstractSecuredWebServer(com.emc.storageos.security.AbstractSecuredWebServer) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) ServletContainer(com.sun.jersey.spi.container.servlet.ServletContainer) MediaType(javax.ws.rs.core.MediaType) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) AbstractSessionManager(org.eclipse.jetty.server.session.AbstractSessionManager)

Example 95 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project nifi by apache.

the class JettyWebSocketServer method startServer.

@OnEnabled
@Override
public void startServer(final ConfigurationContext context) throws Exception {
    if (server != null && server.isRunning()) {
        getLogger().info("A WebSocket server is already running. {}", new Object[] { server });
        return;
    }
    configuredPolicy = WebSocketPolicy.newServerPolicy();
    configurePolicy(context, configuredPolicy);
    server = new Server();
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
    final ServletContextHandler contextHandler = new ServletContextHandler();
    servletHandler = new ServletHandler();
    contextHandler.insertHandler(servletHandler);
    handlerCollection.setHandlers(new Handler[] { contextHandler });
    server.setHandler(handlerCollection);
    listenPort = context.getProperty(LISTEN_PORT).asInteger();
    final SslContextFactory sslContextFactory = createSslFactory(context);
    final ServerConnector serverConnector = createConnector(sslContextFactory, listenPort);
    server.setConnectors(new Connector[] { serverConnector });
    servletHandler.addServletWithMapping(JettyWebSocketServlet.class, "/*");
    getLogger().info("Starting JettyWebSocketServer on port {}.", new Object[] { listenPort });
    server.start();
    portToControllerService.put(listenPort, this);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)390 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)253 Server (org.eclipse.jetty.server.Server)217 ServerConnector (org.eclipse.jetty.server.ServerConnector)98 Test (org.junit.Test)70 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)56 IOException (java.io.IOException)42 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HttpClient (org.eclipse.jetty.client.HttpClient)39 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)39 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)37 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)37 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)32 Connector (org.eclipse.jetty.server.Connector)29 Request (org.eclipse.jetty.client.api.Request)27 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)24 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)24 ServletException (javax.servlet.ServletException)22 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)22