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 static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.net.URL;
 import java.util.Date;
 
An implementation of SSIExternalResolver that is used with servlets.

Author(s):
Dan Sandberg
David Becker
Version:
$Revision: 1473 $, $Date: 2010-05-17 19:46:58 +0200 (Mon, 17 May 2010) $
 
 public class SSIServletExternalResolver implements SSIExternalResolver {
     protected final String VARIABLE_NAMES[] = {"AUTH_TYPE""CONTENT_LENGTH",
             "CONTENT_TYPE""DOCUMENT_NAME""DOCUMENT_URI",
             "GATEWAY_INTERFACE""HTTP_ACCEPT""HTTP_ACCEPT_ENCODING",
             "HTTP_ACCEPT_LANGUAGE""HTTP_CONNECTION""HTTP_HOST",
             "HTTP_REFERER""HTTP_USER_AGENT""PATH_INFO""PATH_TRANSLATED",
             "QUERY_STRING""QUERY_STRING_UNESCAPED""REMOTE_ADDR",
             "REMOTE_HOST""REMOTE_PORT""REMOTE_USER""REQUEST_METHOD",
             "REQUEST_URI""SCRIPT_FILENAME""SCRIPT_NAME""SERVER_ADDR",
             "SERVER_NAME""SERVER_PORT""SERVER_PROTOCOL""SERVER_SOFTWARE",
             "UNIQUE_ID"};
     protected ServletContext context;
     protected HttpServletRequest req;
     protected HttpServletResponse res;
     protected boolean isVirtualWebappRelative;
     protected int debug;
     protected String inputEncoding;
 
     public SSIServletExternalResolver(ServletContext context,
             HttpServletRequest reqHttpServletResponse res,
             boolean isVirtualWebappRelativeint debugString inputEncoding) {
         this. = context;
         this. = req;
         this. = res;
         this. = isVirtualWebappRelative;
         this. = debug;
         this. = inputEncoding;
     }
 
 
     public void log(String messageThrowable throwable) {
         //We can't assume that Servlet.log( message, null )
         //is the same as Servlet.log( message ), since API
         //doesn't seem to say so.
         if (throwable != null) {
             .log(messagethrowable);
         } else {
             .log(message);
         }
     }
 
 
     public void addVariableNames(Collection variableNames) {
         for (int i = 0; i < .i++) {
             String variableName = [i];
             String variableValue = getVariableValue(variableName);
             if (variableValue != null) {
                 variableNames.add(variableName);
             }
         }
         Enumeration e = .getAttributeNames();
         while (e.hasMoreElements()) {
             String name = (String)e.nextElement();
            if (!isNameReserved(name)) {
                variableNames.add(name);
            }
        }
    }
    protected Object getReqAttributeIgnoreCase(String targetName) {
        Object object = null;
        if (!isNameReserved(targetName)) {
            object = .getAttribute(targetName);
            if (object == null) {
                Enumeration e = .getAttributeNames();
                while (e.hasMoreElements()) {
                    String name = (String)e.nextElement();
                    if (targetName.equalsIgnoreCase(name)
                            && !isNameReserved(name)) {
                        object = .getAttribute(name);
                        if (object != null) {
                            break;
                        }
                    }
                }
            }
        }
        return object;
    }
    protected boolean isNameReserved(String name) {
        return name.startsWith("java.") || name.startsWith("javax.")
                || name.startsWith("sun.");
    }
    public void setVariableValue(String nameString value) {
        if (!isNameReserved(name)) {
            .setAttribute(namevalue);
        }
    }
    public String getVariableValue(String name) {
        String retVal = null;
        Object object = getReqAttributeIgnoreCase(name);
        if (object != null) {
            retVal = object.toString();
        } else {
            retVal = getCGIVariable(name);
        }
        return retVal;
    }
    protected String getCGIVariable(String name) {
        String retVal = null;
        String[] nameParts = name.toUpperCase(.).split("_");
        int requiredParts = 2;
        if (nameParts.length == 1) {
            if (nameParts[0].equals("PATH")) {
                requiredParts = 1;
                retVal = null// Not implemented
            }
        }
        else if (nameParts[0].equals("AUTH")) {
            if (nameParts[1].equals("TYPE")) {
                retVal = .getAuthType();
            }
        } else if(nameParts[0].equals("CONTENT")) {
            if (nameParts[1].equals("LENGTH")) {
                int contentLength = .getContentLength();
                if (contentLength >= 0) {
                    retVal = Integer.toString(contentLength);
                }
            } else if (nameParts[1].equals("TYPE")) {
                retVal = .getContentType();
            }
        } else if (nameParts[0].equals("DOCUMENT")) {
            if (nameParts[1].equals("NAME")) {
                String requestURI = .getRequestURI();
                retVal = requestURI.substring(requestURI.lastIndexOf('/') + 1);
            } else if (nameParts[1].equals("URI")) {
                retVal = .getRequestURI();
            }
        } else if (name.equalsIgnoreCase("GATEWAY_INTERFACE")) {
            retVal = "CGI/1.1";
        } else if (nameParts[0].equals("HTTP")) {
            if (nameParts[1].equals("ACCEPT")) {
                String accept = null;
                if (nameParts.length == 2) {
                    accept = "Accept";
                } else if (nameParts[2].equals("ENCODING")) {
                    requiredParts = 3;
                    accept = "Accept-Encoding";
                } else if (nameParts[2].equals("LANGUAGE")) {
                    requiredParts = 3;
                    accept = "Accept-Language";
                }
                if (accept != null) {
                    Enumeration acceptHeaders = .getHeaders(accept);
                    if (acceptHeaders != null)
                        if (acceptHeaders.hasMoreElements()) {
                            StringBuilder rv = new StringBuilder(
                                    (StringacceptHeaders.nextElement());
                            while (acceptHeaders.hasMoreElements()) {
                                rv.append(", ");
                                rv.append((StringacceptHeaders.nextElement());
                            }
                        retVal = rv.toString();
                    }
                }
            }
            else if (nameParts[1].equals("CONNECTION")) {
                retVal = .getHeader("Connection");
            }
            else if (nameParts[1].equals("HOST")) {
                retVal = .getHeader("Host");
            }
            else if (nameParts[1].equals("REFERER")) {
                retVal = .getHeader("Referer");
            }
            else if (nameParts[1].equals("USER"))
                if (nameParts.length == 3)
                    if (nameParts[2].equals("AGENT")) {
                        requiredParts = 3;
                        retVal = .getHeader("User-Agent");
                    }
        } else if (nameParts[0].equals("PATH")) {
            if (nameParts[1].equals("INFO")) {
                retVal = .getPathInfo();
            } else if (nameParts[1].equals("TRANSLATED")) {
                retVal = .getPathTranslated();
            }
        } else if (nameParts[0].equals("QUERY")) {
            if (nameParts[1].equals("STRING")) {
                String queryString = .getQueryString();
                if (nameParts.length == 2) {
                    //apache displays this as an empty string rather than (none)
                    retVal = nullToEmptyString(queryString);
                } else if (nameParts[2].equals("UNESCAPED")) {
                    requiredParts = 3;
                    if (queryString != null) {
                        // Use default as a last resort
                        String queryStringEncoding =
                            .;
                
                        String uriEncoding = null;
                        boolean useBodyEncodingForURI = false;
                
                        // Get encoding settings from request / connector if
                        // possible
                        String requestEncoding = .getCharacterEncoding();
                        if ( instanceof Request) {
                            uriEncoding =
                                ((Request)).getConnector().getURIEncoding();
                            useBodyEncodingForURI = ((Request))
                                    .getConnector().getUseBodyEncodingForURI();
                        }
                
                        // If valid, apply settings from request / connector
                        if (uriEncoding != null) {
                            queryStringEncoding = uriEncoding;
                        } else if(useBodyEncodingForURI) {
                            if (requestEncoding != null) {
                                queryStringEncoding = requestEncoding;
                            }
                        }
                
                        try {
                            retVal = URLDecoder.decode(queryString,
                                    queryStringEncoding);                       
                        } catch (UnsupportedEncodingException e) {
                            retVal = queryString;
                        }
                    }
                }
            }
        } else if(nameParts[0].equals("REMOTE")) {
            if (nameParts[1].equals("ADDR")) {
                retVal = .getRemoteAddr();
            } else if (nameParts[1].equals("HOST")) {
                retVal = .getRemoteHost();
            } else if (nameParts[1].equals("IDENT")) {
                retVal = null// Not implemented
            } else if (nameParts[1].equals("PORT")) {
                retVal = Integer.toString.getRemotePort());
            } else if (nameParts[1].equals("USER")) {
                retVal = .getRemoteUser();
            }
        } else if(nameParts[0].equals("REQUEST")) {
            if (nameParts[1].equals("METHOD")) {
                retVal = .getMethod();
            }
            else if (nameParts[1].equals("URI")) {
                // If this is an error page, get the original URI
                retVal = (String.getAttribute(
                        "javax.servlet.forward.request_uri");
                if (retVal == nullretVal=.getRequestURI();
            }
        } else if (nameParts[0].equals("SCRIPT")) {
            String scriptName = .getServletPath();
            if (nameParts[1].equals("FILENAME")) {
                retVal = .getRealPath(scriptName);
            }
            else if (nameParts[1].equals("NAME")) {
                retVal = scriptName;
            }
        } else if (nameParts[0].equals("SERVER")) {
            if (nameParts[1].equals("ADDR")) {
                retVal = .getLocalAddr();
            }
            if (nameParts[1].equals("NAME")) {
                retVal = .getServerName();
            } else if (nameParts[1].equals("PORT")) {
                retVal = Integer.toString(.getServerPort());
            } else if (nameParts[1].equals("PROTOCOL")) {
                retVal = .getProtocol();
            } else if (nameParts[1].equals("SOFTWARE")) {
                StringBuilder rv = new StringBuilder(.getServerInfo());
                rv.append(" ");
                rv.append(System.getProperty("java.vm.name"));
                rv.append("/");
                rv.append(System.getProperty("java.vm.version"));
                rv.append(" ");
                rv.append(System.getProperty("os.name"));
                retVal = rv.toString();
            }
        } else if (name.equalsIgnoreCase("UNIQUE_ID")) {
            retVal = .getRequestedSessionId();
        }
        if (requiredParts != nameParts.lengthreturn null;
            return retVal;
    }
    public Date getCurrentDate() {
        return new Date();
    }
    protected String nullToEmptyString(String string) {
        String retVal = string;
        if (retVal == null) {
            retVal = "";
        }
        return retVal;
    }
    protected String getPathWithoutFileName(String servletPath) {
        String retVal = null;
        int lastSlash = servletPath.lastIndexOf('/');
        if (lastSlash >= 0) {
            //cut off file namee
            retVal = servletPath.substring(0, lastSlash + 1);
        }
        return retVal;
    }
    protected String getPathWithoutContext(final String contextPath,
            final String servletPath) {
        if (servletPath.startsWith(contextPath)) {
            return servletPath.substring(contextPath.length());
        }
        return servletPath;
    }
    protected String getAbsolutePath(String paththrows IOException {
        String pathWithoutContext = SSIServletRequestUtil.getRelativePath();
        String prefix = getPathWithoutFileName(pathWithoutContext);
        if (prefix == null) {
            throw new IOException(.ssiFailedRemovingFilename(pathWithoutContext));
        }
        String fullPath = prefix + path;
        String retVal = SSIServletRequestUtil.normalize(fullPath);
        if (retVal == null) {
            throw new IOException(.ssiFailedNormalization(fullPath));
        }
        return retVal;
    }
            String nonVirtualPaththrows IOException {
        if (nonVirtualPath.startsWith("/") || nonVirtualPath.startsWith("\\")) {
            throw new IOException(.ssiInvalidNonVirtualPath(nonVirtualPath));
        }
        if (nonVirtualPath.indexOf("../") >= 0) {
            throw new IOException(.ssiInvalidNonVirtualPathWithTraversal(nonVirtualPath));
        }
        String path = getAbsolutePath(nonVirtualPath);
        ServletContextAndPath csAndP = new ServletContextAndPath(
                path);
        return csAndP;
    }
            String virtualPaththrows IOException {
        if (!virtualPath.startsWith("/") && !virtualPath.startsWith("\\")) {
            return new ServletContextAndPath(,
                    getAbsolutePath(virtualPath));
        } else {
            String normalized = SSIServletRequestUtil.normalize(virtualPath);
            if () {
                return new ServletContextAndPath(normalized);
            } else {
                ServletContext normContext = .getContext(normalized);
                if (normContext == null) {
                    throw new IOException(.ssiCannotGetContext(normalized));
                }
                //If it's the root context, then there is no context element
                // to remove,
                // ie:
                // '/file1.shtml' vs '/appName1/file1.shtml'
                if (!isRootContext(normContext)) {
                    String noContext = getPathWithoutContext(
                            normContext.getContextPath(), normalized);
                    if (noContext == null) {
                        throw new IOException(.ssiCannotRemoveContext(normalized));
                    }
                    return new ServletContextAndPath(normContextnoContext);
                } else {
                    return new ServletContextAndPath(normContextnormalized);
                }
            }
        }
    }
    //Assumes servletContext is not-null
    //Assumes that identity comparison will be true for the same context
    //Assuming the above, getContext("/") will be non-null as long as the root
    // context is
    // accessible.
    //If it isn't, then servletContext can't be the root context anyway, hence
    // they will
    // not match.
    protected boolean isRootContext(ServletContext servletContext) {
        return servletContext == servletContext.getContext("/");
    }
            String originalPathboolean virtualthrows IOException {
        ServletContextAndPath csAndP = null;
        if ( > 0) {
            log("SSIServletExternalResolver.getServletContextAndPath( "
                    + originalPath + ", " + virtual + ")"null);
        }
        if (virtual) {
            csAndP = getServletContextAndPathFromVirtualPath(originalPath);
        } else {
            csAndP = getServletContextAndPathFromNonVirtualPath(originalPath);
        }
        return csAndP;
    }
    protected URLConnection getURLConnection(String originalPath,
            boolean virtualthrows IOException {
        ServletContextAndPath csAndP = getServletContextAndPath(originalPath,
                virtual);
        ServletContext context = csAndP.getServletContext();
        String path = csAndP.getPath();
        URL url = context.getResource(path);
        if (url == null) {
            throw new IOException("Context did not contain resource: " + path);
        }
        URLConnection urlConnection = url.openConnection();
        return urlConnection;
    }
    public long getFileLastModified(String pathboolean virtual)
            throws IOException {
        long lastModified = 0;
        try {
            URLConnection urlConnection = getURLConnection(pathvirtual);
            lastModified = urlConnection.getLastModified();
        } catch (IOException e) {
            // Ignore this. It will always fail for non-file based includes
        }
        return lastModified;
    }
    public long getFileSize(String pathboolean virtualthrows IOException {
        long fileSize = -1;
        try {
            URLConnection urlConnection = getURLConnection(pathvirtual);
            fileSize = urlConnection.getContentLength();
        } catch (IOException e) {
            // Ignore this. It will always fail for non-file based includes
        }
        return fileSize;
    }
    //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.
    public String getFileText(String originalPathboolean virtual)
            throws IOException {
        try {
            ServletContextAndPath csAndP = getServletContextAndPath(
                    originalPathvirtual);
            ServletContext context = csAndP.getServletContext();
            String path = csAndP.getPath();
            RequestDispatcher rd = context.getRequestDispatcher(path);
            if (rd == null) {
                throw new IOException(.ssiCannotGetRequestDispatcher(path));
            }
            ByteArrayServletOutputStream basos =
                new ByteArrayServletOutputStream();
            ResponseIncludeWrapper responseIncludeWrapper =
                new ResponseIncludeWrapper(contextbasos);
            rd.include(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 ( == null) {
                retVal = new Stringbytes );
            } else {
                retVal = new String (bytes);
            }
            //make an assumption that an empty response is a failure. This is
            // a problem
            // if a truly empty file
            //were included, but not sure how else to tell.
            if (retVal.equals("") && !.getMethod().equalsIgnoreCase(
                    .....)) {
                throw new IOException(.ssiCannotFindFile(path));
            }
            return retVal;
        } catch (ServletException e) {
            throw new IOException(.ssiServletIncludeFailed(originalPath), e);
        }
    }
    protected class ServletContextAndPath {
        protected ServletContext servletContext;
        protected String path;
        public ServletContextAndPath(ServletContext servletContext,
                                     String path) {
            this. = servletContext;
            this. = path;
        }
        public ServletContext getServletContext() {
            return ;
        }
        public String getPath() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X