Skip to content

Commit 01be7d5

Browse files
committed
Updated URLBuilder API to provide QueryString
1 parent a97f265 commit 01be7d5

13 files changed

Lines changed: 280 additions & 175 deletions

File tree

‎api-servlet/src/main/java/com/ocpsoft/rewrite/servlet/http/event/HttpServletRewrite.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,25 @@ public interface HttpServletRewrite extends
3636
* Portion of the request URL representing request path within the application. The context path is not included, and
3737
* should be retrieved using {@link #getContextPath()}
3838
*/
39-
public String getRequestURL();
39+
public String getRequestPath();
4040

4141
/**
4242
* Portion of the request URL representing the query string.
4343
*/
4444
public String getRequestQueryString();
4545

4646
/**
47-
* Portion of the request URL joining the {@link #getRequestURL()} and {@link #getRequestQueryString()}. If
47+
* Portion of the request URL joining the {@link #getRequestPath()} and {@link #getRequestQueryString()}. If
4848
* {@link #getRequestQueryString()} is valued, this will return "?"; otherwise, if {@link #getRequestQueryString()}
4949
* is empty, this too will return an empty string.
5050
*/
5151
String getRequestQueryStringSeparator();
5252

5353
/**
54-
* Return the full URL including query string. Note that for {@link HttpInboundServletRewrite} events, this method
55-
* returns {@link #getRequestURL()} + {@link #getRequestQueryStringSeparator()} + {@link #getRequestQueryString()};
56-
* for {@link HttpOutboundServletRewrite} events, this method returns
54+
* For {@link HttpInboundServletRewrite} events, return the request URL excluding context path, but including query
55+
* string: {@link #getRequestPath()} + {@link #getRequestQueryStringSeparator()} + {@link #getRequestQueryString()}
56+
* <p>
57+
* For {@link HttpOutboundServletRewrite} events, this method returns
5758
* {@link HttpOutboundServletRewrite#getOutboundURL()}
5859
*/
5960
public String getURL();

‎api/src/main/java/com/ocpsoft/rewrite/bind/El.java‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ public Object retrieve(final Rewrite event, final EvaluationContext context)
181181
}
182182
catch (UnsupportedEvaluationException e) {
183183
log.debug("El provider [" + provider.getClass().getName()
184-
+ "] could not invoke method #{"
185-
+ getExpression + "}", e);
184+
+ "] could not invoke method ["
185+
+ getExpression + "]", e);
186186
}
187187
catch (Exception e)
188188
{
189189
throw new RewriteException("El provider [" + provider.getClass().getName()
190-
+ "] could not retrieve value from property #{"
191-
+ getExpression + "}", e);
190+
+ "] could not retrieve value from property ["
191+
+ getExpression + "]", e);
192192

193193
}
194194
}
@@ -210,13 +210,13 @@ public Object submit(final Rewrite event, final EvaluationContext context, final
210210
}
211211
catch (UnsupportedEvaluationException e) {
212212
log.debug("El provider [" + provider.getClass().getName()
213-
+ "] could not submit method #{" + setExpression
213+
+ "] could not submit method [" + setExpression
214214
+ "} with value [" + value + "]", e);
215215
}
216216
catch (Exception e)
217217
{
218218
throw new RewriteException("El provider [" + provider.getClass().getName()
219-
+ "] could not submit method #{"
219+
+ "] could not submit method ["
220220
+ setExpression + "} with value [" + value + "]", e);
221221

222222
}
@@ -239,7 +239,7 @@ public boolean supportsSubmission()
239239
@Override
240240
public String toString()
241241
{
242-
return "ElMethod [retrieve= #{ " + getExpression + " }, submit= #{ " + setExpression + " }";
242+
return "ElMethod [retrieve= [ " + getExpression + " }, submit= [ " + setExpression + " ]";
243243
}
244244

245245
}
@@ -272,13 +272,13 @@ public Object retrieve(final Rewrite event, final EvaluationContext context)
272272
}
273273
catch (UnsupportedEvaluationException e)
274274
{
275-
log.debug("El provider [" + provider.getClass().getName() + "] could not extract value from property #{"
276-
+ expression + "}", e);
275+
log.debug("El provider [" + provider.getClass().getName() + "] could not extract value from property ["
276+
+ expression + "]", e);
277277
}
278278
catch (Exception e) {
279279
throw new RewriteException("El provider [" + provider.getClass().getName()
280-
+ "] could not extract value from property #{"
281-
+ expression + "}", e);
280+
+ "] could not extract value from property ["
281+
+ expression + "]", e);
282282
}
283283

