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 import org.eclipse.jetty.util.log.Logger;
27
28 /**
29 * Base container to group rules. Can be extended so that the contained rules
30 * will only be applied under certain conditions
31 *
32 *
33 */
34
35 public class RuleContainer extends Rule
36 {
37 private static final Logger LOG = Log.getLogger(RuleContainer.class);
38
39 protected Rule[] _rules;
40
41 protected String _originalPathAttribute;
42 protected boolean _rewriteRequestURI=true;
43 protected boolean _rewritePathInfo=true;
44
45 protected LegacyRule _legacy;
46
47 /* ------------------------------------------------------------ */
48 @Deprecated
49 public LegacyRule getLegacyRule()
50 {
51 if (_legacy==null)
52 {
53 _legacy= new LegacyRule();
54 addRule(_legacy);
55 }
56 return _legacy;
57 }
58
59
60 /* ------------------------------------------------------------ */
61 /**
62 * To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
63 * originalPathAttribute
64 *
65 * @param legacyRule old style rewrite rule
66 */
67 @Deprecated
68 public void setLegacyRule(LegacyRule legacyRule)
69 {
70 _legacy = legacyRule;
71 }
72
73 /* ------------------------------------------------------------ */
74 /**
75 * Returns the list of rules.
76 * @return an array of {@link Rule}.
77 */
78 public Rule[] getRules()
79 {
80 return _rules;
81 }
82
83 /* ------------------------------------------------------------ */
84 /**
85 * Assigns the rules to process.
86 * @param rules an array of {@link Rule}.
87 */
88 public void setRules(Rule[] rules)
89 {
90 if (_legacy==null)
91 _rules = rules;
92 else
93 {
94 _rules=null;
95 addRule(_legacy);
96 if (rules!=null)
97 for (Rule rule:rules)
98 addRule(rule);
99 }
100 }
101
102 /* ------------------------------------------------------------ */
103 /**
104 * Add a Rule
105 * @param rule The rule to add to the end of the rules array
106 */
107 public void addRule(Rule rule)
108 {
109 _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
110 }
111
112
113 /* ------------------------------------------------------------ */
114 /**
115 * @return the rewriteRequestURI If true, this handler will rewrite the value
116 * returned by {@link HttpServletRequest#getRequestURI()}.
117 */
118 public boolean isRewriteRequestURI()
119 {
120 return _rewriteRequestURI;
121 }
122
123 /* ------------------------------------------------------------ */
124 /**
125 * @param rewriteRequestURI true if this handler will rewrite the value
126 * returned by {@link HttpServletRequest#getRequestURI()}.
127 */
128 public void setRewriteRequestURI(boolean rewriteRequestURI)
129 {
130 _rewriteRequestURI=rewriteRequestURI;
131 }
132
133 /* ------------------------------------------------------------ */
134 /**
135 * @return true if this handler will rewrite the value
136 * returned by {@link HttpServletRequest#getPathInfo()}.
137 */
138 public boolean isRewritePathInfo()
139 {
140 return _rewritePathInfo;
141 }
142
143 /* ------------------------------------------------------------ */
144 /**
145 * @param rewritePathInfo true if this handler will rewrite the value
146 * returned by {@link HttpServletRequest#getPathInfo()}.
147 */
148 public void setRewritePathInfo(boolean rewritePathInfo)
149 {
150 _rewritePathInfo=rewritePathInfo;
151 }
152
153 /* ------------------------------------------------------------ */
154 /**
155 * @return the originalPathAttribte. If non null, this string will be used
156 * as the attribute name to store the original request path.
157 */
158 public String getOriginalPathAttribute()
159 {
160 return _originalPathAttribute;
161 }
162
163 /* ------------------------------------------------------------ */
164 /**
165 * @param originalPathAttribte If non null, this string will be used
166 * as the attribute name to store the original request path.
167 */
168 public void setOriginalPathAttribute(String originalPathAttribte)
169 {
170 _originalPathAttribute=originalPathAttribte;
171 }
172
173 /**
174 * Process the contained rules
175 * @param target target field to pass on to the contained rules
176 * @param request request object to pass on to the contained rules
177 * @param response response object to pass on to the contained rules
178 */
179 @Override
180 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
181 {
182 return apply(target, request, response);
183 }
184
185 /**
186 * Process the contained rules (called by matchAndApply)
187 * @param target target field to pass on to the contained rules
188 * @param request request object to pass on to the contained rules
189 * @param response response object to pass on to the contained rules
190 */
191 protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
192 {
193 boolean original_set=_originalPathAttribute==null;
194
195 for (Rule rule : _rules)
196 {
197 String applied=rule.matchAndApply(target,request, response);
198 if (applied!=null)
199 {
200 LOG.debug("applied {}",rule);
201 if (!target.equals(applied))
202 {
203 LOG.debug("rewrote {} to {}",target,applied);
204 if (!original_set)
205 {
206 original_set=true;
207 request.setAttribute(_originalPathAttribute, target);
208 }
209
210 if (_rewriteRequestURI)
211 {
212 if (rule instanceof Rule.ApplyURI && !target.equals(request.getRequestURI()))
213 ((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
214 else
215 ((Request)request).setRequestURI(applied);
216 }
217
218 if (_rewritePathInfo)
219 ((Request)request).setPathInfo(applied);
220
221 target=applied;
222 }
223
224 if (rule.isHandling())
225 {
226 LOG.debug("handling {}",rule);
227 (request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest()).setHandled(true);
228 }
229
230 if (rule.isTerminating())
231 {
232 LOG.debug("terminating {}",rule);
233 break;
234 }
235 }
236 }
237
238 return target;
239 }
240 }