@Inject public ManagedServletPipeline(Injector injector) { this.servletDefinitions = collectServletDefinitions(injector); }
@Override public RequestDispatcher getRequestDispatcher(String path) { final RequestDispatcher dispatcher = servletPipeline.getRequestDispatcher(path); return (null != dispatcher) ? dispatcher : super.getRequestDispatcher(path); } };
@Override public void destroyPipeline() { //destroy servlets first servletPipeline.destroy(); //go down chain and destroy all our filters Set<Filter> destroyedSoFar = Sets.newIdentityHashSet(); for (FilterDefinition filterDefinition : filterDefinitions) { filterDefinition.destroy(destroyedSoFar); } } }
new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
@Override public synchronized void initPipeline(ServletContext servletContext) throws ServletException { //double-checked lock, prevents duplicate initialization if (initialized) return; // Used to prevent duplicate initialization. Set<Filter> initializedSoFar = Sets.newIdentityHashSet(); for (FilterDefinition filterDefinition : filterDefinitions) { filterDefinition.init(servletContext, injector, initializedSoFar); } //next, initialize servlets... servletPipeline.init(servletContext, injector); //everything was ok... initialized = true; }
} else { final boolean serviced = servletPipeline.service(servletRequest, servletResponse);
/** * Used to create an proxy that dispatches either to the guice-servlet pipeline or the regular * pipeline based on uri-path match. This proxy also provides minimal forwarding support. * * <p>We cannot forward from a web.xml Servlet/JSP to a guice-servlet (because the filter pipeline * is not called again). However, we can wrap requests with our own dispatcher to forward the * *other* way. web.xml Servlets/JSPs can forward to themselves as per normal. * * <p>This is not a problem cuz we intend for people to migrate from web.xml to guice-servlet, * incrementally, but not the other way around (which, we should actively discourage). */ @SuppressWarnings({"JavaDoc", "deprecation"}) private ServletRequest withDispatcher( ServletRequest servletRequest, final ManagedServletPipeline servletPipeline) { // don't wrap the request if there are no servlets mapped. This prevents us from inserting our // wrapper unless it's actually going to be used. This is necessary for compatibility for apps // that downcast their HttpServletRequests to a concrete implementation. if (!servletPipeline.hasServletsMapped()) { return servletRequest; } HttpServletRequest request = (HttpServletRequest) servletRequest; //noinspection OverlyComplexAnonymousInnerClass return new HttpServletRequestWrapper(request) { @Override public RequestDispatcher getRequestDispatcher(String path) { final RequestDispatcher dispatcher = servletPipeline.getRequestDispatcher(path); return (null != dispatcher) ? dispatcher : super.getRequestDispatcher(path); } }; }
public final void testWrappedRequestUrlNegativePort() { final HttpServletRequest mockRequest = createMock(HttpServletRequest.class); expect(mockRequest.getScheme()).andReturn("http"); expect(mockRequest.getServerName()).andReturn("the.server"); expect(mockRequest.getServerPort()).andReturn(-1); replay(mockRequest); HttpServletRequest wrappedRequest = ManagedServletPipeline.wrapRequest(mockRequest, "/new-uri"); assertEquals("/new-uri", wrappedRequest.getRequestURI()); assertEquals("http://the.server/new-uri", wrappedRequest.getRequestURL().toString()); }
new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
@Override public synchronized void initPipeline(ServletContext servletContext) throws ServletException { //double-checked lock, prevents duplicate initialization if (initialized) return; // Used to prevent duplicate initialization. Set<Filter> initializedSoFar = Sets.newIdentityHashSet(); for (FilterDefinition filterDefinition : filterDefinitions) { filterDefinition.init(servletContext, injector, initializedSoFar); } //next, initialize servlets... servletPipeline.init(servletContext, injector); //everything was ok... initialized = true; }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { index++; //dispatch down the chain while there are more filters if (index < filterDefinitions.length) { filterDefinitions[index].doFilter(servletRequest, servletResponse, this); } else { //we've reached the end of the filterchain, let's try to dispatch to a servlet final boolean serviced = servletPipeline.service(servletRequest, servletResponse); //dispatch to the normal filter chain only if one of our servlets did not match if (!serviced) { proceedingChain.doFilter(servletRequest, servletResponse); } } } }
/** * Used to create an proxy that dispatches either to the guice-servlet pipeline or the regular * pipeline based on uri-path match. This proxy also provides minimal forwarding support. * * We cannot forward from a web.xml Servlet/JSP to a guice-servlet (because the filter pipeline * is not called again). However, we can wrap requests with our own dispatcher to forward the * *other* way. web.xml Servlets/JSPs can forward to themselves as per normal. * * This is not a problem cuz we intend for people to migrate from web.xml to guice-servlet, * incrementally, but not the other way around (which, we should actively discourage). */ @SuppressWarnings({ "JavaDoc", "deprecation" }) private ServletRequest withDispatcher(ServletRequest servletRequest, final ManagedServletPipeline servletPipeline) { HttpServletRequest request = (HttpServletRequest) servletRequest; // don't wrap the request if there are no servlets mapped. This prevents us from inserting our // wrapper unless it's actually going to be used. This is necessary for compatibility for apps // that downcast their HttpServletRequests to a concrete implementation. if (!servletPipeline.hasServletsMapped()) { return servletRequest; } //noinspection OverlyComplexAnonymousInnerClass return new HttpServletRequestWrapper(request) { @Override public RequestDispatcher getRequestDispatcher(String path) { final RequestDispatcher dispatcher = servletPipeline.getRequestDispatcher(path); return (null != dispatcher) ? dispatcher : super.getRequestDispatcher(path); } }; }
public final void testWrappedRequestUrlDefaultPort() { final HttpServletRequest mockRequest = createMock(HttpServletRequest.class); expect(mockRequest.getScheme()).andReturn("http"); expect(mockRequest.getServerName()).andReturn("the.server"); expect(mockRequest.getServerPort()).andReturn(80); replay(mockRequest); HttpServletRequest wrappedRequest = ManagedServletPipeline.wrapRequest(mockRequest, "/new-uri"); assertEquals("/new-uri", wrappedRequest.getRequestURI()); assertEquals("http://the.server/new-uri", wrappedRequest.getRequestURL().toString()); }
@Inject public ManagedServletPipeline(Injector injector) { this.servletDefinitions = collectServletDefinitions(injector); }
@Override public RequestDispatcher getRequestDispatcher(String path) { final RequestDispatcher dispatcher = servletPipeline.getRequestDispatcher(path); return (null != dispatcher) ? dispatcher : super.getRequestDispatcher(path); } };
new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
@Override public void destroyPipeline() { //destroy servlets first servletPipeline.destroy(); //go down chain and destroy all our filters Set<Filter> destroyedSoFar = Sets.newIdentityHashSet(); for (FilterDefinition filterDefinition : filterDefinitions) { filterDefinition.destroy(destroyedSoFar); } } }
public synchronized void initPipeline(ServletContext servletContext) throws ServletException { //double-checked lock, prevents duplicate initialization if (initialized) return; // Used to prevent duplicate initialization. Set<Filter> initializedSoFar = Sets.newSetFromMap(Maps.<Filter, Boolean>newIdentityHashMap()); for (FilterDefinition filterDefinition : filterDefinitions) { filterDefinition.init(servletContext, injector, initializedSoFar); } //next, initialize servlets... servletPipeline.init(servletContext, injector); //everything was ok... initialized = true; }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { index++; //dispatch down the chain while there are more filters if (index < filterDefinitions.length) { filterDefinitions[index].doFilter(servletRequest, servletResponse, this); } else { //we've reached the end of the filterchain, let's try to dispatch to a servlet final boolean serviced = servletPipeline.service(servletRequest, servletResponse); //dispatch to the normal filter chain only if one of our servlets did not match if (!serviced) { proceedingChain.doFilter(servletRequest, servletResponse); } } } }
/** * Used to create an proxy that dispatches either to the guice-servlet pipeline or the regular * pipeline based on uri-path match. This proxy also provides minimal forwarding support. * <p/> * We cannot forward from a web.xml Servlet/JSP to a guice-servlet (because the filter pipeline * is not called again). However, we can wrap requests with our own dispatcher to forward the * *other* way. web.xml Servlets/JSPs can forward to themselves as per normal. * <p/> * This is not a problem cuz we intend for people to migrate from web.xml to guice-servlet, * incrementally, but not the other way around (which, we should actively discourage). */ @SuppressWarnings({"JavaDoc", "deprecation"}) private ServletRequest withDispatcher(ServletRequest servletRequest, final ManagedServletPipeline servletPipeline) { HttpServletRequest request = (HttpServletRequest) servletRequest; // don't wrap the request if there are no servlets mapped. This prevents us from inserting our // wrapper unless it's actually going to be used. This is necessary for compatibility for apps // that downcast their HttpServletRequests to a concrete implementation. if (!servletPipeline.hasServletsMapped()) { return servletRequest; } //noinspection OverlyComplexAnonymousInnerClass return new HttpServletRequestWrapper(request) { @Override public RequestDispatcher getRequestDispatcher(String path) { final RequestDispatcher dispatcher = servletPipeline.getRequestDispatcher(path); return (null != dispatcher) ? dispatcher : super.getRequestDispatcher(path); } }; }