use of javax.servlet.DispatcherType in project spring-boot by spring-projects.
the class SecurityAutoConfigurationTests method customFilterDispatcherTypes.
@Test
public void customFilterDispatcherTypes() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "security.filter-dispatcher-types:INCLUDE,ERROR");
this.context.refresh();
DelegatingFilterProxyRegistrationBean bean = this.context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class);
@SuppressWarnings("unchecked") EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils.getField(bean, "dispatcherTypes");
assertThat(dispatcherTypes).containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR);
}
use of javax.servlet.DispatcherType in project felix by apache.
the class AsyncTest method testAsyncServletWithDispatchOk.
/**
* Tests that we can use an asynchronous servlet (introduced in Servlet 3.0 spec) using the dispatching functionality.
*/
@Test
public void testAsyncServletWithDispatchOk() throws Exception {
CountDownLatch initLatch = new CountDownLatch(1);
CountDownLatch destroyLatch = new CountDownLatch(1);
TestServlet servlet = new TestServlet(initLatch, destroyLatch) {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DispatcherType dispatcherType = req.getDispatcherType();
if (DispatcherType.REQUEST == dispatcherType) {
final AsyncContext asyncContext = req.startAsync(req, resp);
asyncContext.start(new Runnable() {
@Override
public void run() {
try {
// Simulate a long running process...
Thread.sleep(1000);
asyncContext.getRequest().setAttribute("msg", "Hello Async world!");
asyncContext.dispatch();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
} else if (DispatcherType.ASYNC == dispatcherType) {
String response = (String) req.getAttribute("msg");
resp.setStatus(SC_OK);
resp.getWriter().printf(response);
resp.flushBuffer();
}
}
};
register("/test", servlet);
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
assertContent("Hello Async world!", createURL("/test"));
unregister("/test");
assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
assertResponseCode(SC_NOT_FOUND, createURL("/test"));
}
use of javax.servlet.DispatcherType in project felix by apache.
the class JettyServiceTest method testInitBundleContextDeployIT.
/**
* Tests to ensure the osgi-bundlecontext is available for init methods.
*
* @throws MalformedURLException
* @throws InterruptedException
*/
@Test
public void testInitBundleContextDeployIT() throws Exception {
// Setup mocks
Deployment mockDeployment = mock(Deployment.class);
Bundle mockBundle = mock(Bundle.class);
BundleContext mockBundleContext = mock(BundleContext.class);
// Setup behaviors
when(mockDeployment.getBundle()).thenReturn(mockBundle);
final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
when(f.toString()).thenReturn("(prop=*)");
when(mockBundleContext.createFilter(anyString())).thenReturn(f);
when(mockBundle.getBundleContext()).thenReturn(mockBundleContext);
when(mockBundle.getSymbolicName()).thenReturn("test");
when(mockBundle.getVersion()).thenReturn(new Version("0.0.1"));
Dictionary<String, String> headerProperties = new Hashtable<String, String>();
headerProperties.put("Web-ContextPath", "test");
when(mockBundle.getHeaders()).thenReturn(headerProperties);
when(mockDeployment.getContextPath()).thenReturn("test");
when(mockBundle.getEntry("/")).thenReturn(new URL("http://www.apache.com"));
when(mockBundle.getState()).thenReturn(Bundle.ACTIVE);
EnumSet<DispatcherType> dispatcherSet = EnumSet.allOf(DispatcherType.class);
dispatcherSet.add(DispatcherType.REQUEST);
WebAppBundleContext webAppBundleContext = new WebAppBundleContext("/", mockBundle, this.getClass().getClassLoader());
final CountDownLatch testLatch = new CountDownLatch(2);
// Add a Filter to test whether the osgi-bundlecontext is available at init
webAppBundleContext.addServlet(new ServletHolder(new Servlet() {
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// Do Nothing
}
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
testLatch.countDown();
}
@Override
public String getServletInfo() {
return null;
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void destroy() {
// Do Nothing
}
}), "/test1");
webAppBundleContext.addFilter(new FilterHolder(new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext context = filterConfig.getServletContext();
assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
testLatch.countDown();
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Do Nothing
}
@Override
public void destroy() {
// Do Nothing
}
}), "/test2", dispatcherSet);
jettyService.deploy(mockDeployment, webAppBundleContext);
// Fail if takes too long.
if (!testLatch.await(10, TimeUnit.SECONDS)) {
fail("Test Was not asserted");
}
}
use of javax.servlet.DispatcherType in project dropwizard-guicey by xvik.
the class WebFilterInstaller method configure.
private void configure(final ServletEnvironment environment, final Filter filter, final String name, final WebFilter annotation) {
final FilterRegistration.Dynamic mapping = environment.addFilter(name, filter);
final EnumSet<DispatcherType> dispatcherTypes = EnumSet.copyOf(Arrays.asList(annotation.dispatcherTypes()));
if (annotation.servletNames().length > 0) {
mapping.addMappingForServletNames(dispatcherTypes, false, annotation.servletNames());
} else {
final String[] urlPatterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value();
mapping.addMappingForUrlPatterns(dispatcherTypes, false, urlPatterns);
}
if (annotation.initParams().length > 0) {
for (WebInitParam param : annotation.initParams()) {
mapping.setInitParameter(param.name(), param.value());
}
}
mapping.setAsyncSupported(annotation.asyncSupported());
}
use of javax.servlet.DispatcherType in project fabric8 by fabric8io.
the class Servers method startServer.
public static Server startServer(String appName, Function<ServletContextHandler, Void> contextCallback, String defaultPort) throws Exception {
String port = Systems.getEnvVarOrSystemProperty("HTTP_PORT", "HTTP_PORT", defaultPort);
Integer num = Integer.parseInt(port);
String service = Systems.getEnvVarOrSystemProperty("WEB_CONTEXT_PATH", "WEB_CONTEXT_PATH", "");
String servicesPath = "cxf/servicesList";
String servletContextPath = "/" + service;
ManagedApi.setSingletonCxfServletContext(servletContextPath);
String url = "http://localhost:" + port + servletContextPath;
if (!url.endsWith("/")) {
url += "/";
}
System.out.println();
System.out.println("-------------------------------------------------------------");
System.out.println(appName + " is now running at: " + url);
System.out.println("-------------------------------------------------------------");
System.out.println();
final Server server = new Server(num);
// Register and map the dispatcher servlet
final ServletHolder servletHolder = new ServletHolder(new CXFCdiServlet());
// change default service list URI
servletHolder.setInitParameter("service-list-path", "/" + servicesPath);
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addEventListener(new Listener());
context.addEventListener(new BeanManagerResourceBindingListener());
String servletPath = "/*";
if (Strings.isNotBlank(service)) {
servletPath = servletContextPath + "/*";
}
context.addServlet(servletHolder, servletPath);
server.setHandler(context);
EnumSet<DispatcherType> dispatches = EnumSet.allOf(DispatcherType.class);
context.addFilter(RestCorsFilter.class, "/*", dispatches);
if (contextCallback != null) {
contextCallback.apply(context);
}
server.start();
return server;
}
Aggregations