1 //========================================================================
2 //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3 //------------------------------------------------------------------------
4 //Licensed under the Apache License, Version 2.0 (the "License");
5 //you may not use this file except in compliance with the License.
6 //You may obtain a copy of the License at
7 //http://www.apache.org/licenses/LICENSE-2.0
8 //Unless required by applicable law or agreed to in writing, software
9 //distributed under the License is distributed on an "AS IS" BASIS,
10 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 //See the License for the specific language governing permissions and
12 //limitations under the License.
13 //========================================================================
14
15 package org.mortbay.jetty.handler;
16
17 import java.io.IOException;
18 import java.util.Map;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.mortbay.jetty.Request;
25 import org.mortbay.jetty.servlet.PathMap;
26 import org.mortbay.util.URIUtil;
27
28 /* ------------------------------------------------------------ */
29 /** Path Rewrite Handler
30 *<p>This path uses the pattern matching of {@link PathMap} to rewrite URI's
31 * of received requests. A typical jetty.xml configuration would be: <pre>
32 * <Set name="handler">
33 * <New id="Handlers" class="org.mortbay.jetty.handler.RewriteHandler">
34 * <Set name="rewriteRequestURI">false</Set>
35 * <Set name="rewritePathInfo">false</Set>
36 * <Set name="originalPathAttribute">requestedPath</Set>
37 * <Call name="addRewriteRule"><Arg>/other/*</Arg><Arg>/test</Arg></Call>
38 * <Call name="addRewriteRule"><Arg>/test/*</Arg><Arg></Arg></Call>
39 * <Call name="addRewriteRule"><Arg>/*</Arg><Arg>/test</Arg></Call>
40 * <Set name="handler">
41 * <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
42 * <Set name="handlers">
43 * <Array type="org.mortbay.jetty.Handler">
44 * <Item>
45 * <New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
46 * </Item>
47 * <Item>
48 * <New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
49 * </Item>
50 * <Item>
51 * <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
52 * </Item>
53 * </Array>
54 * </Set>
55 * </New>
56 * </Set>
57 * </New>
58 * </Set>
59 * </pre>
60 *
61 */
62 public class RewriteHandler extends HandlerWrapper
63 {
64 private boolean _rewriteRequestURI=true;
65 private boolean _rewritePathInfo=true;
66 private String _originalPathAttribute;
67 private PathMap _rewrite = new PathMap(true);
68
69 /* ------------------------------------------------------------ */
70 /**
71 * @return the rewriteRequestURI If true, this handler will rewrite the value
72 * returned by {@link HttpServletRequest#getRequestURI()}.
73 *
74 */
75 public boolean isRewriteRequestURI()
76 {
77 return _rewriteRequestURI;
78 }
79
80 /* ------------------------------------------------------------ */
81 /**
82 * @param rewriteRequestURI true if this handler will rewrite the value
83 * returned by {@link HttpServletRequest#getRequestURI()}.
84 */
85 public void setRewriteRequestURI(boolean rewriteRequestURI)
86 {
87 _rewriteRequestURI=rewriteRequestURI;
88 }
89
90 /* ------------------------------------------------------------ */
91 /**
92 * @return true if this handler will rewrite the value
93 * returned by {@link HttpServletRequest#getPathInfo()}.
94 */
95 public boolean isRewritePathInfo()
96 {
97 return _rewritePathInfo;
98 }
99
100 /* ------------------------------------------------------------ */
101 /**
102 * @param rewritePathInfo true if this handler will rewrite the value
103 * returned by {@link HttpServletRequest#getPathInfo()}.
104 */
105 public void setRewritePathInfo(boolean rewritePathInfo)
106 {
107 _rewritePathInfo=rewritePathInfo;
108 }
109
110 /* ------------------------------------------------------------ */
111 /**
112 * @return the originalPathAttribte. If non null, this string will be used
113 * as the attribute name to store the original request path.
114 */
115 public String getOriginalPathAttribute()
116 {
117 return _originalPathAttribute;
118 }
119
120 /* ------------------------------------------------------------ */
121 /**
122 * @param originalPathAttribte If non null, this string will be used
123 * as the attribute name to store the original request path.
124 */
125 public void setOriginalPathAttribute(String originalPathAttribte)
126 {
127 _originalPathAttribute=originalPathAttribte;
128 }
129
130 /* ------------------------------------------------------------ */
131 /**
132 * @return A {@link PathMap} of the rewriting rules.
133 */
134 public PathMap getRewrite()
135 {
136 return _rewrite;
137 }
138
139 /* ------------------------------------------------------------ */
140 /**
141 * @param rewrite A {@link PathMap} of the rewriting rules. Only
142 * prefix paths should be included.
143 */
144 public void setRewrite(PathMap rewrite)
145 {
146 _rewrite=rewrite;
147 }
148
149
150 /* ------------------------------------------------------------ */
151 /** Add a path rewriting rule
152 * @param pattern The path pattern to match. The pattern must start with / and may use
153 * a trailing /* as a wildcard.
154 * @param prefix The path prefix which will replace the matching part of the path.
155 */
156 public void addRewriteRule(String pattern, String prefix)
157 {
158 if (pattern==null || pattern.length()==0 || !pattern.startsWith("/"))
159 throw new IllegalArgumentException();
160 if (_rewrite==null)
161 _rewrite=new PathMap(true);
162 _rewrite.put(pattern,prefix);
163 }
164
165 /* ------------------------------------------------------------ */
166 /* (non-Javadoc)
167 * @see org.mortbay.jetty.handler.HandlerWrapper#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
168 */
169 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
170 {
171 if (isStarted() && _rewrite!=null)
172 {
173 Map.Entry rewrite =_rewrite.getMatch(target);
174
175 if (rewrite!=null && rewrite.getValue()!=null)
176 {
177 if (_originalPathAttribute!=null)
178 request.setAttribute(_originalPathAttribute,target);
179
180 target=URIUtil.addPaths(rewrite.getValue().toString(),
181 PathMap.pathInfo(rewrite.getKey().toString(),target));
182
183 if (_rewriteRequestURI)
184 ((Request)request).setRequestURI(target);
185
186 if (_rewritePathInfo)
187 ((Request)request).setPathInfo(target);
188 }
189 }
190 super.handle(target,request,response,dispatch);
191
192 }
193 }