Search in sources :

Example 46 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project felix by apache.

the class RequestDispatchTest method testDispatchIncludeRelativeURIOk.

/**
 * Tests that we can include content from other servlets using the {@link RequestDispatcher} service.
 */
@Test
public void testDispatchIncludeRelativeURIOk() throws Exception {
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    TestServlet servlet = new TestServlet(initLatch, destroyLatch) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Object contextPathAttr = req.getAttribute(INCLUDE_CONTEXT_PATH);
            if (contextPathAttr != null) {
                assertEquals("", req.getContextPath());
                assertEquals("/foo", req.getPathInfo());
                assertEquals("/test", req.getServletPath());
                assertEquals("/test/foo", req.getRequestURI());
                assertEquals("bar=qux&quu", req.getQueryString());
                assertEquals("", contextPathAttr);
                assertEquals("/test", req.getAttribute(INCLUDE_SERVLET_PATH));
                // assertEquals("/include", req.getAttribute(INCLUDE_PATH_INFO));
                // assertEquals("/test/include", req.getAttribute(INCLUDE_REQUEST_URI));
                assertEquals(null, req.getAttribute(INCLUDE_QUERY_STRING));
                resp.getWriter().print("INCLUDE\n");
            } else {
                assertEquals("", req.getContextPath());
                assertEquals("/test", req.getServletPath());
                // assertEquals("/foo", req.getPathInfo());
                // assertEquals("/test/foo", req.getRequestURI());
                assertEquals("bar=qux&quu", req.getQueryString());
                resp.getWriter().print("BEFORE\n");
                // ServletContext#getRequestDispatcher only takes absolute paths...
                RequestDispatcher disp = req.getServletContext().getRequestDispatcher("include");
                assertNull("ServletContext returned RequestDispatcher for relative path?!", disp);
                // Causes a request to ourselves being made (/test/forward)...
                disp = req.getRequestDispatcher("include");
                assertNotNull("ServletRequest returned NO RequestDispatcher for relative path?!", disp);
                disp.include(req, resp);
                resp.getWriter().print("AFTER\n");
            }
        }
    };
    register("/test", servlet);
    assertTrue(initLatch.await(5, TimeUnit.SECONDS));
    assertContent("BEFORE\nINCLUDE\nAFTER\n", createURL("/test/foo?bar=qux&quu"));
    unregister("/test");
    assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) CountDownLatch(java.util.concurrent.CountDownLatch) RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.Test)

Example 47 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project entando-core by entando.

the class AbstractWidgetExecutorService method extractJspOutput.

protected static String extractJspOutput(RequestContext reqCtx, String jspPath) throws ServletException, IOException {
    HttpServletRequest request = reqCtx.getRequest();
    HttpServletResponse response = reqCtx.getResponse();
    BufferedHttpResponseWrapper wrapper = new BufferedHttpResponseWrapper(response);
    ServletContext context = request.getSession().getServletContext();
    String url = response.encodeRedirectURL(jspPath);
    RequestDispatcher dispatcher = context.getRequestDispatcher(url);
    dispatcher.include(request, wrapper);
    return wrapper.getOutput();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 48 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project sling by apache.

the class SyntheticResourceFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final String resourceType = getResourceTypeFromSuffix(slingRequest);
    final Configuration config = configurationWhiteboard.getConfiguration(slingRequest, resourceType);
    if (config == null || !config.hasIncludeSelector(slingRequest) || !ResourceUtil.isSyntheticResource(slingRequest.getResource())) {
        chain.doFilter(request, response);
        return;
    }
    final RequestDispatcherOptions options = new RequestDispatcherOptions();
    options.setForceResourceType(resourceType);
    final RequestDispatcher dispatcher = slingRequest.getRequestDispatcher(slingRequest.getResource(), options);
    dispatcher.forward(request, response);
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 49 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project processdash by dtuma.

the class TinyCGIBase method retrieveParamsFromServlet.

protected void retrieveParamsFromServlet(String servletUriParamName) throws IOException {
    String servletUri = getParameter(servletUriParamName);
    if (StringUtils.hasValue(servletUri)) {
        try {
            HttpServletRequest req = (HttpServletRequest) env.get(HttpServletRequest.class);
            HttpServletResponse resp = (HttpServletResponse) env.get(HttpServletResponse.class);
            if (!servletUri.startsWith("/"))
                servletUri = resolveRelativeURI(servletUri);
            RequestDispatcher disp = req.getRequestDispatcher(servletUri);
            disp.include(req, resp);
            Object params = req.getAttribute("REQUEST_PARAMS");
            if (params instanceof Map)
                parameters.putAll((Map) params);
            else
                throw new TinyCGIException(404, "Could not retrieve " + servletUri);
            req.removeAttribute("REQUEST_PARAMS");
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) RequestDispatcher(javax.servlet.RequestDispatcher) TinyCGIException(net.sourceforge.processdash.net.http.TinyCGIException) MalformedURLException(java.net.MalformedURLException) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) TinyCGIException(net.sourceforge.processdash.net.http.TinyCGIException)

Example 50 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project sling by apache.

the class FormServlet method forward.

private void forward(final Form form, final SlingHttpServletRequest request, final SlingHttpServletResponse response, final RequestDispatcherOptions options) throws ServletException, IOException {
    final FormValidationHttpServletRequestWrapper requestWrapper = new FormValidationHttpServletRequestWrapper(request, form);
    final RequestDispatcher dispatcher = request.getRequestDispatcher(request.getResource(), options);
    dispatcher.forward(requestWrapper, response);
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (javax.servlet.RequestDispatcher)354 ServletException (javax.servlet.ServletException)98 HttpSession (javax.servlet.http.HttpSession)97 IOException (java.io.IOException)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)56 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 SQLException (java.sql.SQLException)31 User (com.zyf.bean.User)28 ServletContext (javax.servlet.ServletContext)26 Properties (java.util.Properties)14 Test (org.junit.Test)14 RelatorioDAO (br.senac.tads3.pi03b.gruposete.dao.RelatorioDAO)13 RelatorioMudancas (br.senac.tads3.pi03b.gruposete.models.RelatorioMudancas)12 PrintWriter (java.io.PrintWriter)12 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)11 Resource (org.apache.sling.api.resource.Resource)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 GenericValue (org.apache.ofbiz.entity.GenericValue)9 WebUser (org.compiere.util.WebUser)9