Search in sources :

Example 41 with ServletConfig

use of javax.servlet.ServletConfig in project metrics by dropwizard.

the class MetricsServletTest method constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig.

@Test
public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig() throws Exception {
    final MetricRegistry metricRegistry = mock(MetricRegistry.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    final MetricsServlet metricsServlet = new MetricsServlet(metricRegistry);
    metricsServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, never()).getAttribute(eq(MetricsServlet.METRICS_REGISTRY));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 42 with ServletConfig

use of javax.servlet.ServletConfig in project Lucee by lucee.

the class PageContextUtil method getPageContext.

public static PageContext getPageContext(Config config, ServletConfig servletConfig, File contextRoot, String host, String scriptName, String queryString, Cookie[] cookies, Map<String, Object> headers, Map<String, String> parameters, Map<String, Object> attributes, OutputStream os, boolean register, long timeout, boolean ignoreScopes) throws ServletException {
    boolean callOnStart = ThreadLocalPageContext.callOnStart.get();
    try {
        ThreadLocalPageContext.callOnStart.set(false);
        if (contextRoot == null)
            contextRoot = new File(".");
        // Engine
        CFMLEngine engine = null;
        try {
            engine = CFMLEngineFactory.getInstance();
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
        if (engine == null)
            throw new ServletException("there is no ServletContext");
        if (headers == null)
            headers = new HashMap<String, Object>();
        if (parameters == null)
            parameters = new HashMap<String, String>();
        if (attributes == null)
            attributes = new HashMap<String, Object>();
        // Request
        HttpServletRequest req = CreationImpl.getInstance(engine).createHttpServletRequest(contextRoot, host, scriptName, queryString, cookies, headers, parameters, attributes, null);
        // Response
        HttpServletResponse rsp = CreationImpl.getInstance(engine).createHttpServletResponse(os);
        if (config == null)
            config = ThreadLocalPageContext.getConfig();
        CFMLFactory factory = null;
        HttpServlet servlet;
        if (config instanceof ConfigWeb) {
            ConfigWeb cw = (ConfigWeb) config;
            factory = cw.getFactory();
            servlet = factory.getServlet();
        } else {
            if (servletConfig == null) {
                ServletConfig[] configs = engine.getServletConfigs();
                String rootDir = contextRoot.getAbsolutePath();
                for (ServletConfig conf : configs) {
                    if (lucee.commons.io.SystemUtil.arePathsSame(rootDir, conf.getServletContext().getRealPath("/"))) {
                        servletConfig = conf;
                        break;
                    }
                }
                if (servletConfig == null)
                    servletConfig = configs[0];
            }
            factory = engine.getCFMLFactory(servletConfig, req);
            servlet = new HTTPServletImpl(servletConfig, servletConfig.getServletContext(), servletConfig.getServletName());
        }
        return factory.getLuceePageContext(servlet, req, rsp, null, false, -1, false, register, timeout, false, ignoreScopes);
    } finally {
        ThreadLocalPageContext.callOnStart.set(callOnStart);
    }
}
Also used : HashMap(java.util.HashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) CFMLFactory(lucee.runtime.CFMLFactory) ConfigWeb(lucee.runtime.config.ConfigWeb) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) CFMLEngine(lucee.loader.engine.CFMLEngine) HTTPServletImpl(lucee.cli.servlet.HTTPServletImpl) File(java.io.File)

Example 43 with ServletConfig

use of javax.servlet.ServletConfig in project cxf by apache.

the class AtmosphereWebSocketJettyDestination method activate.

protected void activate() {
    super.activate();
    if (handler.getServletContext().getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") == null) {
        try {
            Class<?> dcc = ClassUtils.forName("org.eclipse.jetty.util.DecoratedObjectFactory", getClass().getClassLoader());
            handler.getServletContext().setAttribute("org.eclipse.jetty.util.DecoratedObjectFactory", dcc.newInstance());
        } catch (ClassNotFoundException | LinkageError | InstantiationException | IllegalAccessException e) {
        // ignore, old version of Jetty
        }
    }
    ServletConfig config = new VoidServletConfig(initParams) {

        @Override
        public ServletContext getServletContext() {
            return handler.getServletContext();
        }
    };
    try {
        framework.init(config);
    } catch (ServletException e) {
        throw new Fault(new Message("Could not initialize WebSocket Framework", LOG, e.getMessage()), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) VoidServletConfig(org.atmosphere.util.VoidServletConfig) Message(org.apache.cxf.common.i18n.Message) ServletConfig(javax.servlet.ServletConfig) VoidServletConfig(org.atmosphere.util.VoidServletConfig) Fault(org.apache.cxf.interceptor.Fault)

Example 44 with ServletConfig

use of javax.servlet.ServletConfig in project tomee by apache.

the class ArquillianFilterRunner method init.

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
    try {
        delegate = HttpServlet.class.cast(Thread.currentThread().getContextClassLoader().loadClass(ARQUILLIAN_SERVLET_RUNNER).newInstance());
        delegate.init(new ServletConfig() {

            @Override
            public String getServletName() {
                return ArquillianFilterRunner.class.getName();
            }

            @Override
            public ServletContext getServletContext() {
                return filterConfig.getServletContext();
            }

            @Override
            public String getInitParameter(final String name) {
                return null;
            }

            @Override
            public Enumeration<String> getInitParameterNames() {
                return emptyEnumeration();
            }
        });
    } catch (final Exception e) {
    // no-op: can happen if the servlet is not present, that's a normal case
    }
}
Also used : Collections.emptyEnumeration(java.util.Collections.emptyEnumeration) Enumeration(java.util.Enumeration) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 45 with ServletConfig

use of javax.servlet.ServletConfig in project openremote by openremote.

the class JSAPIServlet method scanResources.

@SuppressWarnings("unchecked")
public void scanResources() throws Exception {
    ServletConfig config = getServletConfig();
    ServletContext servletContext = config.getServletContext();
    Map<String, ResteasyDeployment> deployments = (Map<String, ResteasyDeployment>) servletContext.getAttribute(ResteasyContextParameters.RESTEASY_DEPLOYMENTS);
    if (deployments == null)
        return;
    synchronized (this) {
        services = new HashMap<String, ServiceRegistry>();
        for (Map.Entry<String, ResteasyDeployment> entry : deployments.entrySet()) {
            ResourceMethodRegistry registry = (ResourceMethodRegistry) entry.getValue().getRegistry();
            ResteasyProviderFactory providerFactory = entry.getValue().getProviderFactory();
            ServiceRegistry service = new ServiceRegistry(null, registry, providerFactory, null);
            services.put(entry.getKey(), service);
        }
    }
}
Also used : ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) ResourceMethodRegistry(org.jboss.resteasy.core.ResourceMethodRegistry) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ServletConfig (javax.servlet.ServletConfig)79 ServletContext (javax.servlet.ServletContext)54 Enumeration (java.util.Enumeration)38 ServletException (javax.servlet.ServletException)24 BeforeMethod (org.testng.annotations.BeforeMethod)21 Test (org.junit.Test)17 IOException (java.io.IOException)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)8 HttpServlet (javax.servlet.http.HttpServlet)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 UnavailableException (javax.servlet.UnavailableException)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3