284284
if (value != null)
@@ -324,12 +324,12 @@ public Object submit(final Rewrite event, final EvaluationContext context, final
324324
catch (UnsupportedEvaluationException e)
325325
{
326326
log.debug("El provider [" + provider.getClass().getName()
327-
+ "] could not inject property #{" + expression
327+
+ "] could not inject property [" + expression
328328
+ "} with value [" + value + "]", e);
329329
}
330330
catch (Exception e) {
331331
throw new RewriteException("El provider [" + provider.getClass().getName()
332-
+ "] could not inject property #{" + expression
332+
+ "] could not inject property [" + expression
333333
+ "} with value [" + value + "]", e);
334334
}
335335
}
@@ -340,7 +340,7 @@ public Object submit(final Rewrite event, final EvaluationContext context, final
340340
@Override
341341
public String toString()
342342
{
343-
return "ElProperty #{ " + expression + " }";
343+
return "ElProperty [ " + expression + " ]";
344344
}
345345

346346
}

‎api/src/main/java/com/ocpsoft/rewrite/bind/Evaluation.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ private String getParameterName(final CharSequence parameter)
8686
@Override
8787
public Object retrieve(final Rewrite event, final EvaluationContext context)
8888
{
89-
return context.get(getParameterName(property));
89+
Object object = context.get(getParameterName(property));
90+
if (object.getClass().isArray())
91+
{
92+
Object[] array = (Object[]) object;
93+
if (array.length == 1)
94+
{
95+
return array[0];
96+
}
97+
}
98+
return object;
9099
}
91100

92101
@Override
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ocpsoft.rewrite.util;
17+
18+
import java.util.Collection;
19+
import java.util.Iterator;
20+
21+
/**
22+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
23+
*/
24+
public abstract class Strings
25+
{
26+
/**
27+
* Join a collection of strings using the given delimeter.
28+
*/
29+
public static String join(final Collection<?> collection, final String delimiter)
30+
{
31+
StringBuffer buffer = new StringBuffer();
32+
Iterator<?> iter = collection.iterator();
33+
while (iter.hasNext())
34+
{
35+
buffer.append(iter.next());
36+
if (iter.hasNext())
37+
{
38+
buffer.append(delimiter);
39+
}
40+
}
41+
return buffer.toString();
42+
}
43+
}

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/Path.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.ocpsoft.rewrite.util.Assert;
3232

3333
/**
34-
* A {@link Condition} that inspects the value of {@link HttpServletRewrite#getRequestURL()}
34+
* A {@link Condition} that inspects the value of {@link HttpServletRewrite#getRequestPath()}
3535
*
3636
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3737
*/
@@ -116,7 +116,7 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
116116
}
117117
}
118118
else
119-
requestURL = event.getRequestURL();
119+
requestURL = event.getRequestPath();
120120

121121
if (expression.matches(requestURL))
122122
{

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/impl/HttpInboundRewriteImpl.java‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,11 @@ private String encodeRedirectUrl(final HttpServletResponse response, final Strin
145145
{
146146
try
147147
{
148-
// The base URL and Query String require different encoding rules.
149-
String[] urlParts = url.split("\\?", 2);
148+
URLBuilder builder = URLBuilder.build(url);
149+
String path = builder.decode().toPath();
150150

151-
String baseUrlEncoded = new URLBuilder(urlParts[0]).decode().toURL();
152-
153-
if ((urlParts.length > 1) && (urlParts[1] != null) && !"".equals(urlParts[1]))
154-
{
155-
return response.encodeRedirectURL(baseUrlEncoded
156-
+ QueryStringBuilder.build(urlParts[1]).encode().toQueryString());
157-
}
158-
else
159-
{
160-
return response.encodeRedirectURL(baseUrlEncoded);
161-
}
151+
return response.encodeRedirectURL(path
152+
+ builder.getQueryStringBuilder().encode().toQueryString());
162153
}
163154
catch (Exception e)
164155
{
@@ -175,15 +166,15 @@ public String getContextPath()
175166
}
176167

177168
@Override
178-
public String getRequestURL()
169+
public String getRequestPath()
179170
{
180171
String url = getRequest().getRequestURI();
181172
if (url.startsWith(getContextPath()))
182173
{
183174
url = url.substring(getContextPath().length());
184175
}
185176

186-
url = new URLBuilder(url).decode().toURL();
177+
url = URLBuilder.build(url).decode().toURL();
187178
return url;
188179
}
189180

@@ -208,6 +199,13 @@ public String getRequestQueryString()
208199
@Override
209200
public String getURL()
210201
{
211-
return getRequestURL() + getRequestQueryStringSeparator() + getRequestQueryString();
202+
return getRequestPath() + getRequestQueryStringSeparator() + getRequestQueryString();
203+
}
204+
205+
@Override
206+
public String toString()
207+
{
208+
return "InboundRewrite [url=" + getURL() + ", flow=" + getFlow() + ", dispatchResource=" + getDispatchResource()
209+
+ "]";
212210
}
213211
}

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/impl/HttpOutboundRewriteImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String getContextPath()
5858
}
5959

6060
@Override
61-
public String getRequestURL()
61+
public String getRequestPath()
6262
{
6363
return getRequest().getRequestURI().substring(getContextPath().length());
6464
}

0 commit comments

Comments
 (0)