SSIServletpublic class SSIServlet extends HttpServlet Servlet to process SSI requests within a webpage. Mapped to a path from
within web.xml. |
Fields Summary |
---|
protected int | debugDebug level for this servlet. | protected boolean | bufferedShould the output be buffered. | protected Long | expiresExpiration time in seconds for the doc. | protected boolean | isVirtualWebappRelativevirtual path can be webapp-relative | protected String | inputEncodingInput encoding. If not specified, uses platform default | protected String | outputEncodingOutput encoding. If not specified, uses platform default |
Methods Summary |
---|
public void | doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)Process and forward the GET request to our requestHandler() *
if (debug > 0) log("SSIServlet.doGet()");
requestHandler(req, res);
| public void | doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)Process and forward the POST request to our
requestHandler() .
if (debug > 0) log("SSIServlet.doPost()");
requestHandler(req, res);
| public void | init()Initialize this servlet.
//----------------- Public methods.
if (getServletConfig().getInitParameter("debug") != null)
debug = Integer.parseInt(getServletConfig().getInitParameter("debug"));
isVirtualWebappRelative =
Boolean.parseBoolean(getServletConfig().getInitParameter("isVirtualWebappRelative"));
if (getServletConfig().getInitParameter("expires") != null)
expires = Long.valueOf(getServletConfig().getInitParameter("expires"));
buffered = Boolean.parseBoolean(getServletConfig().getInitParameter("buffered"));
inputEncoding = getServletConfig().getInitParameter("inputEncoding");
if (getServletConfig().getInitParameter("outputEncoding") != null)
outputEncoding = getServletConfig().getInitParameter("outputEncoding");
if (debug > 0)
log("SSIServlet.init() SSI invoker started with 'debug'=" + debug);
| protected void | processSSI(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.net.URL resource)
SSIExternalResolver ssiExternalResolver =
new SSIServletExternalResolver(getServletContext(), req, res,
isVirtualWebappRelative, debug, inputEncoding);
SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver,
debug);
PrintWriter printWriter = null;
StringWriter stringWriter = null;
if (buffered) {
stringWriter = new StringWriter();
printWriter = new PrintWriter(stringWriter);
} else {
printWriter = res.getWriter();
}
URLConnection resourceInfo = resource.openConnection();
InputStream resourceInputStream = resourceInfo.getInputStream();
String encoding = resourceInfo.getContentEncoding();
if (encoding == null) {
encoding = inputEncoding;
}
InputStreamReader isr;
if (encoding == null) {
isr = new InputStreamReader(resourceInputStream);
} else {
isr = new InputStreamReader(resourceInputStream, encoding);
}
BufferedReader bufferedReader = new BufferedReader(isr);
long lastModified = ssiProcessor.process(bufferedReader,
resourceInfo.getLastModified(), printWriter);
if (lastModified > 0) {
res.setDateHeader("last-modified", lastModified);
}
if (buffered) {
printWriter.flush();
String text = stringWriter.toString();
res.getWriter().write(text);
}
| protected void | requestHandler(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)Process our request and locate right SSI command.
ServletContext servletContext = getServletContext();
String path = SSIServletRequestUtil.getRelativePath(req);
if (debug > 0)
log("SSIServlet.requestHandler()\n" + "Serving "
+ (buffered?"buffered ":"unbuffered ") + "resource '"
+ path + "'");
// Exclude any resource in the /WEB-INF and /META-INF subdirectories
// (the "toUpperCase()" avoids problems on Windows systems)
if (path == null || path.toUpperCase().startsWith("/WEB-INF")
|| path.toUpperCase().startsWith("/META-INF")) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't serve file: " + path);
return;
}
URL resource = servletContext.getResource(path);
if (resource == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't find file: " + path);
return;
}
String resourceMimeType = servletContext.getMimeType(path);
if (resourceMimeType == null) {
resourceMimeType = "text/html";
}
res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
if (expires != null) {
res.setDateHeader("Expires", (new java.util.Date()).getTime()
+ expires.longValue() * 1000);
}
req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
processSSI(req, res, resource);
|
|