Search in sources :

Example 21 with RequestDispatcher

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);
}
Also used : Binding(com.google.inject.Binding) HttpServlet(javax.servlet.http.HttpServlet) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) Injector(com.google.inject.Injector) EasyMock.anyObject(org.easymock.EasyMock.anyObject)

Example 22 with RequestDispatcher

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());
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.Test)

Example 23 with RequestDispatcher

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));
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.Test)

Example 24 with RequestDispatcher

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 25 with RequestDispatcher

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);
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (javax.servlet.RequestDispatcher)203 ServletException (javax.servlet.ServletException)63 HttpSession (javax.servlet.http.HttpSession)58 IOException (java.io.IOException)41 SQLException (java.sql.SQLException)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 Properties (java.util.Properties)14 ServletContext (javax.servlet.ServletContext)12 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 WebUser (org.compiere.util.WebUser)9 ClienteDAO (br.senac.tads3.pi03b.gruposete.dao.ClienteDAO)8 PrintWriter (java.io.PrintWriter)8 ServletResponse (javax.servlet.ServletResponse)8 Cliente (br.senac.tads3.pi03b.gruposete.models.Cliente)7 ServletRequest (javax.servlet.ServletRequest)7 User (org.opennms.netmgt.config.users.User)7 HotelDAO (br.senac.tads3.pi03b.gruposete.dao.HotelDAO)6 VooDAO (br.senac.tads3.pi03b.gruposete.dao.VooDAO)6