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