1
2
3
4
5
6
7
8
9
10
11
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
29
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
62
63
64
65
66 @Deprecated
67 public void setLegacyRule(LegacyRule legacyRule)
68 {
69 _legacy = legacyRule;
70 }
71
72
73
74
75
76
77 public Rule[] getRules()
78 {
79 return _rules;
80 }
81
82
83
84
85
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
104
105
106 public void addRule(Rule rule)
107 {
108 _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
109 }
110
111
112
113
114
115
116
117 public boolean isRewriteRequestURI()
118 {
119 return _rewriteRequestURI;
120 }
121
122
123
124
125
126
127 public void setRewriteRequestURI(boolean rewriteRequestURI)
128 {
129 _rewriteRequestURI=rewriteRequestURI;
130 }
131
132
133
134
135
136
137 public boolean isRewritePathInfo()
138 {
139 return _rewritePathInfo;
140 }
141
142
143
144
145
146
147 public void setRewritePathInfo(boolean rewritePathInfo)
148 {
149 _rewritePathInfo=rewritePathInfo;
150 }
151
152
153
154
155
156
157 public String getOriginalPathAttribute()
158 {
159 return _originalPathAttribute;
160 }
161
162
163
164
165
166
167 public void setOriginalPathAttribute(String originalPathAttribte)
168 {
169 _originalPathAttribute=originalPathAttribte;
170 }
171
172
173
174
175
176
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
186
187
188
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 if (!target.equals(applied))
201 {
202 LOG.debug("rewrote {} to {}",target,applied);
203 if (!original_set)
204 {
205 original_set=true;
206 request.setAttribute(_originalPathAttribute, target);
207 }
208
209 if (_rewriteRequestURI)
210 {
211 if (rule instanceof Rule.ApplyURI && !target.equals(request.getRequestURI()))
212 ((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
213 else
214 ((Request)request).setRequestURI(applied);
215 }
216
217 if (_rewritePathInfo)
218 ((Request)request).setPathInfo(applied);
219
220 target=applied;
221 }
222
223 if (rule.isHandling())
224 {
225 LOG.debug("handling {}",rule);
226 (request instanceof Request?(Request)request:AbstractHttpConnection.getCurrentConnection().getRequest()).setHandled(true);
227 }
228
229 if (rule.isTerminating())
230 {
231 LOG.debug("terminating {}",rule);
232 break;
233 }
234 }
235 }
236
237 return target;
238 }
239 }