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.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
30
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
63
64
65
66
67 @Deprecated
68 public void setLegacyRule(LegacyRule legacyRule)
69 {
70 _legacy = legacyRule;
71 }
72
73
74
75
76
77
78 public Rule[] getRules()
79 {
80 return _rules;
81 }
82
83
84
85
86
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
105
106
107 public void addRule(Rule rule)
108 {
109 _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
110 }
111
112
113
114
115
116
117
118 public boolean isRewriteRequestURI()
119 {
120 return _rewriteRequestURI;
121 }
122
123
124
125
126
127
128 public void setRewriteRequestURI(boolean rewriteRequestURI)
129 {
130 _rewriteRequestURI=rewriteRequestURI;
131 }
132
133
134
135
136
137
138 public boolean isRewritePathInfo()
139 {
140 return _rewritePathInfo;
141 }
142
143
144
145
146
147
148 public void setRewritePathInfo(boolean rewritePathInfo)
149 {
150 _rewritePathInfo=rewritePathInfo;
151 }
152
153
154
155
156
157
158 public String getOriginalPathAttribute()
159 {
160 return _originalPathAttribute;
161 }
162
163
164
165
166
167
168 public void setOriginalPathAttribute(String originalPathAttribte)
169 {
170 _originalPathAttribute=originalPathAttribte;
171 }
172
173
174
175
176
177
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
187
188
189
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 }