Search in sources :

Example 11 with ServletContainer

use of com.sun.jersey.spi.container.servlet.ServletContainer in project coprhd-controller by CoprHD.

the class AbstractSecuredWebServer method initServer.

/**
 * Initialize server handlers, rest resources.
 *
 * @throws Exception
 */
protected void initServer() throws Exception {
    _server = new Server();
    initThreadPool();
    initConnectors();
    // AuthN servlet filters
    servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletHandler.setContextPath("/");
    _server.setHandler(servletHandler);
    ((AbstractSessionManager) servletHandler.getSessionHandler().getSessionManager()).setUsingCookies(false);
    if (_disabler != null) {
        final FilterHolder securityFilterHolder = new FilterHolder(new DelegatingFilterProxy(_disablingFilter));
        servletHandler.addFilter(securityFilterHolder, "/*", FilterMapping.REQUEST);
        _log.warn("security checks are disabled... skipped adding security filters");
    } else {
        final FilterHolder securityFilterHolder = new FilterHolder(new DelegatingFilterProxy(_secFilters));
        servletHandler.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);
        type.put("octet-stream", MediaType.APPLICATION_OCTET_STREAM_TYPE);
        type.put("form-data", MediaType.MULTIPART_FORM_DATA_TYPE);
        servletHandler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
        // AuthZ resource filters
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES, _resourceFilterFactory);
        // Adding the ContainerResponseFilter
        props.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, _responseFilter);
        config.setPropertiesAndFeatures(props);
    }
    if (_dbClient != null) {
        // Otherwise there could be a dependency loop between services.
        if (startDbClientInBackground) {
            _log.info("starting dbclient in background");
            new Thread() {

                public void run() {
                    _dbClient.start();
                }
            }.start();
        } else {
            _log.info("starting dbclient");
            _dbClient.start();
        }
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) 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) 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 12 with ServletContainer

use of com.sun.jersey.spi.container.servlet.ServletContainer in project ANNIS by korpling.

the class AnnisServiceRunner method createWebServer.

private void createWebServer() {
    // create beans
    ctx = new GenericXmlApplicationContext();
    ctx.setValidating(false);
    AnnisXmlContextHelper.prepareContext(ctx);
    ctx.load("file:" + Utils.getAnnisFile("conf/spring/Service.xml").getAbsolutePath());
    ctx.refresh();
    ResourceConfig rc = new PackagesResourceConfig("annis.service.internal", "annis.provider", "annis.rest.provider");
    final IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc, ctx);
    int port = overridePort == null ? ctx.getBean(QueryServiceImpl.class).getPort() : overridePort;
    try {
        // only allow connections from localhost
        // if the administrator wants to allow external acccess he *has* to
        // use a HTTP proxy which also should use SSL encryption
        InetSocketAddress addr = new InetSocketAddress("localhost", port);
        server = new Server(addr);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        server.setThreadPool(new ExecutorThreadPool());
        ServletContainer jerseyContainer = new ServletContainer(rc) {

            @Override
            protected void initiate(ResourceConfig rc, WebApplication wa) {
                wa.initiate(rc, factory);
            }
        };
        ServletHolder holder = new ServletHolder(jerseyContainer);
        context.addServlet(holder, "/*");
        context.setInitParameter(EnvironmentLoader.ENVIRONMENT_CLASS_PARAM, MultipleIniWebEnvironment.class.getName());
        if (useAuthentification) {
            log.info("Using authentification");
            context.setInitParameter(EnvironmentLoader.CONFIG_LOCATIONS_PARAM, "file:" + System.getProperty("annis.home") + "/conf/shiro.ini," + "file:" + System.getProperty("annis.home") + "/conf/develop_shiro.ini");
        } else {
            log.warn("*NOT* using authentification, your ANNIS service *IS NOT SECURED*");
            context.setInitParameter(EnvironmentLoader.CONFIG_LOCATIONS_PARAM, "file:" + System.getProperty("annis.home") + "/conf/shiro_no_security.ini");
        }
        EnumSet<DispatcherType> gzipDispatcher = EnumSet.of(DispatcherType.REQUEST);
        context.addFilter(GzipFilter.class, "/*", gzipDispatcher);
        // configure Apache Shiro with the web application
        context.addEventListener(new EnvironmentLoaderListener());
        EnumSet<DispatcherType> shiroDispatchers = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR);
        context.addFilter(ShiroFilter.class, "/*", shiroDispatchers);
    } catch (IllegalArgumentException ex) {
        log.error("IllegalArgumentException at ANNIS service startup", ex);
        isShutdownRequested = true;
        errorCode = 101;
    } catch (NullPointerException ex) {
        log.error("NullPointerException at ANNIS service startup", ex);
        isShutdownRequested = true;
        errorCode = 101;
    } catch (AnnisRunnerException ex) {
        errorCode = ex.getExitCode();
    }
}
Also used : EnvironmentLoaderListener(org.apache.shiro.web.env.EnvironmentLoaderListener) Server(org.eclipse.jetty.server.Server) MultipleIniWebEnvironment(annis.security.MultipleIniWebEnvironment) SpringComponentProviderFactory(com.sun.jersey.spi.spring.container.SpringComponentProviderFactory) InetSocketAddress(java.net.InetSocketAddress) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) PackagesResourceConfig(com.sun.jersey.api.core.PackagesResourceConfig) IoCComponentProviderFactory(com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory) AnnisRunnerException(annis.AnnisRunnerException) ServletContainer(com.sun.jersey.spi.container.servlet.ServletContainer) ExecutorThreadPool(org.eclipse.jetty.util.thread.ExecutorThreadPool) PackagesResourceConfig(com.sun.jersey.api.core.PackagesResourceConfig) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) WebApplication(com.sun.jersey.spi.container.WebApplication) DispatcherType(javax.servlet.DispatcherType)

