Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.catalina.ssi;
 
 
 import java.net.URL;
 
Servlet to process SSI requests within a webpage. Mapped to a path from within web.xml.

Author(s):
Bip Thelin
Amy Roh
Dan Sandberg
David Becker
Version:
$Id: SSIServlet.java 1534 2010-08-18 12:40:08Z remy.maucherat@jboss.com $
 
 public class SSIServlet extends HttpServlet {
    
Debug level for this servlet.
 
     protected int debug = 0;
    
Should the output be buffered.
 
     protected boolean buffered = false;
    
Expiration time in seconds for the doc.
 
     protected Long expires = null;
    
virtual path can be webapp-relative
 
     protected boolean isVirtualWebappRelative = false;
    
Input encoding. If not specified, uses platform default
 
     protected String inputEncoding = null;
    
Output encoding. If not specified, uses platform default
 
     protected String outputEncoding = "UTF-8";
    
Allow exec (normally blocked for security)
 
     protected boolean allowExec = false;
 
 
     //----------------- Public methods.
     
Initialize this servlet.

Throws:
javax.servlet.ServletException if an error occurs
 
     @Override
     public void init() throws ServletException {
         
         if (getServletConfig().getInitParameter("debug") != null)
              = Integer.parseInt(getServletConfig().getInitParameter("debug"));
         
          = 
             Boolean.parseBoolean(getServletConfig().getInitParameter("isVirtualWebappRelative"));
         
         if (getServletConfig().getInitParameter("expires") != null)
              = Long.valueOf(getServletConfig().getInitParameter("expires"));
         
          = Boolean.parseBoolean(getServletConfig().getInitParameter("buffered"));
         
          = getServletConfig().getInitParameter("inputEncoding");
         
         if (getServletConfig().getInitParameter("outputEncoding") != null)
              = getServletConfig().getInitParameter("outputEncoding");
         
          = Boolean.parseBoolean(
                 getServletConfig().getInitParameter("allowExec"));
 
         if ( > 0)
             log("SSIServlet.init() SSI invoker started with 'debug'=" + );
 
     }


    
Process and forward the GET request to our requestHandler()*

Parameters:
req a value of type 'HttpServletRequest'
res a value of type 'HttpServletResponse'
Throws:
java.io.IOException if an error occurs
javax.servlet.ServletException if an error occurs
    @Override
    public void doGet(HttpServletRequest reqHttpServletResponse res)
            throws IOExceptionServletException {
        if ( > 0) log("SSIServlet.doGet()");
        requestHandler(reqres);
    }


    
Process and forward the POST request to our requestHandler().

Parameters:
req a value of type 'HttpServletRequest'
res a value of type 'HttpServletResponse'
Throws:
java.io.IOException if an error occurs
javax.servlet.ServletException if an error occurs
    @Override
    public void doPost(HttpServletRequest reqHttpServletResponse res)
            throws IOExceptionServletException {
        if ( > 0) log("SSIServlet.doPost()");
        requestHandler(reqres);
    }


    
Process our request and locate right SSI command.

Parameters:
req a value of type 'HttpServletRequest'
res a value of type 'HttpServletResponse'
    protected void requestHandler(HttpServletRequest req,
            HttpServletResponse resthrows IOException {
        ServletContext servletContext = getServletContext();
        String path = SSIServletRequestUtil.getRelativePath(req);
        if ( > 0)
            log("SSIServlet.requestHandler()\n" + "Serving "
                    + (?"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(.path);
            return;
        }
        URL resource = servletContext.getResource(path);
        if (resource == null) {
            res.sendError(.path);
            return;
        }
        String resourceMimeType = servletContext.getMimeType(path);
        if (resourceMimeType == null) {
            resourceMimeType = "text/html";
        }
        res.setContentType(resourceMimeType + ";charset=" + );
        if ( != null) {
            res.setDateHeader("Expires", (new java.util.Date()).getTime()
                    + .longValue() * 1000);
        }
        req.setAttribute(."true");
        processSSI(reqresresource);
    }
    protected void processSSI(HttpServletRequest reqHttpServletResponse res,
            URL resourcethrows IOException {
        SSIExternalResolver ssiExternalResolver =
            new SSIServletExternalResolver(getServletContext(), reqres,
                    );
        SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver,
                );
        PrintWriter printWriter = null;
        StringWriter stringWriter = null;
        if () {
            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 = ;
        }
        InputStreamReader isr;
        if (encoding == null) {
            isr = new InputStreamReader(resourceInputStream);
        } else {
            isr = new InputStreamReader(resourceInputStreamencoding);
        }
        BufferedReader bufferedReader = new BufferedReader(isr);
        long lastModified = ssiProcessor.process(bufferedReader,
                resourceInfo.getLastModified(), printWriter);
        if (lastModified > 0) {
            res.setDateHeader("last-modified"lastModified);
        }
        if () {
            printWriter.flush();
            String text = stringWriter.toString();
            res.getWriter().write(text);
        }
        bufferedReader.close();
    }
New to GrepCode? Check out our FAQ X