1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.rewrite.handler;
15
16 import java.io.IOException;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.eclipse.jetty.http.PathMap;
22 import org.eclipse.jetty.server.HttpConnection;
23 import org.eclipse.jetty.server.Request;
24 import org.eclipse.jetty.util.LazyList;
25 import org.eclipse.jetty.util.log.Log;
26
27 /**
28 * Base container to group rules. Can be extended so that the contained rules
29 * will only be applied under certain conditions
30 *
31 *
32 */
33
34 public class RuleContainer extends Rule
35 {
36 protected Rule[] _rules;
37 protected boolean _handled;
38
39 protected String _originalPathAttribute;
40 protected boolean _rewriteRequestURI=true;
41 protected boolean _rewritePathInfo=true;
42
43 protected LegacyRule _legacy;
44
45 /* ------------------------------------------------------------ */
46 private LegacyRule getLegacyRule()
47 {
48 if (_legacy==null)
49 {
50 _legacy= new LegacyRule();
51 addRule(_legacy);
52 }
53 return _legacy;
54 }
55
56
57 /* ------------------------------------------------------------ */
58 /**
59 * To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
60 * originalPathAttribute
61 *
62 * @param legacyRule old style rewrite rule
63 */
64 public void setLegacyRule(LegacyRule legacyRule)
65 {
66 _legacy = legacyRule;
67 }
68
69 /* ------------------------------------------------------------ */
70 /**
71 * Returns the list of rules.
72 * @return an array of {@link Rule}.
73 */
74 public Rule[] getRules()
75 {
76 return _rules;
77 }
78
79 /* ------------------------------------------------------------ */
80 /**
81 * Assigns the rules to process.
82 * @param rules an array of {@link Rule}.
83 */
84 public void setRules(Rule[] rules)
85 {
86 if (_legacy==null)
87 _rules = rules;
88 else
89 {
90 _rules=null;
91 addRule(_legacy);
92 if (rules!=null)
93 for (Rule rule:rules)
94 addRule(rule);
95 }
96 }
97
98 /* ------------------------------------------------------------ */
99 /**
100 * Add a Rule
101 * @param rule The rule to add to the end of the rules array
102 */
103 public void addRule(Rule rule)
104 {
105 _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
106 }
107
108
109 /* ------------------------------------------------------------ */
110 /**
111 * @return the rewriteRequestURI If true, this handler will rewrite the value
112 * returned by {@link HttpServletRequest#getRequestURI()}.
113 */
114 public boolean isRewriteRequestURI()
115 {
116 return _rewriteRequestURI;
117 }
118
119 /* ------------------------------------------------------------ */
120 /**
121 * @param rewriteRequestURI true if this handler will rewrite the value
122 * returned by {@link HttpServletRequest#getRequestURI()}.
123 */
124 public void setRewriteRequestURI(boolean rewriteRequestURI)
125 {
126 _rewriteRequestURI=rewriteRequestURI;
127 }
128
129 /* ------------------------------------------------------------ */
130 /**
131 * @return true if this handler will rewrite the value
132 * returned by {@link HttpServletRequest#getPathInfo()}.
133 */
134 public boolean isRewritePathInfo()
135 {
136 return _rewritePathInfo;
137 }
138
139 /* ------------------------------------------------------------ */
140 /**
141 * @param rewritePathInfo true if this handler will rewrite the value
142 * returned by {@link HttpServletRequest#getPathInfo()}.
143 */
144 public void setRewritePathInfo(boolean rewritePathInfo)
145 {
146 _rewritePathInfo=rewritePathInfo;
147 }
148
149 /* ------------------------------------------------------------ */
150 /**
151 * @return the originalPathAttribte. If non null, this string will be used
152 * as the attribute name to store the original request path.
153 */
154 public String getOriginalPathAttribute()
155 {
156 return _originalPathAttribute;
157 }
158
159 /* ------------------------------------------------------------ */
160 /**
161 * @param originalPathAttribte If non null, this string will be used
162 * as the attribute name to store the original request path.
163 */
164 public void setOriginalPathAttribute(String originalPathAttribte)
165 {
166 _originalPathAttribute=originalPathAttribte;
167 }
168
169
170 /* ------------------------------------------------------------ */
171 /**
172 * @deprecated
173 */
174 public PathMap getRewrite()
175 {
176 return getLegacyRule().getRewrite();
177 }
178
179 /* ------------------------------------------------------------ */
180 /**
181 * @deprecated
182 */
183 public void setRewrite(PathMap rewrite)
184 {
185 getLegacyRule().setRewrite(rewrite);
186 }
187
188 /* ------------------------------------------------------------ */
189 /**
190 * @deprecated
191 */
192 public void addRewriteRule(String pattern, String prefix)
193 {
194 getLegacyRule().addRewriteRule(pattern,prefix);
195 }
196
197
198 /**
199 * @return handled true if one of the rules within the rule container is handling the request
200 */
201 public boolean isHandled()
202 {
203 return _handled;
204 }
205
206 /*------------------------------------------------------------ */
207 /**
208 * @param handled true if one of the rules within the rule container is handling the request
209 */
210 public void setHandled(boolean handled)
211 {
212 _handled=handled;
213 }
214
215
216 /**
217 * Process the contained rules
218 * @param target target field to pass on to the contained rules
219 * @param request request object to pass on to the contained rules
220 * @param response response object to pass on to the contained rules
221 */
222 @Override
223 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
224 {
225 return apply(target, request, response);
226 }
227
228 /**
229 * Process the contained rules (called by matchAndApply)
230 * @param target target field to pass on to the contained rules
231 * @param request request object to pass on to the contained rules
232 * @param response response object to pass on to the contained rules
233 */
234 protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
235 {
236 _handled=false;
237
238 boolean original_set=_originalPathAttribute==null;
239
240 for (Rule rule : _rules)
241 {
242 String applied=rule.matchAndApply(target,request, response);
243 if (applied!=null)
244 {
245 Log.debug("applied {}",rule);
246 if (!target.equals(applied))
247 {
248 Log.debug("rewrote {} to {}",target,applied);
249 if (!original_set)
250 {
251 original_set=true;
252 request.setAttribute(_originalPathAttribute, target);
253 }
254
255 if (_rewriteRequestURI)
256 ((Request)request).setRequestURI(applied);
257
258 if (_rewritePathInfo)
259 ((Request)request).setPathInfo(applied);
260
261 target=applied;
262 }
263
264 if (rule.isHandling())
265 {
266 Log.debug("handling {}",rule);
267 _handled=true;
268 (request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest()).setHandled(true);
269 }
270
271 if (rule.isTerminating())
272 {
273 Log.debug("terminating {}",rule);
274 break;
275 }
276 }
277 }
278
279 return target;
280 }
281 }