use of javax.servlet.RequestDispatcher in project roboguice by roboguice.
the class ServletPipelineRequestDispatcherTest method testForwardToManagedServlet.
public final void testForwardToManagedServlet() throws IOException, ServletException {
String pattern = "blah.html";
final ServletDefinition servletDefinition = new ServletDefinition(pattern, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
final Injector injector = createMock(Injector.class);
final Binding binding = createMock(Binding.class);
final HttpServletRequest requestMock = createMock(HttpServletRequest.class);
final HttpServletResponse mockResponse = createMock(HttpServletResponse.class);
expect(requestMock.getAttribute(A_KEY)).andReturn(A_VALUE);
requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true);
requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST);
expect(mockResponse.isCommitted()).andReturn(false);
mockResponse.resetBuffer();
expectLastCall().once();
final List<String> paths = new ArrayList<String>();
final HttpServlet mockServlet = new HttpServlet() {
protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
paths.add(request.getRequestURI());
final Object o = request.getAttribute(A_KEY);
assertEquals("Wrong attrib returned - " + o, A_VALUE, o);
}
};
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
expect(injector.getInstance(HTTP_SERLVET_KEY)).andReturn(mockServlet);
final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class));
Binding<ServletDefinition> mockBinding = createMock(Binding.class);
expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))).andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding));
Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition);
expect(mockBinding.getProvider()).andReturn(bindingProvider);
replay(injector, binding, requestMock, mockResponse, mockBinding);
// Have to init the Servlet before we can dispatch to it.
servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet());
final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern);
assertNotNull(dispatcher);
dispatcher.forward(requestMock, mockResponse);
assertTrue("Include did not dispatch to our servlet!", paths.contains(pattern));
verify(injector, requestMock, mockResponse, mockBinding);
}
use of javax.servlet.RequestDispatcher in project spring-framework by spring-projects.
the class MockServletContextTests method setDefaultServletName.
@Test
public void setDefaultServletName() throws Exception {
final String originalDefault = "default";
final String newDefault = "test";
assertNotNull(sc.getNamedDispatcher(originalDefault));
sc.setDefaultServletName(newDefault);
assertEquals(newDefault, sc.getDefaultServletName());
assertNull(sc.getNamedDispatcher(originalDefault));
RequestDispatcher namedDispatcher = sc.getNamedDispatcher(newDefault);
assertNotNull(namedDispatcher);
MockHttpServletResponse response = new MockHttpServletResponse();
namedDispatcher.forward(new MockHttpServletRequest(sc), response);
assertEquals(newDefault, response.getForwardedUrl());
}
use of javax.servlet.RequestDispatcher in project spring-framework by spring-projects.
the class MockServletContextTests method registerAndUnregisterNamedDispatcher.
@Test
public void registerAndUnregisterNamedDispatcher() throws Exception {
final String name = "test-servlet";
final String url = "/test";
assertNull(sc.getNamedDispatcher(name));
sc.registerNamedDispatcher(name, new MockRequestDispatcher(url));
RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name);
assertNotNull(namedDispatcher);
MockHttpServletResponse response = new MockHttpServletResponse();
namedDispatcher.forward(new MockHttpServletRequest(sc), response);
assertEquals(url, response.getForwardedUrl());
sc.unregisterNamedDispatcher(name);
assertNull(sc.getNamedDispatcher(name));
}
use of javax.servlet.RequestDispatcher in project spring-framework by spring-projects.
the class InternalResourceView method renderMergedOutputModel.
/**
* Render the internal resource given the specified model.
* This includes setting the model as request attributes.
*/
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Expose the model object as request attributes.
exposeModelAsRequestAttributes(model, request);
// Expose helpers as request attributes, if any.
exposeHelpers(request);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = getRequestDispatcher(request, dispatcherPath);
if (rd == null) {
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!");
}
// If already included or response already committed, perform include, else forward.
if (useInclude(request, response)) {
response.setContentType(getContentType());
if (logger.isDebugEnabled()) {
logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.include(request, response);
} else {
// Note: The forwarded resource is supposed to determine the content type itself.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.forward(request, response);
}
}
use of javax.servlet.RequestDispatcher in project spring-framework by spring-projects.
the class DefaultServletHttpRequestHandler method handleRequest.
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
if (rd == null) {
throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" + this.defaultServletName + "'");
}
rd.forward(request, response);
}
Aggregations