Search in sources :

Example 16 with FilterInfo

use of io.undertow.servlet.api.FilterInfo in project spring-framework by spring-projects.

the class UndertowTestServer method deployConfig.

@Override
@SuppressWarnings("deprecation")
public void deployConfig(WebApplicationContext wac, Filter... filters) {
    DispatcherServletInstanceFactory servletFactory = new DispatcherServletInstanceFactory(wac);
    // manually building WebSocketDeploymentInfo in order to avoid class cast exceptions
    // with tomcat's implementation when using undertow 1.1.0+
    WebSocketDeploymentInfo info = new WebSocketDeploymentInfo();
    try {
        info.setWorker(Xnio.getInstance().createWorker(OptionMap.EMPTY));
        info.setBuffers(new org.xnio.ByteBufferSlicePool(1024, 1024));
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
    DeploymentInfo servletBuilder = deployment().setClassLoader(UndertowTestServer.class.getClassLoader()).setDeploymentName("undertow-websocket-test").setContextPath("/").addServlet(servlet("DispatcherServlet", DispatcherServlet.class, servletFactory).addMapping("/").setAsyncSupported(true)).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, info);
    for (final Filter filter : filters) {
        String filterName = filter.getClass().getName();
        FilterInstanceFactory filterFactory = new FilterInstanceFactory(filter);
        FilterInfo filterInfo = new FilterInfo(filterName, filter.getClass(), filterFactory);
        servletBuilder.addFilter(filterInfo.setAsyncSupported(true));
        for (DispatcherType type : DispatcherType.values()) {
            servletBuilder.addFilterUrlMapping(filterName, "/*", type);
        }
    }
    try {
        this.manager = defaultContainer().addDeployment(servletBuilder);
        this.manager.deploy();
        HttpHandler httpHandler = this.manager.start();
        this.server = Undertow.builder().addHttpListener(0, "localhost").setHandler(httpHandler).build();
    } catch (ServletException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) DispatcherType(javax.servlet.DispatcherType) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 17 with FilterInfo

use of io.undertow.servlet.api.FilterInfo in project undertow by undertow-io.

the class ServletContextImpl method getFilterRegistration.

@Override
public FilterRegistration getFilterRegistration(final String filterName) {
    ensureNotProgramaticListener();
    final FilterInfo filterInfo = deploymentInfo.getFilters().get(filterName);
    if (filterInfo == null) {
        return null;
    }
    return new FilterRegistrationImpl(filterInfo, deployment, this);
}
Also used : FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 18 with FilterInfo

use of io.undertow.servlet.api.FilterInfo in project undertow by undertow-io.

the class ServletContextImpl method addFilter.

@Override
public FilterRegistration.Dynamic addFilter(final String filterName, final String className) {
    ensureNotProgramaticListener();
    ensureNotInitialized();
    if (deploymentInfo.getFilters().containsKey(filterName)) {
        return null;
    }
    try {
        Class<? extends Filter> filterClass = (Class<? extends Filter>) deploymentInfo.getClassLoader().loadClass(className);
        FilterInfo filter = new FilterInfo(filterName, filterClass, deploymentInfo.getClassIntrospecter().createInstanceFactory(filterClass));
        deploymentInfo.addFilter(filter);
        deployment.getFilters().addFilter(filter);
        return new FilterRegistrationImpl(filter, deployment, this);
    } catch (ClassNotFoundException e) {
        throw UndertowServletMessages.MESSAGES.cannotLoadClass(className, e);
    } catch (NoSuchMethodException e) {
        throw UndertowServletMessages.MESSAGES.couldNotCreateFactory(className, e);
    }
}
Also used : Filter(javax.servlet.Filter) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 19 with FilterInfo

use of io.undertow.servlet.api.FilterInfo in project undertow by undertow-io.

the class ServletContextImpl method addFilter.

@Override
public FilterRegistration.Dynamic addFilter(final String filterName, final Class<? extends Filter> filterClass) {
    ensureNotProgramaticListener();
    ensureNotInitialized();
    if (deploymentInfo.getFilters().containsKey(filterName)) {
        return null;
    }
    try {
        FilterInfo filter = new FilterInfo(filterName, filterClass, deploymentInfo.getClassIntrospecter().createInstanceFactory(filterClass));
        deploymentInfo.addFilter(filter);
        deployment.getFilters().addFilter(filter);
        return new FilterRegistrationImpl(filter, deployment, this);
    } catch (NoSuchMethodException e) {
        throw UndertowServletMessages.MESSAGES.couldNotCreateFactory(filterClass.getName(), e);
    }
}
Also used : FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 20 with FilterInfo

use of io.undertow.servlet.api.FilterInfo in project undertow by undertow-io.

the class ServletMetricsHandlerTestCase method testMetrics.

@Test
public void testMetrics() throws Exception {
    final TestMetricsCollector metricsCollector = new TestMetricsCollector();
    CompletionLatchHandler completionLatchHandler;
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(ServletPathMappingTestCase.class.getClassLoader()).setContextPath("/servletContext").setDeploymentName("servletContext.war").setResourceManager(new TestResourceLoader(DefaultServletTestCase.class));
    builder.addServlet(new ServletInfo("MetricTestServlet", MetricTestServlet.class).addMapping("/path/default"));
    builder.addFilter(new FilterInfo("Filter", HelloFilter.class));
    builder.addFilterUrlMapping("Filter", "/filterpath/*", DispatcherType.REQUEST);
    builder.setMetricsCollector(metricsCollector);
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(completionLatchHandler = new CompletionLatchHandler(root));
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/path/default");
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertTrue(HttpClientUtils.readResponse(result).contains("metric"));
        completionLatchHandler.await();
        completionLatchHandler.reset();
        MetricsHandler.MetricResult metrics = metricsCollector.getMetrics("MetricTestServlet");
        Assert.assertEquals(1, metrics.getTotalRequests());
        Assert.assertTrue(metrics.getMaxRequestTime() > 0);
        Assert.assertEquals(metrics.getMinRequestTime(), metrics.getMaxRequestTime());
        Assert.assertEquals(metrics.getMaxRequestTime(), metrics.getTotalRequestTime());
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertTrue(HttpClientUtils.readResponse(result).contains("metric"));
        completionLatchHandler.await();
        completionLatchHandler.reset();
        metrics = metricsCollector.getMetrics("MetricTestServlet");
        Assert.assertEquals(2, metrics.getTotalRequests());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HelloFilter(io.undertow.servlet.test.defaultservlet.HelloFilter) DefaultServletTestCase(io.undertow.servlet.test.defaultservlet.DefaultServletTestCase) TestResourceLoader(io.undertow.servlet.test.util.TestResourceLoader) DeploymentManager(io.undertow.servlet.api.DeploymentManager) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) ServletInfo(io.undertow.servlet.api.ServletInfo) ServletContainer(io.undertow.servlet.api.ServletContainer) MetricsHandler(io.undertow.server.handlers.MetricsHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo) Test(org.junit.Test)

Aggregations

FilterInfo (io.undertow.servlet.api.FilterInfo)24 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)19 DeploymentManager (io.undertow.servlet.api.DeploymentManager)16 ServletContainer (io.undertow.servlet.api.ServletContainer)16 ServletInfo (io.undertow.servlet.api.ServletInfo)15 PathHandler (io.undertow.server.handlers.PathHandler)14 BeforeClass (org.junit.BeforeClass)6 Test (org.junit.Test)6 TestResourceLoader (io.undertow.servlet.test.util.TestResourceLoader)5 TestHttpClient (io.undertow.testutils.TestHttpClient)5 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)4 LoginConfig (io.undertow.servlet.api.LoginConfig)3 PerMessageDeflateHandshake (io.undertow.websockets.extensions.PerMessageDeflateHandshake)3 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)2 HandlerWrapper (io.undertow.server.HandlerWrapper)2 HttpHandler (io.undertow.server.HttpHandler)2 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)2 ServletExtension (io.undertow.servlet.ServletExtension)2 ErrorPage (io.undertow.servlet.api.ErrorPage)2 ListenerInfo (io.undertow.servlet.api.ListenerInfo)2