use of javax.servlet.ServletResponse in project guice by google.
the class ServletTest method testExistingSessionObject.
public void testExistingSessionObject() throws CreationException, IOException, ServletException {
final Injector injector = createInjector();
final HttpServletRequest request = newFakeHttpServletRequest();
GuiceFilter filter = new GuiceFilter();
final boolean[] invoked = new boolean[1];
FilterChain filterChain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
invoked[0] = true;
InSession inSession = injector.getInstance(InSession.class);
assertSame(inSession, injector.getInstance(InSession.class));
assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
}
};
filter.doFilter(request, null, filterChain);
assertTrue(invoked[0]);
}
use of javax.servlet.ServletResponse in project guice by google.
the class ServletTest method testHttpSessionIsSerializable.
public void testHttpSessionIsSerializable() throws Exception {
final Injector injector = createInjector();
final HttpServletRequest request = newFakeHttpServletRequest();
final HttpSession session = request.getSession();
GuiceFilter filter = new GuiceFilter();
final boolean[] invoked = new boolean[1];
FilterChain filterChain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
invoked[0] = true;
assertNotNull(injector.getInstance(InSession.class));
assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
}
};
filter.doFilter(request, null, filterChain);
assertTrue(invoked[0]);
HttpSession deserializedSession = reserialize(session);
String inSessionKey = IN_SESSION_KEY.toString();
String inSessionNullKey = IN_SESSION_NULL_KEY.toString();
assertTrue(deserializedSession.getAttribute(inSessionKey) instanceof InSession);
assertEquals(NullObject.INSTANCE, deserializedSession.getAttribute(inSessionNullKey));
}
use of javax.servlet.ServletResponse in project hudson-2.x by hudson.
the class ChainedServletFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
LOGGER.entering(ChainedServletFilter.class.getName(), "doFilter");
new FilterChain() {
private int position = 0;
// capture the array for thread-safety
private final Filter[] filters = ChainedServletFilter.this.filters;
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
if (position == filters.length) {
// reached to the end
chain.doFilter(request, response);
} else {
// call next
filters[position++].doFilter(request, response, this);
}
}
}.doFilter(request, response);
}
use of javax.servlet.ServletResponse in project hadoop by apache.
the class TestStaticUserWebFilter method testFilter.
@Test
public void testFilter() throws Exception {
FilterConfig config = mockConfig("myuser");
StaticUserFilter suf = new StaticUserFilter();
suf.init(config);
ArgumentCaptor<HttpServletRequestWrapper> wrapperArg = ArgumentCaptor.forClass(HttpServletRequestWrapper.class);
FilterChain chain = mock(FilterChain.class);
suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), chain);
Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject());
HttpServletRequestWrapper wrapper = wrapperArg.getValue();
assertEquals("myuser", wrapper.getUserPrincipal().getName());
assertEquals("myuser", wrapper.getRemoteUser());
suf.destroy();
}
use of javax.servlet.ServletResponse in project hadoop by apache.
the class TestHostnameFilter method hostname.
@Test
public void hostname() throws Exception {
ServletRequest request = Mockito.mock(ServletRequest.class);
Mockito.when(request.getRemoteAddr()).thenReturn("localhost");
ServletResponse response = Mockito.mock(ServletResponse.class);
final AtomicBoolean invoked = new AtomicBoolean();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
// Hostname was set to "localhost", but may get resolved automatically to
// "127.0.0.1" depending on OS.
assertTrue(HostnameFilter.get().contains("localhost") || HostnameFilter.get().contains("127.0.0.1"));
invoked.set(true);
}
};
Filter filter = new HostnameFilter();
filter.init(null);
assertNull(HostnameFilter.get());
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
assertNull(HostnameFilter.get());
filter.destroy();
}
Aggregations