use of javax.servlet.RequestDispatcher in project tomcat by apache.
the class SSIServletExternalResolver method getFileText.
//We are making lots of unnecessary copies of the included data here. If
//someone ever complains that this is slow, we should connect the included
// stream to the print writer that SSICommand uses.
@Override
public String getFileText(String originalPath, boolean virtual) throws IOException {
try {
ServletContextAndPath csAndP = getServletContextAndPath(originalPath, virtual);
ServletContext context = csAndP.getServletContext();
String path = csAndP.getPath();
RequestDispatcher rd = context.getRequestDispatcher(path);
if (rd == null) {
throw new IOException("Couldn't get request dispatcher for path: " + path);
}
ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(context, req, res, basos);
rd.include(req, responseIncludeWrapper);
//We can't assume the included servlet flushed its output
responseIncludeWrapper.flushOutputStreamOrWriter();
byte[] bytes = basos.toByteArray();
//Assume platform default encoding unless otherwise specified
String retVal;
if (inputEncoding == null) {
retVal = new String(bytes);
} else {
retVal = new String(bytes, B2CConverter.getCharset(inputEncoding));
}
//were included, but not sure how else to tell.
if (retVal.equals("") && !req.getMethod().equalsIgnoreCase("HEAD")) {
throw new IOException("Couldn't find file: " + path);
}
return retVal;
} catch (ServletException e) {
throw new IOException("Couldn't include file: " + originalPath + " because of ServletException: " + e.getMessage());
}
}
use of javax.servlet.RequestDispatcher in project tomcat by apache.
the class JspRuntimeLibrary method include.
/**
* Perform a RequestDispatcher.include() operation, with optional flushing
* of the response beforehand.
*
* @param request The servlet request we are processing
* @param response The servlet response we are processing
* @param relativePath The relative path of the resource to be included
* @param out The Writer to whom we are currently writing
* @param flush Should we flush before the include is processed?
*
* @exception IOException if thrown by the included servlet
* @exception ServletException if thrown by the included servlet
*/
public static void include(ServletRequest request, ServletResponse response, String relativePath, JspWriter out, boolean flush) throws IOException, ServletException {
if (flush && !(out instanceof BodyContent))
out.flush();
// FIXME - It is tempting to use request.getRequestDispatcher() to
// resolve a relative path directly, but Catalina currently does not
// take into account whether the caller is inside a RequestDispatcher
// include or not. Whether Catalina *should* take that into account
// is a spec issue currently under review. In the mean time,
// replicate Jasper's previous behavior
String resourcePath = getContextRelativePath(request, relativePath);
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
rd.include(request, new ServletResponseWrapperInclude(response, out));
}
use of javax.servlet.RequestDispatcher in project jetty.project by eclipse.
the class ConcatServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getQueryString();
if (query == null) {
response.sendError(HttpServletResponse.SC_NO_CONTENT);
return;
}
List<RequestDispatcher> dispatchers = new ArrayList<>();
String[] parts = query.split("\\&");
String type = null;
for (String part : parts) {
String path = URIUtil.canonicalPath(URIUtil.decodePath(part));
if (path == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// Verify that the path is not protected.
if (startsWith(path, "/WEB-INF/") || startsWith(path, "/META-INF/")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String t = getServletContext().getMimeType(path);
if (t != null) {
if (type == null) {
type = t;
} else if (!type.equals(t)) {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
return;
}
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
if (dispatcher != null)
dispatchers.add(dispatcher);
}
if (type != null)
response.setContentType(type);
for (RequestDispatcher dispatcher : dispatchers) dispatcher.include(request, response);
}
use of javax.servlet.RequestDispatcher in project jetty.project by eclipse.
the class DispatchServlet method doGet.
/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException {
if (sreq.getParameter("wrap") != null) {
sreq = new HttpServletRequestWrapper(sreq);
sres = new HttpServletResponseWrapper(sres);
}
if (sreq.getParameter("session") != null)
sreq.getSession(true);
String prefix = sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath();
String info;
if (sreq.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) != null)
info = (String) sreq.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
else
info = sreq.getPathInfo();
if (info == null)
info = "NULL";
if (info.indexOf(sreq.getServletPath()) > 0) {
sres.sendError(403, "Nested " + sreq.getServletPath() + " forbidden.");
return;
}
if (info.indexOf(getServletName()) > 0) {
sres.sendError(403, "Nested " + getServletName() + " forbidden.");
return;
}
if (info.startsWith("/includeW/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
PrintWriter pout = null;
pout = sres.getWriter();
pout.write("<H1>Include (writer): " + info + "</H1><HR>");
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
pout = sres.getWriter();
pout.write("<H1>Null dispatcher</H1>");
} else
dispatch.include(sreq, sres);
pout.write("<HR><H1>-- Included (writer)</H1>");
} else if (info.startsWith("/includeS/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
OutputStream out = null;
out = sres.getOutputStream();
out.write(("<H1>Include (outputstream): " + info + "</H1><HR>").getBytes());
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
out = sres.getOutputStream();
out.write("<H1>Null dispatcher</H1>".getBytes());
} else
dispatch.include(sreq, sres);
out.write("<HR><H1>-- Included (outputstream)</H1>".getBytes());
} else if (info.startsWith("/forward/")) {
info = info.substring(8);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch != null) {
ServletOutputStream out = sres.getOutputStream();
out.print("Can't see this");
dispatch.forward(sreq, sres);
try {
// should be closed
out.println("IOException");
// should not get here
throw new IllegalStateException();
} catch (IOException e) {
// getServletContext().log("ignore",e);
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/forwardC/")) {
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
String cpath = info.substring(0, info.indexOf('/', 1));
info = info.substring(cpath.length());
ServletContext context = getServletContext().getContext(cpath);
RequestDispatcher dispatch = context.getRequestDispatcher(info);
if (dispatch != null) {
dispatch.forward(sreq, sres);
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/includeN/")) {
sres.setContentType("text/html");
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
PrintWriter pout;
if (info.startsWith("/null"))
info = info.substring(5);
else {
pout = sres.getWriter();
pout.write("<H1>Include named: " + info + "</H1><HR>");
}
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.include(sreq, sres);
else {
pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
}
pout = sres.getWriter();
pout.write("<HR><H1>Included ");
} else if (info.startsWith("/forwardN/")) {
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.forward(sreq, sres);
else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
pout.flush();
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>Dispatch URL must be of the form: </H1>" + "<PRE>" + prefix + "/includeW/path\n" + prefix + "/includeS/path\n" + prefix + "/forward/path\n" + prefix + "/includeN/name\n" + prefix + "/forwardC/_context/path\n</PRE>");
}
}
use of javax.servlet.RequestDispatcher in project sonarqube by SonarSource.
the class RequestProcessor method doInclude.
/**
* <p>Do an include of specified URI using a <code>RequestDispatcher</code>.
* This method is used by all internal method needing to do an
* include.</p>
*
* @param uri Context-relative URI to include
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
* @since Struts 1.1
*/
protected void doInclude(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.include(request, response);
}
Aggregations