1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.PathBuilder; 34 import com.caucho.server.dispatch.UrlMap; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Path; 37 38 import javax.annotation.PostConstruct; 39 import java.util.ArrayList ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 48 public class RewriteRealPath { 49 static final L10N L = new L10N(RewriteRealPath.class); 50 static final Logger log = Logger.getLogger(RewriteRealPath.class.getName()); 51 52 private Path _appDir; 53 54 private final ArrayList <Program> _programList = new ArrayList <Program>(); 55 56 private UrlMap<String > _pathMapping; 58 59 public RewriteRealPath(Path appDir) 60 { 61 _appDir = appDir; 62 } 63 64 67 public void addRewrite(Rewrite rewrite) 68 { 69 _programList.add(rewrite); 70 } 71 72 75 public void addRealPath(RealPath realPath) 76 { 77 _programList.add(realPath); 78 } 79 80 83 public void addPathPattern(String urlPattern, String realPath) 84 { 85 if (_pathMapping == null) 86 _pathMapping = new UrlMap<String >(); 87 88 _pathMapping.addMap(urlPattern, realPath); 89 } 90 91 94 public void addPathRegexp(String urlRegexp, String realPath) 95 { 96 if (_pathMapping == null) 97 _pathMapping = new UrlMap<String >(); 98 99 _pathMapping.addRegexp(urlRegexp, realPath); 100 } 101 102 105 public String mapToRealPath(String uri) 106 { 107 for (int i = 0; i < _programList.size(); i++) { 108 Program program = _programList.get(i); 109 110 uri = program.rewrite(uri); 111 112 String realPath = program.toRealPath(uri); 113 114 if (realPath != null) 115 return _appDir.lookup(realPath).getNativePath(); 116 } 117 118 return pathMappingToRealPath(uri); 119 } 120 121 124 private String pathMappingToRealPath(String uri) 125 { 126 if (_pathMapping == null) 127 return _appDir.lookup("./" + uri).getNativePath(); 128 129 ArrayList <String > regexpVars = new ArrayList <String >(); 130 131 String map = _pathMapping.map(uri, regexpVars); 132 133 Path path; 134 if (map == null) 135 path = _appDir.lookup("./" + uri); 136 else { 137 try { 138 path = PathBuilder.lookupPath(map, regexpVars); 139 } catch (Exception e) { 140 log.log(Level.WARNING, e.toString(), e); 141 142 path = _appDir.lookup(map); 143 } 144 145 String match = (String ) regexpVars.get(0); 146 String tail = uri.substring(match.length()); 147 148 if (uri.endsWith("/") && ! tail.endsWith("/")) 150 tail = tail + '/'; 151 152 if (tail.startsWith("/")) 153 tail = '.' + tail; 154 155 if (! tail.equals("")) 156 path = path.lookup(tail); 157 } 158 159 String nativePath = path.getNativePath(); 160 161 168 169 return nativePath; 170 } 171 172 static class Program { 173 public String rewrite(String uri) 174 { 175 return uri; 176 } 177 178 public String toRealPath(String uri) 179 { 180 return null; 181 } 182 } 183 184 public static class Rewrite extends Program { 185 private Pattern _regexp; 186 private String _replacement; 187 188 191 public void setRegexp(String regexp) 192 { 193 _regexp = Pattern.compile(regexp); 194 } 195 196 199 public void setReplacement(String replacement) 200 { 201 _replacement = replacement; 202 } 203 204 207 @PostConstruct 208 public void init() 209 throws ConfigException 210 { 211 if (_regexp == null) 212 throw new ConfigException(L.l("rewrite needs 'regexp' attribute.")); 213 if (_replacement == null) 214 throw new ConfigException(L.l("rewrite needs 'replacement' attribute.")); 215 } 216 217 public String rewrite(String uri) 218 { 219 Matcher matcher = _regexp.matcher(uri); 220 221 if (matcher.find()) { 222 matcher.reset(); 223 return matcher.replaceAll(_replacement); 224 } 225 else 226 return uri; 227 } 228 } 229 230 public static class RealPath extends Program { 231 private Pattern _regexp; 232 private String _target; 233 234 237 public void setRegexp(String regexp) 238 { 239 _regexp = Pattern.compile(regexp); 240 } 241 242 245 public void setTarget(String target) 246 { 247 _target = target; 248 } 249 250 253 @PostConstruct 254 public void init() 255 throws ConfigException 256 { 257 if (_regexp == null) 258 throw new ConfigException(L.l("real-path needs 'regexp' attribute.")); 259 if (_target == null) 260 throw new ConfigException(L.l("real-path needs 'target' attribute.")); 261 } 262 263 public String toRealPath(String uri) 264 { 265 Matcher matcher = _regexp.matcher(uri); 266 267 if (matcher.find()) { 268 matcher.reset(); 269 270 return matcher.replaceAll(_target); 271 } 272 else 273 return null; 274 } 275 } 276 } 277 | Popular Tags |