Search in sources :

Example 71 with ServletContextHandler

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

the class SPARQLServer method buildServer.

// Later : private and in constructor.
private ServletContextHandler buildServer(String jettyConfig, boolean enableCompression) {
    if (jettyConfig != null) {
        // --jetty-config=jetty-fuseki.xml
        // for detailed configuration of the server using Jetty features.
        server = configServer(jettyConfig);
    } else
        server = defaultServerConfig(serverConfig.port, serverConfig.loopback);
    // Keep the server to a maximum number of threads.
    // server.setThreadPool(new QueuedThreadPool(ThreadPoolSize)) ;
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setErrorHandler(new FusekiErrorHandler());
    context.addEventListener(new FusekiServletContextListener(this));
    // Increase form size.
    context.getServletContext().getContextHandler().setMaxFormContentSize(10 * 1000 * 1000);
    // Wire up authentication if appropriate
    if (jettyConfig == null && serverConfig.authConfigFile != null) {
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(new String[] { "fuseki" });
        constraint.setAuthenticate(true);
        ConstraintMapping mapping = new ConstraintMapping();
        mapping.setConstraint(constraint);
        mapping.setPathSpec("/*");
        IdentityService identService = new DefaultIdentityService();
        ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
        securityHandler.addConstraintMapping(mapping);
        securityHandler.setIdentityService(identService);
        HashLoginService loginService = new HashLoginService("Fuseki Authentication", serverConfig.authConfigFile);
        loginService.setIdentityService(identService);
        securityHandler.setLoginService(loginService);
        securityHandler.setAuthenticator(new BasicAuthenticator());
        context.setSecurityHandler(securityHandler);
        serverLog.debug("Basic Auth Configuration = " + serverConfig.authConfigFile);
    }
    // Wire up context handler to server
    server.setHandler(context);
    // Constants. Add RDF types.
    MimeTypes mt = new MimeTypes();
    mt.addMimeMapping("rdf", WebContent.contentTypeRDFXML + ";charset=utf-8");
    mt.addMimeMapping("ttl", WebContent.contentTypeTurtle + ";charset=utf-8");
    mt.addMimeMapping("nt", WebContent.contentTypeNTriples + ";charset=ascii");
    mt.addMimeMapping("nq", WebContent.contentTypeNQuads + ";charset=ascii");
    mt.addMimeMapping("trig", WebContent.contentTypeTriG + ";charset=utf-8");
    // mt.addMimeMapping("tpl", "text/html;charset=utf-8") ;
    context.setMimeTypes(mt);
    server.setHandler(context);
    serverLog.debug("Pages = " + serverConfig.pages);
    boolean installManager = true;
    boolean installServices = true;
    String validationRoot = "/validate";
    if (installManager || installServices) {
        // TODO Respect port.
        if (serverConfig.pagesPort != serverConfig.port)
            serverLog.warn("Not supported yet - pages on a different port to services");
        if (serverConfig.pages != null) {
            if (!FileOps.exists(serverConfig.pages))
                serverLog.warn("No pages directory - " + serverConfig.pages);
            String base = serverConfig.pages;
            Map<String, Object> data = new HashMap<>();
            data.put("mgt", new MgtFunctions());
            SimpleVelocityServlet templateEngine = new SimpleVelocityServlet(base, data);
            addServlet(context, templateEngine, "*.tpl", false);
        }
    }
    if (installManager) {
        // Action when control panel selects a dataset.
        HttpServlet datasetChooser = new ActionDataset();
        addServlet(context, datasetChooser, PageNames.actionDatasetNames, false);
    }
    if (installServices) {
        // Validators
        HttpServlet validateQuery = new QueryValidator();
        HttpServlet validateUpdate = new UpdateValidator();
        HttpServlet validateData = new DataValidator();
        HttpServlet validateIRI = new IRIValidator();
        HttpServlet dumpService = new DumpServlet();
        HttpServlet generalQueryService = new SPARQL_QueryGeneral();
        addServlet(context, validateQuery, validationRoot + "/query", false);
        addServlet(context, validateUpdate, validationRoot + "/update", false);
        addServlet(context, validateData, validationRoot + "/data", false);
        addServlet(context, validateIRI, validationRoot + "/iri", false);
        // general query processor.
        addServlet(context, generalQueryService, HttpNames.ServiceGeneralQuery, enableCompression);
    }
    if (installManager || installServices) {
        String[] files = { "fuseki.html", "index.html" };
        context.setWelcomeFiles(files);
        addContent(context, "/", serverConfig.pages);
    }
    return context;
}
Also used : Constraint(org.eclipse.jetty.util.security.Constraint) MgtFunctions(org.apache.jena.fuseki.mgt.MgtFunctions) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) IRIValidator(org.apache.jena.fuseki.validation.IRIValidator) ActionDataset(org.apache.jena.fuseki.mgt.ActionDataset) HttpServlet(javax.servlet.http.HttpServlet) MimeTypes(org.eclipse.jetty.http.MimeTypes) UpdateValidator(org.apache.jena.fuseki.validation.UpdateValidator) QueryValidator(org.apache.jena.fuseki.validation.QueryValidator) DataValidator(org.apache.jena.fuseki.validation.DataValidator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 72 with ServletContextHandler

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

the class ManagementServer method createManagementServer.

public static Server createManagementServer(int mgtPort) {
    Fuseki.serverLog.info("Adding management functions");
    // Separate Jetty server
    Server server = new Server();
    //        BlockingChannelConnector bcConnector = new BlockingChannelConnector() ;
    //        bcConnector.setUseDirectBuffers(false) ;
    //        Connector connector = bcConnector ;
    Connector connector = new SelectChannelConnector();
    // Ignore idle time. 
    // If set, then if this goes off, it keeps going off and you get a lot of log messages.
    // Jetty outputs a lot of messages if this goes off.
    connector.setMaxIdleTime(0);
    connector.setPort(mgtPort);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setErrorHandler(new FusekiErrorHandler());
    server.setHandler(context);
    // Add the server control servlet
    addServlet(context, new MgtCmdServlet(), "/mgt");
    addServlet(context, new DumpServlet(), "/dump");
    addServlet(context, new StatsServlet(), "/stats");
    addServlet(context, new PingServlet(), "/ping");
    return server;
// Old plan
//      // Development : server control panel.
//      addServlet(context, new ServerServlet(), "/server") ;
//      addServlet(context, new ActionBackup(), "/backup") ;
}
Also used : Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) DumpServlet(org.apache.jena.fuseki.servlets.DumpServlet) FusekiErrorHandler(org.apache.jena.fuseki.server.FusekiErrorHandler) Server(org.eclipse.jetty.server.Server) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 73 with ServletContextHandler

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

