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));
}
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();
}
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);
}
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();
}
}
}
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);
}
Aggregations