use of javax.servlet.ServletResponse in project guice by google.
the class FilterDefinitionTest method testGetFilterIfMatchingNotMatching.
public void testGetFilterIfMatchingNotMatching() throws ServletException {
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
HttpServletRequest servletRequest = createMock(HttpServletRequest.class);
ServletContext servletContext = createMock(ServletContext.class);
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
final MockFilter mockFilter = new MockFilter() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//suppress rest of chain...
}
};
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(servletRequest.getContextPath()).andReturn("/a_context_path");
expect(servletRequest.getRequestURI()).andReturn("/test.html");
replay(servletRequest, binding, injector);
filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
Filter filter = filterDef.getFilterIfMatching(servletRequest);
assertNull(filter);
verify(servletRequest, binding, injector);
}
use of javax.servlet.ServletResponse in project pulsar by yahoo.
the class ApiVersionFilterTest method testAuthFilterTest.
@Test
public void testAuthFilterTest() throws Exception {
BrokerService nativeService = mock(BrokerService.class);
AuthenticationService authService = mock(AuthenticationService.class);
doReturn(nativeService).when(pulsar).getBrokerService();
doReturn(authService).when(nativeService).getAuthenticationService();
doReturn("test-role").when(authService).authenticateHttpRequest(mock(HttpServletRequest.class));
AuthenticationFilter filter = new AuthenticationFilter(pulsar);
HttpServletRequest request = mock(HttpServletRequest.class);
ServletResponse response = null;
doNothing().when(request).setAttribute(anyString(), anyObject());
filter.doFilter(request, response, chain);
}
use of javax.servlet.ServletResponse in project java-chassis by ServiceComb.
the class RestAsyncListener method onTimeout.
@Override
public void onTimeout(AsyncEvent event) throws IOException {
// TODO:超时的处理,要重新考虑
ServletResponse response = event.getAsyncContext().getResponse();
PrintWriter out = response.getWriter();
out.write("TimeOut Error in Processing");
}
use of javax.servlet.ServletResponse in project atmosphere by Atmosphere.
the class SSEAtmosphereInterceptorTest method testDataWriter.
@Test
public void testDataWriter() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ServletResponse resp = Mockito.mock(HttpServletResponse.class);
Mockito.when(resp.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
});
AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
request.header(HeaderConfig.X_ATMOSPHERE_TRANSPORT, "SSE");
AtmosphereResponse response = AtmosphereResponseImpl.newInstance(request);
response.request(request);
response.setResponse(resp);
AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
resource.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, Mockito.mock(AsyncSupport.class), null);
resource.suspend();
SSEAtmosphereInterceptor interceptor = new SSEAtmosphereInterceptor();
interceptor.configure(config);
interceptor.inspect(resource);
// no newline
response.write("Good Morning".getBytes());
assertEquals(baos.toString(), "data:Good Morning\r\n\r\n");
baos.reset();
// \n
response.write("Hello World!\nHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
baos.reset();
// \r
response.write("Hello World!\rHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
baos.reset();
// \r\n
response.write("Hello World!\r\nHave a nice day!".getBytes());
assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
}
use of javax.servlet.ServletResponse in project lucene-solr by apache.
the class MockAuthenticationPlugin method doAuthenticate.
@Override
public boolean doAuthenticate(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
String user = null;
if (predicate != null) {
if (predicate.test(request)) {
user = (String) request.getAttribute(Principal.class.getName());
request.removeAttribute(Principal.class.getName());
}
}
final FilterChain ffc = filterChain;
final AtomicBoolean requestContinues = new AtomicBoolean(false);
forward(user, request, response, new FilterChain() {
@Override
public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
ffc.doFilter(req, res);
requestContinues.set(true);
}
});
return requestContinues.get();
}
Aggregations