1 11 package org.apache.catalina.ssi; 12 13 14 import java.io.IOException ; 15 import java.util.Collection ; 16 import java.util.Date ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 import java.util.TimeZone ; 21 import org.apache.catalina.util.DateTool; 22 import org.apache.catalina.util.Strftime; 23 import org.apache.catalina.util.URLEncoder; 24 35 public class SSIMediator { 36 protected final static String DEFAULT_CONFIG_ERR_MSG = "[an error occurred while processing this directive]"; 37 protected final static String DEFAULT_CONFIG_TIME_FMT = "%A, %d-%b-%Y %T %Z"; 38 protected final static String DEFAULT_CONFIG_SIZE_FMT = "abbrev"; 39 protected static URLEncoder urlEncoder; 40 protected String configErrMsg = DEFAULT_CONFIG_ERR_MSG; 41 protected String configTimeFmt = DEFAULT_CONFIG_TIME_FMT; 42 protected String configSizeFmt = DEFAULT_CONFIG_SIZE_FMT; 43 protected String className = getClass().getName(); 44 protected SSIExternalResolver ssiExternalResolver; 45 protected long lastModifiedDate; 46 protected int debug; 47 protected Strftime strftime; 48 protected SSIConditionalState conditionalState = new SSIConditionalState(); 49 static { 50 urlEncoder = new URLEncoder(); 52 urlEncoder.addSafeCharacter(','); 53 urlEncoder.addSafeCharacter(':'); 54 urlEncoder.addSafeCharacter('-'); 55 urlEncoder.addSafeCharacter('_'); 56 urlEncoder.addSafeCharacter('.'); 57 urlEncoder.addSafeCharacter('*'); 58 urlEncoder.addSafeCharacter('/'); 59 urlEncoder.addSafeCharacter('!'); 60 urlEncoder.addSafeCharacter('~'); 61 urlEncoder.addSafeCharacter('\''); 62 urlEncoder.addSafeCharacter('('); 63 urlEncoder.addSafeCharacter(')'); 64 } 65 66 67 public SSIMediator(SSIExternalResolver ssiExternalResolver, 68 long lastModifiedDate, int debug) { 69 this.ssiExternalResolver = ssiExternalResolver; 70 this.lastModifiedDate = lastModifiedDate; 71 this.debug = debug; 72 setConfigTimeFmt(DEFAULT_CONFIG_TIME_FMT, true); 73 } 74 75 76 public void setConfigErrMsg(String configErrMsg) { 77 this.configErrMsg = configErrMsg; 78 } 79 80 81 public void setConfigTimeFmt(String configTimeFmt) { 82 setConfigTimeFmt(configTimeFmt, false); 83 } 84 85 86 public void setConfigTimeFmt(String configTimeFmt, boolean fromConstructor) { 87 this.configTimeFmt = configTimeFmt; 88 this.strftime = new Strftime(configTimeFmt, DateTool.LOCALE_US); 90 setDateVariables(fromConstructor); 94 } 95 96 97 public void setConfigSizeFmt(String configSizeFmt) { 98 this.configSizeFmt = configSizeFmt; 99 } 100 101 102 public String getConfigErrMsg() { 103 return configErrMsg; 104 } 105 106 107 public String getConfigTimeFmt() { 108 return configTimeFmt; 109 } 110 111 112 public String getConfigSizeFmt() { 113 return configSizeFmt; 114 } 115 116 117 public SSIConditionalState getConditionalState() { 118 return conditionalState; 119 } 120 121 122 public Collection getVariableNames() { 123 Set variableNames = new HashSet (); 124 variableNames.add("DATE_GMT"); 128 variableNames.add("DATE_LOCAL"); 129 variableNames.add("LAST_MODIFIED"); 130 ssiExternalResolver.addVariableNames(variableNames); 131 Iterator iter = variableNames.iterator(); 133 while (iter.hasNext()) { 134 String name = (String )iter.next(); 135 if (isNameReserved(name)) { 136 iter.remove(); 137 } 138 } 139 return variableNames; 140 } 141 142 143 public long getFileSize(String path, boolean virtual) throws IOException { 144 return ssiExternalResolver.getFileSize(path, virtual); 145 } 146 147 148 public long getFileLastModified(String path, boolean virtual) 149 throws IOException { 150 return ssiExternalResolver.getFileLastModified(path, virtual); 151 } 152 153 154 public String getFileText(String path, boolean virtual) throws IOException { 155 return ssiExternalResolver.getFileText(path, virtual); 156 } 157 158 159 protected boolean isNameReserved(String name) { 160 return name.startsWith(className + "."); 161 } 162 163 164 public String getVariableValue(String variableName) { 165 return getVariableValue(variableName, "none"); 166 } 167 168 169 public void setVariableValue(String variableName, String variableValue) { 170 if (!isNameReserved(variableName)) { 171 ssiExternalResolver.setVariableValue(variableName, variableValue); 172 } 173 } 174 175 176 public String getVariableValue(String variableName, String encoding) { 177 String lowerCaseVariableName = variableName.toLowerCase(); 178 String variableValue = null; 179 if (!isNameReserved(lowerCaseVariableName)) { 180 variableValue = ssiExternalResolver.getVariableValue(variableName); 184 if (variableValue == null) { 185 variableName = variableName.toUpperCase(); 186 variableValue = (String )ssiExternalResolver 187 .getVariableValue(className + "." + variableName); 188 } 189 if (variableValue != null) { 190 variableValue = encode(variableValue, encoding); 191 } 192 } 193 return variableValue; 194 } 195 196 197 201 public String substituteVariables(String val) { 202 if (val.indexOf('$') < 0) return val; 205 StringBuffer sb = new StringBuffer (val); 206 for (int i = 0; i < sb.length();) { 207 for (; i < sb.length(); i++) { 209 if (sb.charAt(i) == '$') { 210 i++; 211 break; 212 } 213 } 214 if (i == sb.length()) break; 215 if (i > 1 && sb.charAt(i - 2) == '\\') { 217 sb.deleteCharAt(i - 2); 218 i--; 219 continue; 220 } 221 int nameStart = i; 222 int start = i - 1; 223 int end = -1; 224 int nameEnd = -1; 225 char endChar = ' '; 226 if (sb.charAt(i) == '{') { 228 nameStart++; 229 endChar = '}'; 230 } 231 for (; i < sb.length(); i++) { 233 if (sb.charAt(i) == endChar) break; 234 } 235 end = i; 236 nameEnd = end; 237 if (endChar == '}') end++; 238 String varName = sb.substring(nameStart, nameEnd); 240 String value = getVariableValue(varName); 241 if (value == null) value = ""; 242 sb.replace(start, end, value); 244 i = start + value.length(); 247 } 248 return sb.toString(); 249 } 250 251 252 protected String formatDate(Date date, TimeZone timeZone) { 253 String retVal; 254 if (timeZone != null) { 255 TimeZone oldTimeZone = strftime.getTimeZone(); 259 strftime.setTimeZone(timeZone); 260 retVal = strftime.format(date); 261 strftime.setTimeZone(oldTimeZone); 262 } else { 263 retVal = strftime.format(date); 264 } 265 return retVal; 266 } 267 268 269 protected String encode(String value, String encoding) { 270 String retVal = null; 271 if (encoding.equalsIgnoreCase("url")) { 272 retVal = urlEncoder.encode(value); 273 } else if (encoding.equalsIgnoreCase("none")) { 274 retVal = value; 275 } else if (encoding.equalsIgnoreCase("entity")) { 276 retVal = value; 278 } else { 279 throw new IllegalArgumentException ("Unknown encoding: " + encoding); 281 } 282 return retVal; 283 } 284 285 286 public void log(String message) { 287 ssiExternalResolver.log(message, null); 288 } 289 290 291 public void log(String message, Throwable throwable) { 292 ssiExternalResolver.log(message, throwable); 293 } 294 295 296 protected void setDateVariables(boolean fromConstructor) { 297 boolean alreadySet = ssiExternalResolver.getVariableValue(className 298 + ".alreadyset") != null; 299 if (!(fromConstructor && alreadySet)) { 303 ssiExternalResolver.setVariableValue(className + ".alreadyset", 304 "true"); 305 Date date = new Date (); 306 TimeZone timeZone = TimeZone.getTimeZone("GMT"); 307 String retVal = formatDate(date, timeZone); 308 setVariableValue("DATE_GMT", null); 313 ssiExternalResolver.setVariableValue(className + ".DATE_GMT", 314 retVal); 315 retVal = formatDate(date, null); 316 setVariableValue("DATE_LOCAL", null); 317 ssiExternalResolver.setVariableValue(className + ".DATE_LOCAL", 318 retVal); 319 retVal = formatDate(new Date (lastModifiedDate), null); 320 setVariableValue("LAST_MODIFIED", null); 321 ssiExternalResolver.setVariableValue(className + ".LAST_MODIFIED", 322 retVal); 323 } 324 } 325 } | Popular Tags |