1 52 53 package freemarker.cache; 54 55 import java.io.File ; 56 import java.io.FileInputStream ; 57 import java.io.FileNotFoundException ; 58 import java.io.IOException ; 59 import java.io.InputStreamReader ; 60 import java.io.Reader ; 61 import java.security.AccessController ; 62 import java.security.PrivilegedAction ; 63 import java.security.PrivilegedActionException ; 64 import java.security.PrivilegedExceptionAction ; 65 66 import freemarker.template.utility.SecurityUtilities; 67 68 77 public class FileTemplateLoader implements TemplateLoader 78 { 79 private static final boolean SEP_IS_SLASH = File.separatorChar == '/'; 80 public final File baseDir; 81 private final String canonicalPath; 82 83 88 public FileTemplateLoader() 89 throws 90 IOException 91 { 92 this(new File (SecurityUtilities.getSystemProperty("user.dir"))); 93 } 94 95 100 public FileTemplateLoader(final File baseDir) 101 throws 102 IOException 103 { 104 try 105 { 106 Object [] retval = (Object []) AccessController.doPrivileged(new PrivilegedExceptionAction () 107 { 108 public Object run() 109 throws 110 IOException 111 { 112 if (!baseDir.exists()) 113 { 114 throw new FileNotFoundException (baseDir + " does not exist."); 115 } 116 if (!baseDir.isDirectory()) 117 { 118 throw new IOException (baseDir + " is not a directory."); 119 } 120 Object [] retval = new Object [2]; 121 retval[0] = baseDir.getCanonicalFile(); 122 retval[1] = ((File ) retval[0]).getPath() + File.separatorChar; 123 return retval; 124 } 125 }); 126 this.baseDir = (File ) retval[0]; 127 this.canonicalPath = (String ) retval[1]; 128 } 129 catch(PrivilegedActionException e) 130 { 131 throw (IOException )e.getException(); 132 } 133 } 134 135 public Object findTemplateSource(final String name) 136 throws 137 IOException 138 { 139 try 140 { 141 return AccessController.doPrivileged(new PrivilegedExceptionAction () 142 { 143 public Object run() 144 throws 145 IOException 146 { 147 File source = new File (baseDir, SEP_IS_SLASH ? name : name.replace('/', File.separatorChar)); 148 if(!source.isFile()) { 149 return null; 150 } 151 String normalized = source.getCanonicalPath(); 154 if (normalized.startsWith(canonicalPath)) { 155 return source; 156 } 157 throw new SecurityException (normalized + " doesn't start with " + canonicalPath); 158 } 159 }); 160 } 161 catch(PrivilegedActionException e) 162 { 163 throw (IOException )e.getException(); 164 } 165 } 166 167 public long getLastModified(final Object templateSource) 168 { 169 return ((Long )(AccessController.doPrivileged(new PrivilegedAction () 170 { 171 public Object run() 172 { 173 return new Long (((File )templateSource).lastModified()); 174 } 175 }))).longValue(); 176 177 178 } 179 180 public Reader getReader(final Object templateSource, final String encoding) 181 throws 182 IOException 183 { 184 try 185 { 186 return (Reader )AccessController.doPrivileged(new PrivilegedExceptionAction () 187 { 188 public Object run() 189 throws 190 IOException 191 { 192 if (!(templateSource instanceof File )) { 193 throw new IllegalArgumentException ( 194 "templateSource is a: " + templateSource.getClass().getName()); 195 } 196 return new InputStreamReader (new FileInputStream ((File ) templateSource), encoding); 197 } 198 }); 199 } 200 catch(PrivilegedActionException e) 201 { 202 throw (IOException )e.getException(); 203 } 204 } 205 206 public void closeTemplateSource(Object templateSource) 207 { 208 } 210 } | Popular Tags |