Search in sources :

Example 21 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project activemq-artemis by apache.

the class WebServerComponent method configure.

@Override
public void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception {
    webServerConfig = (WebServerDTO) config;
    uri = new URI(webServerConfig.bind);
    server = new Server();
    String scheme = uri.getScheme();
    if ("https".equals(scheme)) {
        SslContextFactory sslFactory = new SslContextFactory();
        sslFactory.setKeyStorePath(webServerConfig.keyStorePath == null ? artemisInstance + "/etc/keystore.jks" : webServerConfig.keyStorePath);
        sslFactory.setKeyStorePassword(webServerConfig.getKeyStorePassword() == null ? "password" : webServerConfig.getKeyStorePassword());
        if (webServerConfig.clientAuth != null) {
            sslFactory.setNeedClientAuth(webServerConfig.clientAuth);
            if (webServerConfig.clientAuth) {
                sslFactory.setTrustStorePath(webServerConfig.trustStorePath);
                sslFactory.setTrustStorePassword(webServerConfig.getTrustStorePassword());
            }
        }
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslFactory, "HTTP/1.1");
        HttpConfiguration https = new HttpConfiguration();
        https.addCustomizer(new SecureRequestCustomizer());
        HttpConnectionFactory httpFactory = new HttpConnectionFactory(https);
        connector = new ServerConnector(server, sslConnectionFactory, httpFactory);
    } else {
        connector = new ServerConnector(server);
    }
    connector.setPort(uri.getPort());
    connector.setHost(uri.getHost());
    server.setConnectors(new Connector[] { connector });
    handlers = new HandlerList();
    Path homeWarDir = Paths.get(artemisHome != null ? artemisHome : ".").resolve(webServerConfig.path).toAbsolutePath();
    Path instanceWarDir = Paths.get(artemisInstance != null ? artemisInstance : ".").resolve(webServerConfig.path).toAbsolutePath();
    if (webServerConfig.apps != null && webServerConfig.apps.size() > 0) {
        webContexts = new ArrayList<>();
        for (AppDTO app : webServerConfig.apps) {
            Path dirToUse = homeWarDir;
            if (new File(instanceWarDir.toFile().toString() + File.separator + app.war).exists()) {
                dirToUse = instanceWarDir;
            }
            WebAppContext webContext = deployWar(app.url, app.war, dirToUse);
            webContexts.add(webContext);
            if (app.war.startsWith("console")) {
                consoleUrl = webServerConfig.bind + "/" + app.url;
            }
        }
    }
    ResourceHandler homeResourceHandler = new ResourceHandler();
    homeResourceHandler.setResourceBase(homeWarDir.toString());
    homeResourceHandler.setDirectoriesListed(false);
    homeResourceHandler.setWelcomeFiles(new String[] { "index.html" });
    ContextHandler homeContext = new ContextHandler();
    homeContext.setContextPath("/");
    homeContext.setResourceBase(homeWarDir.toString());
    homeContext.setHandler(homeResourceHandler);
    homeContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    ResourceHandler instanceResourceHandler = new ResourceHandler();
    instanceResourceHandler.setResourceBase(instanceWarDir.toString());
    instanceResourceHandler.setDirectoriesListed(false);
    instanceResourceHandler.setWelcomeFiles(new String[] { "index.html" });
    ContextHandler instanceContext = new ContextHandler();
    instanceContext.setContextPath("/");
    instanceContext.setResourceBase(instanceWarDir.toString());
    instanceContext.setHandler(instanceResourceHandler);
    homeContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setServeIcon(false);
    handlers.addHandler(homeContext);
    handlers.addHandler(instanceContext);
    handlers.addHandler(defaultHandler);
    server.setHandler(handlers);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Path(java.nio.file.Path) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) AppDTO(org.apache.activemq.artemis.dto.AppDTO) File(java.io.File)

Example 22 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler 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 23 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project parseq by linkedin.

the class TracevisServer method start.