Example 13 with ServletContainer

use of com.sun.jersey.spi.container.servlet.ServletContainer in project opencast by opencast.

the class RestServiceTestEnv method setUpServer.

/**
 * Return the base URL of the HTTP server. <code>http://host:port</code> public URL getBaseUrl() { return baseUrl; }
 *
 * Call in @BeforeClass annotated method.
 */
public void setUpServer() {
    try {
        // cut of any base pathbasestUrl might have
        int port = baseUrl.getPort();
        logger.info("Start http server at port " + port);
        hs = new Server(port);
        ServletContainer servletContainer = cfg.isSome() ? new ServletContainer(cfg.get()) : new ServletContainer();
        ServletHolder jerseyServlet = new ServletHolder(servletContainer);
        ServletContextHandler context = new ServletContextHandler(hs, "/");
        context.addServlet(jerseyServlet, "/*");
        hs.start();
    } catch (Exception e) {
        chuck(e);
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(com.sun.jersey.spi.container.servlet.ServletContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) IOException(java.io.IOException)

Example 14 with ServletContainer

use of com.sun.jersey.spi.container.servlet.ServletContainer in project coprhd-controller by CoprHD.

the class ErrorHandlingTest method setupServer.

@BeforeClass
public static void setupServer() throws Exception {
    _server = new Server(port);
    // AuthN servlet filters
    ServletContextHandler rootHandler = new ServletContextHandler();
    rootHandler.setContextPath("/");
    _server.setHandler(rootHandler);
    final StorageApplication application = new StorageApplication();
    final Set<Object> resources = new HashSet<Object>();
    resources.add(new ErrorHandlingTestResource());
    resources.add(new ServiceCodeExceptionMapper());
    application.setResource(resources);
    final ResourceConfig config = new DefaultResourceConfig();
    config.add(application);
    rootHandler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
    _server.start();
}
Also used : Server(org.eclipse.jetty.server.Server) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(com.sun.jersey.spi.container.servlet.ServletContainer) DataObject(com.emc.storageos.db.client.model.DataObject) StorageApplication(com.emc.storageos.api.service.impl.resource.StorageApplication) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServiceCodeExceptionMapper(com.emc.storageos.svcs.errorhandling.mappers.ServiceCodeExceptionMapper) HashSet(java.util.HashSet) BeforeClass(org.junit.BeforeClass)

Aggregations

ServletContainer (com.sun.jersey.spi.container.servlet.ServletContainer)14 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)9 Server (org.eclipse.jetty.server.Server)7 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)7 ResourceConfig (com.sun.jersey.api.core.ResourceConfig)6 DefaultResourceConfig (com.sun.jersey.api.core.DefaultResourceConfig)4 DispatcherType (javax.servlet.DispatcherType)3 MediaType (javax.ws.rs.core.MediaType)3 AbstractSessionManager (org.eclipse.jetty.server.session.AbstractSessionManager)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3 ApplicationAdapter (com.sun.jersey.api.core.ApplicationAdapter)2 File (java.io.File)2 EnvironmentLoaderListener (org.apache.shiro.web.env.EnvironmentLoaderListener)2 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)2 Bean (org.springframework.context.annotation.Bean)2 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)2 AnnisRunnerException (annis.AnnisRunnerException)1 MultipleIniWebEnvironment (annis.security.MultipleIniWebEnvironment)1 StorageApplication (com.emc.storageos.api.service.impl.resource.StorageApplication)1 DataObject (com.emc.storageos.db.client.model.DataObject)1