use of javax.servlet.ServletRequest in project felix by apache.
the class WhiteboardManager method invokePreprocessors.
/**
* Invoke all preprocessors
*
* @param req The request
* @param res The response
* @return {@code true} to continue with dispatching, {@code false} to terminate the request.
* @throws IOException
* @throws ServletException
*/
public void invokePreprocessors(final HttpServletRequest req, final HttpServletResponse res, final Preprocessor dispatcher) throws ServletException, IOException {
final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
if (localHandlers.isEmpty()) {
// no preprocessors, we can directly execute
dispatcher.doFilter(req, res, null);
} else {
final FilterChain chain = new FilterChain() {
private int index = 0;
@Override
public void doFilter(final ServletRequest request, final ServletResponse response) throws IOException, ServletException {
if (index == localHandlers.size()) {
dispatcher.doFilter(request, response, null);
} else {
final PreprocessorHandler handler = localHandlers.get(index);
index++;
handler.handle(request, response, this);
}
}
};
chain.doFilter(req, res);
}
}
use of javax.servlet.ServletRequest in project felix by apache.
the class HttpServiceTest method setupOldWhiteboardFilter.
public void setupOldWhiteboardFilter(final String pattern) throws Exception {
Dictionary<String, Object> servletProps = new Hashtable<String, Object>();
servletProps.put("pattern", pattern);
final Filter f = new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
initLatch.countDown();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
response.getWriter().print("FILTER-");
response.flushBuffer();
chain.doFilter(request, response);
}
@Override
public void destroy() {
destroyLatch.countDown();
}
};
registrations.add(m_context.registerService(Filter.class.getName(), f, servletProps));
}
use of javax.servlet.ServletRequest 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.ServletRequest in project gocd by gocd.
the class FlashLoadingFilterIntegrationTest method shouldLoadExistingFlashFromSession.
@Test
public void shouldLoadExistingFlashFromSession() throws IOException, ServletException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpSession session = new MockHttpSession();
FlashMessageService.Flash oldFlash = new FlashMessageService.Flash();
oldFlash.put("my_key", new FlashMessageModel("my other message", "warning"));
session.putValue(FlashLoadingFilter.FLASH_SESSION_KEY, oldFlash);
req.setSession(session);
MockHttpServletResponse res = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) {
flash = service.get("my_key");
}
};
filter.doFilter(req, res, filterChain);
assertThat(flash.toString(), is("my other message"));
assertThat(flash.getFlashClass(), is("warning"));
}
use of javax.servlet.ServletRequest in project gocd by gocd.
the class FlashLoadingFilterIntegrationTest method shouldClearThreadContext.
@Test
public void shouldClearThreadContext() throws IOException, ServletException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) {
messageKey = service.add(new FlashMessageModel("my message", "error"));
flash = service.get(messageKey);
}
};
filter.doFilter(req, res, filterChain);
assertThat(flash.toString(), is("my message"));
try {
service.get(messageKey);
fail("attempt to load flash message should fail, as no thread local is cleared out");
} catch (Exception e) {
assertThat(e.getMessage(), is("No flash context found, this call should only be made within a request."));
}
}
Aggregations