public void start() throws Exception {
    LOG.info("TracevisServer base location: " + _staticContentLocation + ", heapster location: " + _heapsterContentLocation);
    LOG.info("Starting TracevisServer on port: " + _port + ", graphviz location: " + _dotLocation + ", cache size: " + _cacheSize + ", graphviz timeout: " + _timeoutMs + "ms");
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    final Engine engine = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler).build();
    Files.createDirectories(_cacheLocation);
    for (File f : _cacheLocation.toFile().listFiles()) {
        f.delete();
    }
    _graphvizEngine.start();
    Server server = new Server();
    server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", -1);
    server.setConnectors(getConnectors(server));
    TracePostHandler tracePostHandler = new TracePostHandler(_staticContentLocation.toString());
    ResourceHandler traceHandler = new ResourceHandler();
    traceHandler.setDirectoriesListed(true);
    traceHandler.setWelcomeFiles(new String[] { "trace.html" });
    traceHandler.setResourceBase(_staticContentLocation.toString());
    ResourceHandler heapsterHandler = new ResourceHandler();
    heapsterHandler.setDirectoriesListed(true);
    heapsterHandler.setResourceBase(_heapsterContentLocation.toString());
    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { new DotHandler(_graphvizEngine, engine), new JhatHandler(engine), tracePostHandler, traceHandler, new HealthCheckHandler(), heapsterHandler, new DefaultHandler() });
    server.setHandler(handlers);
    try {
        server.start();
        server.join();
    } finally {
        server.stop();
        _graphvizEngine.stop();
        engine.shutdown();
        scheduler.shutdownNow();
        HttpClient.close();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) File(java.io.File) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 24 with ResourceHandler

use of org.eclipse.jetty.server.handler.ResourceHandler in project pentaho-kettle by pentaho.

the class WebServer method startServer.

public void startServer() throws Exception {
    server = new Server();
    List<String> roles = new ArrayList<>();
    roles.add(Constraint.ANY_ROLE);
    // Set up the security handler, optionally with JAAS
    // 
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    if (System.getProperty("loginmodulename") != null && System.getProperty("java.security.auth.login.config") != null) {
        JAASLoginService jaasLoginService = new JAASLoginService(SERVICE_NAME);
        jaasLoginService.setLoginModuleName(System.getProperty("loginmodulename"));
        securityHandler.setLoginService(jaasLoginService);
    } else {
        roles.add(DEFAULT_ROLE);
        HashLoginService hashLoginService;
        SlaveServer slaveServer = transformationMap.getSlaveServerConfig().getSlaveServer();
        if (!Utils.isEmpty(slaveServer.getPassword())) {
            hashLoginService = new HashLoginService(SERVICE_NAME);
            UserStore userStore = new UserStore();
            userStore.addUser(slaveServer.getUsername(), new Password(slaveServer.getPassword()), new String[] { DEFAULT_ROLE });
            hashLoginService.setUserStore(userStore);
        } else {
            // See if there is a kettle.pwd file in the KETTLE_HOME directory:
            if (Utils.isEmpty(passwordFile)) {
                File homePwdFile = new File(Const.getKettleCartePasswordFile());
                if (homePwdFile.exists()) {
                    passwordFile = Const.getKettleCartePasswordFile();
                } else {
                    passwordFile = Const.getKettleLocalCartePasswordFile();
                }
            }
            hashLoginService = new HashLoginService(SERVICE_NAME, passwordFile) {

                @Override
                protected String[] loadRoleInfo(UserPrincipal user) {
                    List<String> newRoles = new ArrayList<>();
                    newRoles.add(DEFAULT_ROLE);
                    String[] roles = super.loadRoleInfo(user);
                    if (null != roles) {
                        Collections.addAll(newRoles, roles);
                    }
                    return newRoles.toArray(new String[0]);
                }
            };
        }
        securityHandler.setLoginService(hashLoginService);
    }
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(roles.toArray(new String[0]));
    constraint.setAuthenticate(true);
    ConstraintMapping constraintMapping = new ConstraintMapping();
    constraintMapping.setConstraint(constraint);
    constraintMapping.setPathSpec("/*");
    securityHandler.setConstraintMappings(new ConstraintMapping[] { constraintMapping });
    // Add all the servlets defined in kettle-servlets.xml ...
    // 
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    // Root
    // 
    ServletContextHandler root = new ServletContextHandler(contexts, GetRootServlet.CONTEXT_PATH, ServletContextHandler.SESSIONS);
    GetRootServlet rootServlet = new GetRootServlet();
    rootServlet.setJettyMode(true);
    root.addServlet(new ServletHolder(rootServlet), "/*");
    PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    List<PluginInterface> plugins = pluginRegistry.getPlugins(CartePluginType.class);
    for (PluginInterface plugin : plugins) {
        CartePluginInterface servlet = pluginRegistry.loadClass(plugin, CartePluginInterface.class);
        servlet.setup(transformationMap, jobMap, socketRepository, detections);
        servlet.setJettyMode(true);
        ServletContextHandler servletContext = new ServletContextHandler(contexts, getContextPath(servlet), ServletContextHandler.SESSIONS);
        ServletHolder servletHolder = new ServletHolder((Servlet) servlet);
        servletContext.addServlet(servletHolder, "/*");
    }
    // setup jersey (REST)
    ServletHolder jerseyServletHolder = new ServletHolder(ServletContainer.class);
    jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
    jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.packages", "org.pentaho.di.www.jaxrs");
    root.addServlet(jerseyServletHolder, "/api/*");
    // setup static resource serving
    // ResourceHandler mobileResourceHandler = new ResourceHandler();
    // mobileResourceHandler.setWelcomeFiles(new String[]{"index.html"});
    // mobileResourceHandler.setResourceBase(getClass().getClassLoader().
    // getResource("org/pentaho/di/www/mobile").toExternalForm());
    // Context mobileContext = new Context(contexts, "/mobile", Context.SESSIONS);
    // mobileContext.setHandler(mobileResourceHandler);
    // Allow png files to be shown for transformations and jobs...
    // 
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase("temp");
    // add all handlers/contexts to server
    // set up static servlet
    ServletHolder staticHolder = new ServletHolder("static", DefaultServlet.class);
    // resourceBase maps to the path relative to where carte is started
    staticHolder.setInitParameter("resourceBase", "./static/");
    staticHolder.setInitParameter("dirAllowed", "true");
    staticHolder.setInitParameter("pathInfoOnly", "true");
    root.addServlet(staticHolder, "/static/*");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, contexts });
    securityHandler.setHandler(handlers);
    server.setHandler(securityHandler);
    // Start execution
    createListeners();
    server.start();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) SlaveServer(org.pentaho.di.cluster.SlaveServer) Constraint(org.eclipse.jetty.util.security.Constraint) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ArrayList(java.util.ArrayList) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) SlaveServer(org.pentaho.di.cluster.SlaveServer) HashLoginService(org.eclipse.jetty.security.HashLoginService) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ArrayList(java.util.ArrayList) HandlerList(org.eclipse.jetty.server.handler.HandlerList) List(java.util.List) Password(org.eclipse.jetty.util.security.Password) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) PluginInterface(org.pentaho.di.core.plugins.PluginInterface) UserStore(org.eclipse.jetty.security.UserStore) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) JAASLoginService(org.eclipse.jetty.jaas.JAASLoginService) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File)

