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);
}
}
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);
}
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);
}
}
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);
}
}
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();
}
}
Aggregations