FileDocCategorySizeDatePackage
SSIServlet.javaAPI DocApache Tomcat 6.0.148324Fri Jul 20 04:20:36 BST 2007org.apache.catalina.ssi

SSIServlet

public class SSIServlet extends HttpServlet
Servlet to process SSI requests within a webpage. Mapped to a path from within web.xml.
author
Bip Thelin
author
Amy Roh
author
Dan Sandberg
author
David Becker
version
$Revision: 531303 $, $Date: 2007-04-23 02:24:01 +0200 (lun., 23 avr. 2007) $

Fields Summary
protected int
debug
Debug level for this servlet.
protected boolean
buffered
Should the output be buffered.
protected Long
expires
Expiration time in seconds for the doc.
protected boolean
isVirtualWebappRelative
virtual path can be webapp-relative
protected String
inputEncoding
Input encoding. If not specified, uses platform default
protected String
outputEncoding
Output encoding. If not specified, uses platform default
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
Process and forward the GET request to our requestHandler()*

param
req a value of type 'HttpServletRequest'
param
res a value of type 'HttpServletResponse'
exception
IOException if an error occurs
exception
ServletException if an error occurs

        if (debug > 0) log("SSIServlet.doGet()");
        requestHandler(req, res);
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
Process and forward the POST request to our requestHandler().

param
req a value of type 'HttpServletRequest'
param
res a value of type 'HttpServletResponse'
exception
IOException if an error occurs
exception
ServletException if an error occurs

        if (debug > 0) log("SSIServlet.doPost()");
        requestHandler(req, res);
    
public voidinit()
Initialize this servlet.

exception
ServletException if an error occurs



    //----------------- 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 voidprocessSSI(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 voidrequestHandler(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
Process our request and locate right SSI command.

param
req a value of type 'HttpServletRequest'
param
res a value of type 'HttpServletResponse'

        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);