1 //========================================================================
2 //$Id$
3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15 package org.mortbay.jetty.handler.rewrite;
16
17 import org.mortbay.jetty.servlet.PathMap;
18 import org.mortbay.util.URIUtil;
19 import java.util.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import java.io.IOException;
24
25 /**
26 * Rule implementing the legacy API of RewriteHandler
27 * @author gregw
28 *
29 */
30 public class LegacyRule extends Rule
31 {
32 private PathMap _rewrite = new PathMap(true);
33
34 public LegacyRule()
35 {
36 _handling = false;
37 _terminating = false;
38 }
39
40 /* ------------------------------------------------------------ */
41 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
42 {
43 Map.Entry<?,?> rewrite =_rewrite.getMatch(target);
44
45 if (rewrite!=null && rewrite.getValue()!=null)
46 {
47 target=URIUtil.addPaths(rewrite.getValue().toString(),
48 PathMap.pathInfo(rewrite.getKey().toString(),target));
49
50 return target;
51 }
52
53 return null;
54 }
55
56 /* ------------------------------------------------------------ */
57 /**
58 * Returns the map of rewriting rules.
59 * @return A {@link PathMap} of the rewriting rules.
60 */
61 public PathMap getRewrite()
62 {
63 return _rewrite;
64 }
65
66 /* ------------------------------------------------------------ */
67 /**
68 * Sets the map of rewriting rules.
69 * @param rewrite A {@link PathMap} of the rewriting rules. Only
70 * prefix paths should be included.
71 */
72 public void setRewrite(PathMap rewrite)
73 {
74 _rewrite=rewrite;
75 }
76
77
78 /* ------------------------------------------------------------ */
79 /** Add a path rewriting rule
80 * @param pattern The path pattern to match. The pattern must start with / and may use
81 * a trailing /* as a wildcard.
82 * @param prefix The path prefix which will replace the matching part of the path.
83 */
84 public void addRewriteRule(String pattern, String prefix)
85 {
86 if (pattern==null || pattern.length()==0 || !pattern.startsWith("/"))
87 throw new IllegalArgumentException();
88 if (_rewrite==null)
89 _rewrite=new PathMap(true);
90 _rewrite.put(pattern,prefix);
91 }
92
93
94 }