FileDocCategorySizeDatePackage
SSIMediator.javaAPI DocGlassfish v2 API12676Fri May 04 22:32:20 BST 2007org.apache.catalina.ssi

SSIMediator

public class SSIMediator extends Object
Allows the different SSICommand implementations to share data/talk to each other
author
Bip Thelin
author
Amy Roh
author
Paul Speed
author
Dan Sandberg
author
David Becker
version
$Revision: 1.5 $, $Date: 2007/05/05 05:32:20 $

Fields Summary
protected static final String
DEFAULT_CONFIG_ERR_MSG
protected static final String
DEFAULT_CONFIG_TIME_FMT
protected static final String
DEFAULT_CONFIG_SIZE_FMT
protected static org.apache.catalina.util.URLEncoder
urlEncoder
protected String
configErrMsg
protected String
configTimeFmt
protected String
configSizeFmt
protected String
className
protected SSIExternalResolver
ssiExternalResolver
protected long
lastModifiedDate
protected int
debug
protected org.apache.catalina.util.Strftime
strftime
protected SSIConditionalState
conditionalState
Constructors Summary
public SSIMediator(SSIExternalResolver ssiExternalResolver, long lastModifiedDate, int debug)

     
        //We try to encode only the same characters that apache does
        urlEncoder = new URLEncoder();
        urlEncoder.addSafeCharacter(',");
        urlEncoder.addSafeCharacter(':");
        urlEncoder.addSafeCharacter('-");
        urlEncoder.addSafeCharacter('_");
        urlEncoder.addSafeCharacter('.");
        urlEncoder.addSafeCharacter('*");
        urlEncoder.addSafeCharacter('/");
        urlEncoder.addSafeCharacter('!");
        urlEncoder.addSafeCharacter('~");
        urlEncoder.addSafeCharacter('\'");
        urlEncoder.addSafeCharacter('(");
        urlEncoder.addSafeCharacter(')");
    
        this.ssiExternalResolver = ssiExternalResolver;
        this.lastModifiedDate = lastModifiedDate;
        this.debug = debug;
        setConfigTimeFmt(DEFAULT_CONFIG_TIME_FMT, true);
    
Methods Summary
protected java.lang.Stringencode(java.lang.String value, java.lang.String encoding)

        String retVal = null;
        if (encoding.equalsIgnoreCase("url")) {
            retVal = urlEncoder.encode(value);
        } else if (encoding.equalsIgnoreCase("none")) {
            retVal = value;
        } else if (encoding.equalsIgnoreCase("entity")) {
            //Not sure how this is really different than none
            retVal = value;
        } else {
            //This shouldn't be possible
            throw new IllegalArgumentException("Unknown encoding: " + encoding);
        }
        return retVal;
    
protected java.lang.StringformatDate(java.util.Date date, java.util.TimeZone timeZone)

        String retVal;
        if (timeZone != null) {
            //we temporarily change strftime. Since SSIMediator is inherently
            // single-threaded, this
            //isn't a problem
            TimeZone oldTimeZone = strftime.getTimeZone();
            strftime.setTimeZone(timeZone);
            retVal = strftime.format(date);
            strftime.setTimeZone(oldTimeZone);
        } else {
            retVal = strftime.format(date);
        }
        return retVal;
    
public SSIConditionalStategetConditionalState()

        return conditionalState;
    
public java.lang.StringgetConfigErrMsg()

        return configErrMsg;
    
public java.lang.StringgetConfigSizeFmt()

        return configSizeFmt;
    
public java.lang.StringgetConfigTimeFmt()

        return configTimeFmt;
    
public longgetFileLastModified(java.lang.String path, boolean virtual)

        return ssiExternalResolver.getFileLastModified(path, virtual);
    
public longgetFileSize(java.lang.String path, boolean virtual)

        return ssiExternalResolver.getFileSize(path, virtual);
    
public java.lang.StringgetFileText(java.lang.String path, boolean virtual)

        return ssiExternalResolver.getFileText(path, virtual);
    
public java.util.CollectiongetVariableNames()

        Set variableNames = new HashSet();
        //These built-in variables are supplied by the mediator ( if not
        // over-written by
        // the user ) and always exist
        variableNames.add("DATE_GMT");
        variableNames.add("DATE_LOCAL");
        variableNames.add("LAST_MODIFIED");
        ssiExternalResolver.addVariableNames(variableNames);
        //Remove any variables that are reserved by this class
        Iterator iter = variableNames.iterator();
        while (iter.hasNext()) {
            String name = (String)iter.next();
            if (isNameReserved(name)) {
                iter.remove();
            }
        }
        return variableNames;
    
public java.lang.StringgetVariableValue(java.lang.String variableName)

        return getVariableValue(variableName, "none");
    
public java.lang.StringgetVariableValue(java.lang.String variableName, java.lang.String encoding)

        String lowerCaseVariableName = variableName.toLowerCase();
        String variableValue = null;
        if (!isNameReserved(lowerCaseVariableName)) {
            //Try getting it externally first, if it fails, try getting the
            // 'built-in'
            // value
            variableValue = ssiExternalResolver.getVariableValue(variableName);
            if (variableValue == null) {
                variableName = variableName.toUpperCase();
                variableValue = (String)ssiExternalResolver
                        .getVariableValue(className + "." + variableName);
            }
            if (variableValue != null) {
                variableValue = encode(variableValue, encoding);
            }
        }
        return variableValue;
    
protected booleanisNameReserved(java.lang.String name)

        return name.startsWith(className + ".");
    
public voidlog(java.lang.String message)

        ssiExternalResolver.log(message, null);
    
public voidlog(java.lang.String message, java.lang.Throwable throwable)

        ssiExternalResolver.log(message, throwable);
    
public voidsetConfigErrMsg(java.lang.String configErrMsg)

        this.configErrMsg = configErrMsg;
    
public voidsetConfigSizeFmt(java.lang.String configSizeFmt)

        this.configSizeFmt = configSizeFmt;
    
public voidsetConfigTimeFmt(java.lang.String configTimeFmt)

        setConfigTimeFmt(configTimeFmt, false);
    
public voidsetConfigTimeFmt(java.lang.String configTimeFmt, boolean fromConstructor)

        this.configTimeFmt = configTimeFmt;
        //What's the story here with DateTool.LOCALE_US?? Why??
        this.strftime = new Strftime(configTimeFmt, DateTool.LOCALE_US);
        //Variables like DATE_LOCAL, DATE_GMT, and LAST_MODIFIED need to be
        // updated when
        //the timefmt changes. This is what Apache SSI does.
        setDateVariables(fromConstructor);
    
protected voidsetDateVariables(boolean fromConstructor)

        boolean alreadySet = ssiExternalResolver.getVariableValue(className
                + ".alreadyset") != null;
        //skip this if we are being called from the constructor, and this has
        // already
        // been set
        if (!(fromConstructor && alreadySet)) {
            ssiExternalResolver.setVariableValue(className + ".alreadyset",
                    "true");
            Date date = new Date();
            TimeZone timeZone = TimeZone.getTimeZone("GMT");
            String retVal = formatDate(date, timeZone);
            //If we are setting on of the date variables, we want to remove
            // them from the
            // user
            //defined list of variables, because this is what Apache does
            setVariableValue("DATE_GMT", null);
            ssiExternalResolver.setVariableValue(className + ".DATE_GMT",
                    retVal);
            retVal = formatDate(date, null);
            setVariableValue("DATE_LOCAL", null);
            ssiExternalResolver.setVariableValue(className + ".DATE_LOCAL",
                    retVal);
            retVal = formatDate(new Date(lastModifiedDate), null);
            setVariableValue("LAST_MODIFIED", null);
            ssiExternalResolver.setVariableValue(className + ".LAST_MODIFIED",
                    retVal);
        }
    
public voidsetVariableValue(java.lang.String variableName, java.lang.String variableValue)

        if (!isNameReserved(variableName)) {
            ssiExternalResolver.setVariableValue(variableName, variableValue);
        }
    
public java.lang.StringsubstituteVariables(java.lang.String val)
Applies variable substitution to the specified String and returns the new resolved string.

        // If it has no variable references then no work
        // need to be done
        if (val.indexOf('$") < 0) return val;
        StringBuffer sb = new StringBuffer(val);
        for (int i = 0; i < sb.length();) {
            // Find the next $
            for (; i < sb.length(); i++) {
                if (sb.charAt(i) == '$") {
                    i++;
                    break;
                }
            }
            if (i == sb.length()) break;
            // Check to see if the $ is escaped
            if (i > 1 && sb.charAt(i - 2) == '\\") {
                sb.deleteCharAt(i - 2);
                i--;
                continue;
            }
            int nameStart = i;
            int start = i - 1;
            int end = -1;
            int nameEnd = -1;
            char endChar = ' ";
            // Check for {} wrapped var
            if (sb.charAt(i) == '{") {
                nameStart++;
                endChar = '}";
            }
            // Find the end of the var reference
            for (; i < sb.length(); i++) {
                if (sb.charAt(i) == endChar) break;
            }
            end = i;
            nameEnd = end;
            if (endChar == '}") end++;
            // We should now have enough to extract the var name
            String varName = sb.substring(nameStart, nameEnd);
            String value = getVariableValue(varName);
            if (value == null) value = "";
            // Replace the var name with its value
            sb.replace(start, end, value);
            // Start searching for the next $ after the value
            // that was just substituted.
            i = start + value.length();
        }
        return sb.toString();