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