Example 25 with ResourceHandler

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

the class SplitFileServer method main.

public static void main(String[] args) throws Exception {
    // Create the Server object and a corresponding ServerConnector and then
    // set the port for the connector. In this example the server will
    // listen on port 8090. If you set this to port 0 then when the server
    // has been started you can called connector.getLocalPort() to
    // programmatically get the port the server started on.
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(8090);
    server.setConnectors(new Connector[] { connector });
    // Create a Context Handler and ResourceHandler. The ContextHandler is
    // getting set to "/" path but this could be anything you like for
    // builing out your url. Note how we are setting the ResourceBase using
    // our jetty maven testing utilities to get the proper resource
    // directory, you needn't use these, you simply need to supply the paths
    // you are looking to serve content from.
    ResourceHandler rh0 = new ResourceHandler();
    ContextHandler context0 = new ContextHandler();
    context0.setContextPath("/");
    File dir0 = MavenTestingUtils.getTestResourceDir("dir0");
    context0.setBaseResource(Resource.newResource(dir0));
    context0.setHandler(rh0);
    // Rinse and repeat the previous item, only specifying a different
    // resource base.
    ResourceHandler rh1 = new ResourceHandler();
    ContextHandler context1 = new ContextHandler();
    context1.setContextPath("/");
    File dir1 = MavenTestingUtils.getTestResourceDir("dir1");
    context1.setBaseResource(Resource.newResource(dir1));
    context1.setHandler(rh1);
    // Create a ContextHandlerCollection and set the context handlers to it.
    // This will let jetty process urls against the declared contexts in
    // order to match up content.
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { context0, context1 });
    server.setHandler(contexts);
    // Start things up! 
    server.start();
    // Dump the server state
    System.out.println(server.dump());
    // The use of server.join() the will make the current thread join and
    // wait until the server is done executing.
    // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
    server.join();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) File(java.io.File)

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