Search in sources :

Example 6 with ResourceConfig

use of com.sun.jersey.api.core.ResourceConfig 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 7 with ResourceConfig

use of com.sun.jersey.api.core.ResourceConfig 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)

Example 8 with ResourceConfig

use of com.sun.jersey.api.core.ResourceConfig in project druid by druid-io.

the class WebserverTestUtils method createServer.

public static HttpServer createServer(String SERVICE_NAME, URI baseUri, String resourceClassName, Consumer<Binder> extender) throws IOException {
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(binder -> {
        binder.bindConstant().annotatedWith(Names.named("serviceName")).to(SERVICE_NAME);
        binder.bindConstant().annotatedWith(Names.named("servicePort")).to(baseUri.getPort());
        binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(baseUri.getPort() + 1);
        binder.bind(Key.get(ServiceEmitter.class)).toInstance(new NoopServiceEmitter());
        binder.bind(Key.get(AuthConfig.class)).toInstance(new AuthConfig());
        binder.bind(AuthorizerMapper.class).toInstance(AuthTestUtils.TEST_AUTHORIZER_MAPPER);
        binder.bind(AuthenticatorMapper.class).toInstance(AuthTestUtils.TEST_AUTHENTICATOR_MAPPER);
        binder.bind(Key.get(HttpClient.class, Client.class)).toInstance(EasyMock.createMock(HttpClient.class));
        extender.accept(binder);
    }));
    ResourceConfig resourceConfig = new ClassNamesResourceConfig(resourceClassName + ';' + MockHttpServletRequest.class.getName());
    IoCComponentProviderFactory ioc = new GuiceComponentProviderFactory(resourceConfig, injector);
    HttpServer server = GrizzlyServerFactory.createHttpServer(baseUri, resourceConfig, ioc);
    return server;
}
Also used : HttpClient(org.apache.druid.java.util.http.client.HttpClient) Provider(javax.ws.rs.ext.Provider) AuthorizerMapper(org.apache.druid.server.security.AuthorizerMapper) Key(com.google.inject.Key) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult) HttpServletRequest(javax.servlet.http.HttpServletRequest) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AuthTestUtils(org.apache.druid.server.security.AuthTestUtils) HttpServer(org.glassfish.grizzly.http.server.HttpServer) UriBuilder(javax.ws.rs.core.UriBuilder) AuthConfig(org.apache.druid.server.security.AuthConfig) URI(java.net.URI) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) Context(javax.ws.rs.core.Context) GuiceComponentProviderFactory(com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory) AuthenticatorMapper(org.apache.druid.server.security.AuthenticatorMapper) ClassNamesResourceConfig(com.sun.jersey.api.core.ClassNamesResourceConfig) SingletonTypeInjectableProvider(com.sun.jersey.spi.inject.SingletonTypeInjectableProvider) IOException(java.io.IOException) Names(com.google.inject.name.Names) EasyMock(org.easymock.EasyMock) GrizzlyServerFactory(com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory) GuiceInjectors(org.apache.druid.guice.GuiceInjectors) Injector(com.google.inject.Injector) Consumer(java.util.function.Consumer) IoCComponentProviderFactory(com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory) Initialization(org.apache.druid.initialization.Initialization) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) Client(org.apache.druid.guice.annotations.Client) Collections(java.util.Collections) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) Injector(com.google.inject.Injector) ClassNamesResourceConfig(com.sun.jersey.api.core.ClassNamesResourceConfig) HttpClient(org.apache.druid.java.util.http.client.HttpClient) HttpServer(org.glassfish.grizzly.http.server.HttpServer) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) AuthConfig(org.apache.druid.server.security.AuthConfig) ResourceConfig(com.sun.jersey.api.core.ResourceConfig) ClassNamesResourceConfig(com.sun.jersey.api.core.ClassNamesResourceConfig) HttpClient(org.apache.druid.java.util.http.client.HttpClient) Client(org.apache.druid.guice.annotations.Client) GuiceComponentProviderFactory(com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory) IoCComponentProviderFactory(com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory)

Aggregations

ResourceConfig (com.sun.jersey.api.core.ResourceConfig)8 ServletContainer (com.sun.jersey.spi.container.servlet.ServletContainer)6 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 Server (org.eclipse.jetty.server.Server)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 DefaultResourceConfig (com.sun.jersey.api.core.DefaultResourceConfig)4 MediaType (javax.ws.rs.core.MediaType)3 ApplicationAdapter (com.sun.jersey.api.core.ApplicationAdapter)2 IoCComponentProviderFactory (com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory)2 IOException (java.io.IOException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 DispatcherType (javax.servlet.DispatcherType)2 AbstractSessionManager (org.eclipse.jetty.server.session.AbstractSessionManager)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 AbstractSecuredWebServer (com.emc.storageos.security.AbstractSecuredWebServer)1 ServiceCodeExceptionMapper (com.emc.storageos.svcs.errorhandling.mappers.ServiceCodeExceptionMapper)1