1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Request;
27 import org.eclipse.jetty.util.ArrayUtil;
28 import org.eclipse.jetty.util.URIUtil;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
31
32
33
34
35
36 public class RuleContainer extends Rule
37 {
38 public static final String ORIGINAL_QUERYSTRING_ATTRIBUTE_SUFFIX = ".QUERYSTRING";
39 private static final Logger LOG = Log.getLogger(RuleContainer.class);
40
41 protected Rule[] _rules;
42
43 protected String _originalPathAttribute;
44 protected String _originalQueryStringAttribute;
45 protected boolean _rewriteRequestURI=true;
46 protected boolean _rewritePathInfo=true;
47
48
49
50
51
52
53 public Rule[] getRules()
54 {
55 return _rules;
56 }
57
58
59
60
61
62
63 public void setRules(Rule[] rules)
64 {
65 _rules = rules;
66 }
67
68
69
70
71
72
73 public void addRule(Rule rule)
74 {
75 _rules = ArrayUtil.addToArray(_rules,rule,Rule.class);
76 }
77
78
79
80
81
82
83
84 public boolean isRewriteRequestURI()
85 {
86 return _rewriteRequestURI;
87 }
88
89
90
91
92
93
94 public void setRewriteRequestURI(boolean rewriteRequestURI)
95 {
96 _rewriteRequestURI=rewriteRequestURI;
97 }
98
99
100
101
102
103
104 public boolean isRewritePathInfo()
105 {
106 return _rewritePathInfo;
107 }
108
109
110
111
112
113
114 public void setRewritePathInfo(boolean rewritePathInfo)
115 {
116 _rewritePathInfo=rewritePathInfo;
117 }
118
119
120
121
122
123
124 public String getOriginalPathAttribute()
125 {
126 return _originalPathAttribute;
127 }
128
129
130
131
132
133
134 public void setOriginalPathAttribute(String originalPathAttribte)
135 {
136 _originalPathAttribute=originalPathAttribte;
137 _originalQueryStringAttribute = originalPathAttribte + ORIGINAL_QUERYSTRING_ATTRIBUTE_SUFFIX;
138 }
139
140
141
142
143
144
145
146 @Override
147 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
148 {
149 return apply(target, request, response);
150 }
151
152
153
154
155
156
157
158
159
160 protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
161 {
162 boolean original_set=_originalPathAttribute==null;
163
164 if (_rules==null)
165 return target;
166
167 for (Rule rule : _rules)
168 {
169 String applied=rule.matchAndApply(target,request, response);
170 if (applied!=null)
171 {
172 LOG.debug("applied {}",rule);
173 LOG.debug("rewrote {} to {}",target,applied);
174 if (!original_set)
175 {
176 original_set=true;
177 request.setAttribute(_originalPathAttribute, target);
178
179 String query = request.getQueryString();
180 if (query != null)
181 request.setAttribute(_originalQueryStringAttribute,query);
182 }
183
184 if (_rewriteRequestURI)
185 {
186 String encoded=URIUtil.encodePath(applied);
187 if (rule instanceof Rule.ApplyURI)
188 ((Rule.ApplyURI)rule).applyURI((Request)request,((Request)request).getRequestURI(), encoded);
189 else
190 ((Request)request).setURIPathQuery(encoded);
191 }
192
193 if (_rewritePathInfo)
194 ((Request)request).setPathInfo(applied);
195
196 target=applied;
197
198 if (rule.isHandling())
199 {
200 LOG.debug("handling {}",rule);
201 Request.getBaseRequest(request).setHandled(true);
202 }
203
204 if (rule.isTerminating())
205 {
206 LOG.debug("terminating {}",rule);
207 break;
208 }
209 }
210 }
211
212 return target;
213 }
214 }