the class VirtualInstance method startJetty.

public synchronized void startJetty() throws Throwable {
    if (jettyServer != null) {
        return;
    }
    servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath("/");
    TopologyConnectorServlet servlet = new TopologyConnectorServlet();
    PrivateAccessor.setField(servlet, "config", config);
    PrivateAccessor.setField(servlet, "clusterViewService", clusterViewService);
    PrivateAccessor.setField(servlet, "announcementRegistry", announcementRegistry);
    Mockery context = new JUnit4Mockery();
    final HttpService httpService = context.mock(HttpService.class);
    context.checking(new Expectations() {

        {
            allowing(httpService).registerServlet(with(any(String.class)), with(any(Servlet.class)), with(any(Dictionary.class)), with(any(HttpContext.class)));
        }
    });
    PrivateAccessor.setField(servlet, "httpService", httpService);
    ComponentContext cc = null;
    PrivateAccessor.invoke(servlet, "activate", new Class[] { ComponentContext.class }, new Object[] { cc });
    ServletHolder holder = new ServletHolder(servlet);
    servletContext.addServlet(holder, "/system/console/topology/*");
    jettyServer = new Server();
    jettyServer.setHandler(servletContext);
    Connector connector = new SelectChannelConnector();
    jettyServer.setConnectors(new Connector[] { connector });
    jettyServer.start();
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) ComponentContext(org.osgi.service.component.ComponentContext) Server(org.eclipse.jetty.server.Server) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpContext(org.osgi.service.http.HttpContext) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) HttpService(org.osgi.service.http.HttpService) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) Servlet(javax.servlet.Servlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 74 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project spring-framework by spring-projects.

the class JettyHttpServer method initServer.

@Override
protected void initServer() throws Exception {
    this.jettyServer = new Server();
    ServletHttpHandlerAdapter servlet = createServletAdapter();
    ServletHolder servletHolder = new ServletHolder(servlet);
    this.contextHandler = new ServletContextHandler(this.jettyServer, "", false, false);
    this.contextHandler.addServlet(servletHolder, "/");
    this.contextHandler.start();
    ServerConnector connector = new ServerConnector(this.jettyServer);
    connector.setHost(getHost());
    connector.setPort(getPort());
    this.jettyServer.addConnector(connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 75 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project rest.li by linkedin.

the class HttpServerBuilder method build.

public Server build() {
    Server server = new Server();
    // HTTP Configuration
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.setSendXPoweredBy(true);
    configuration.setSendServerVersion(true);
    configuration.setSendXPoweredBy(false);
    configuration.setSendServerVersion(false);
    configuration.setSendDateHeader(false);
    // HTTP Connector
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(configuration), new HTTP2CServerConnectionFactory(configuration));
    http.setIdleTimeout(_idleTimeout);
    http.setPort(HTTP_PORT);
    server.addConnector(http);
    ServletContextHandler handler = new ServletContextHandler(server, "");
    handler.addServlet(new ServletHolder(new HttpServlet() {

        private static final long serialVersionUID = 0;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        private void addStatus(HttpServletResponse resp) throws IOException {
            resp.setStatus(_status);
        }

        private void addHeader(HttpServletResponse resp) throws IOException {
            if (_headerSize <= 0) {
                return;
            }
            int valueSize = _headerSize - HEADER_NAME.length();
            char[] headerValue = new char[valueSize];
            Arrays.fill(headerValue, 'a');
            resp.addHeader(HEADER_NAME, new String(headerValue));
        }

        private void addContent(HttpServletResponse resp) throws IOException {
            if (_responseSize <= 0) {
                return;
            }
            char[] content = new char[_responseSize];
            Arrays.fill(content, 'a');
            resp.getWriter().write(content);
        }

        private void awaitLatch() {
            if (_responseLatch != null) {
                try {
                    _responseLatch.await(RESPONSE_LATCH_TIMEOUT, RESPONSE_LATCH_TIMEUNIT);
                } catch (InterruptedException e) {
                }
            }
        }

        private void readEntity(BufferedReader reader) throws IOException {
            while (true) {
                char[] bytes = new char[8192];
                int read = reader.read(bytes);
                if (read < 0) {
                    break;
                }
            }
        }
    }), "/*");
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) HTTP2CServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory) BufferedReader(java.io.BufferedReader) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

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