KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > RewriteRealPath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42 import java.util.regex.Matcher JavaDoc;
43 import java.util.regex.Pattern JavaDoc;
44
45 /**
46  * Configuration for a rewrite-real-path
47  */

48 public class RewriteRealPath {
49   static final L10N L = new L10N(RewriteRealPath.class);
50   static final Logger JavaDoc log = Logger.getLogger(RewriteRealPath.class.getName());
51
52   private Path _appDir;
53
54   private final ArrayList JavaDoc<Program> _programList = new ArrayList JavaDoc<Program>();
55   
56   // path mapping (old path-mapping)
57
private UrlMap<String JavaDoc> _pathMapping;
58
59   public RewriteRealPath(Path appDir)
60   {
61     _appDir = appDir;
62   }
63
64   /**
65    * Adds a rewrite
66    */

67   public void addRewrite(Rewrite rewrite)
68   {
69     _programList.add(rewrite);
70   }
71
72   /**
73    * Adds a real-path.
74    */

75   public void addRealPath(RealPath realPath)
76   {
77     _programList.add(realPath);
78   }
79   
80   /**
81    * Adds a path pattern.
82    */

83   public void addPathPattern(String JavaDoc urlPattern, String JavaDoc realPath)
84   {
85     if (_pathMapping == null)
86       _pathMapping = new UrlMap<String JavaDoc>();
87     
88     _pathMapping.addMap(urlPattern, realPath);
89   }
90
91   /**
92    * Adds a path pattern.
93    */

94   public void addPathRegexp(String JavaDoc urlRegexp, String JavaDoc realPath)
95   {
96     if (_pathMapping == null)
97       _pathMapping = new UrlMap<String JavaDoc>();
98     
99     _pathMapping.addRegexp(urlRegexp, realPath);
100   }
101
102   /**
103    * Maps the path.
104    */

105   public String JavaDoc mapToRealPath(String JavaDoc 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 JavaDoc realPath = program.toRealPath(uri);
113
114       if (realPath != null)
115     return _appDir.lookup(realPath).getNativePath();
116     }
117
118     return pathMappingToRealPath(uri);
119   }
120
121   /**
122    * Compatibility mapping to real path.
123    */

124   private String JavaDoc pathMappingToRealPath(String JavaDoc uri)
125   {
126     if (_pathMapping == null)
127       return _appDir.lookup("./" + uri).getNativePath();
128
129     ArrayList JavaDoc<String JavaDoc> regexpVars = new ArrayList JavaDoc<String JavaDoc>();
130     
131     String JavaDoc 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 JavaDoc e) {
140         log.log(Level.WARNING, e.toString(), e);
141         
142         path = _appDir.lookup(map);
143       }
144     
145       String JavaDoc match = (String JavaDoc) regexpVars.get(0);
146       String JavaDoc tail = uri.substring(match.length());
147
148       // hacks to get the trailing '/' correct
149
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 JavaDoc nativePath = path.getNativePath();
160
161     // server/108j expects case insensitive
162
/*
163     if (CaseInsensitive.isCaseInsensitive())
164       return nativePath.toLowerCase();
165     else
166       return nativePath;
167     */

168     
169     return nativePath;
170   }
171
172   static class Program {
173     public String JavaDoc rewrite(String JavaDoc uri)
174     {
175       return uri;
176     }
177     
178     public String JavaDoc toRealPath(String JavaDoc uri)
179     {
180       return null;
181     }
182   }
183
184   public static class Rewrite extends Program {
185     private Pattern JavaDoc _regexp;
186     private String JavaDoc _replacement;
187
188     /**
189      * Sets the regular expression.
190      */

191     public void setRegexp(String JavaDoc regexp)
192     {
193       _regexp = Pattern.compile(regexp);
194     }
195
196     /**
197      * Sets the target.
198      */

199     public void setReplacement(String JavaDoc replacement)
200     {
201       _replacement = replacement;
202     }
203
204     /**
205      * Init
206      */

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 JavaDoc rewrite(String JavaDoc uri)
218     {
219       Matcher JavaDoc 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 JavaDoc _regexp;
232     private String JavaDoc _target;
233
234     /**
235      * Sets the regular expression.
236      */

237     public void setRegexp(String JavaDoc regexp)
238     {
239       _regexp = Pattern.compile(regexp);
240     }
241
242     /**
243      * Sets the target.
244      */

245     public void setTarget(String JavaDoc target)
246     {
247       _target = target;
248     }
249
250     /**
251      * Init
252      */

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 JavaDoc toRealPath(String JavaDoc uri)
264     {
265       Matcher JavaDoc 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