public NinjaServletFilter() { this.wrapped = new GuiceFilter(); }
@Override public void destroy() { this.wrapped.destroy(); }
@Override public void destroy() { try { // Destroy all registered filters & servlets in that order FilterPipeline filterPipeline = getFilterPipeline(); filterPipeline.destroyPipeline(); } finally { reset(); servletContext.clear(); } }
public void testForwardUsingRequestDispatcher() throws IOException, ServletException { Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { serve("/").with(ForwardingServlet.class); serve("/blah.jsp").with(ForwardedServlet.class); } }); final HttpServletRequest requestMock = createMock(HttpServletRequest.class); HttpServletResponse responseMock = createMock(HttpServletResponse.class); expect(requestMock.getRequestURI()).andReturn("/").anyTimes(); expect(requestMock.getContextPath()).andReturn("").anyTimes(); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); expect(requestMock.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); expect(responseMock.isCommitted()).andReturn(false); responseMock.resetBuffer(); replay(requestMock, responseMock); new GuiceFilter().doFilter(requestMock, responseMock, createMock(FilterChain.class)); assertEquals("Incorrect number of forwards", 1, ForwardedServlet.forwardedTo); verify(requestMock, responseMock); }
public final void testDispatchThruGuiceFilter() throws ServletException, IOException { //create mocks FilterConfig filterConfig = createMock(FilterConfig.class); ServletContext servletContext = createMock(ServletContext.class); HttpServletRequest request = createMock(HttpServletRequest.class); FilterChain proceedingFilterChain = createMock(FilterChain.class); //begin mock script *** expect(filterConfig.getServletContext()).andReturn(servletContext).once(); expect(request.getRequestURI()).andReturn("/public/login.jsp").anyTimes(); expect(request.getContextPath()).andReturn("").anyTimes(); //at the end, proceed down webapp's normal filter chain proceedingFilterChain.doFilter(isA(HttpServletRequest.class), (ServletResponse) isNull()); expectLastCall().once(); //run mock script *** replay(filterConfig, servletContext, request, proceedingFilterChain); final GuiceFilter webFilter = new GuiceFilter(); webFilter.init(filterConfig); webFilter.doFilter(request, null, proceedingFilterChain); webFilter.destroy(); //assert expectations verify(filterConfig, servletContext, request, proceedingFilterChain); }
public final void testServletInNonSingletonScopeThrowsServletException() { GuiceFilter guiceFilter = new GuiceFilter(); Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { serve("/*").with(MyNonSingletonServlet.class); } }); ServletException se = null; try { guiceFilter.init(createMock(FilterConfig.class)); } catch (ServletException e) { se = e; } finally { assertNotNull("Servlet exception was not thrown with wrong scope binding", se); } }
private void deconfigure() { if (services != null) { services.stop(); } // Workaround for the WARNING: Multiple Servlet injectors detected. new GuiceFilter().destroy(); } }
@Override public final void setUp() { inits = 0; doFilters = 0; destroys = 0; GuiceFilter.reset(); }
@Provides HttpSession provideHttpSession() { return GuiceFilter.getRequest(Key.get(HttpSession.class)).getSession(); }
static HttpServletResponse getResponse(Key<?> key) { return getContext(key).getResponse(); }
/** * This method only works if you are using the {@linkplain GuiceServletContextListener} to create * your injector. Otherwise, it returns null. * * @return The current servlet context. * @since 3.0 */ protected final ServletContext getServletContext() { return GuiceFilter.getServletContext(); }
@Provides @RequestScoped HttpServletResponse provideHttpServletResponse() { return GuiceFilter.getResponse(Key.get(HttpServletResponse.class)); }
public void testNewSessionObject() 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; assertNotNull(injector.getInstance(InSession.class)); assertNull(injector.getInstance(IN_SESSION_NULL_KEY)); } }; filter.doFilter(request, null, filterChain); assertTrue(invoked[0]); }
public final void testFilterInNonSingletonScopeThrowsServletException() { GuiceFilter guiceFilter = new GuiceFilter(); Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { filter("/*").through(MyNonSingletonFilter.class); } }); ServletException se = null; try { guiceFilter.init(createMock(FilterConfig.class)); } catch (ServletException e) { se = e; } finally { assertNotNull("Servlet exception was not thrown with wrong scope binding", se); } }
@Override public void setUp() { inits = 0; services = 0; destroys = 0; doFilters = 0; GuiceFilter.reset(); }
@Provides @RequestScoped HttpServletRequest provideHttpServletRequest() { return GuiceFilter.getRequest(Key.get(HttpServletRequest.class)); }
static HttpServletRequest getOriginalRequest(Key<?> key) { return getContext(key).getOriginalRequest(); }