1 // ========================================================================
2 // $Id$
3 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 // The Eclipse Public License is available at
9 // http://www.eclipse.org/legal/epl-v10.html
10 // The Apache License v2.0 is available at
11 // http://www.opensource.org/licenses/apache2.0.php
12 // You may elect to redistribute this code under either of these licenses.
13 // ========================================================================
14 package org.eclipse.jetty.rewrite.handler;
15
16 import java.io.IOException;
17 import java.util.Map;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.http.PathMap;
23 import org.eclipse.jetty.util.URIUtil;
24
25 /**
26 * Rule implementing the legacy API of RewriteHandler
27 *
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 }