Skip to content

Commit 084fe48

Browse files
committed
Fixed context path removal for root context path (ocpsoft#180)
1 parent 122eb48 commit 084fe48

9 files changed

Lines changed: 27 additions & 19 deletions

File tree

‎config-prettyfaces/src/main/java/com/ocpsoft/pretty/PrettyContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public String getContextPath()
229229
*/
230230
public String stripContextPath(String uri)
231231
{
232-
if (uri.startsWith(contextPath))
232+
if (!contextPath.equals("/") && uri.startsWith(contextPath))
233233
{
234234
uri = uri.substring(contextPath.length());
235235
}

‎config-prettyfaces/src/main/java/com/ocpsoft/pretty/faces/servlet/PrettyFacesWrappedResponse.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private String rewrite(final String url)
238238
*/
239239
private String stripContextPath(String uri)
240240
{
241-
if (uri.startsWith(contextPath))
241+
if (!contextPath.equals("/") && uri.startsWith(contextPath))
242242
{
243243
uri = uri.substring(contextPath.length());
244244
}

‎config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/InboundRewriteRuleAdaptor.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public boolean evaluate(final Rewrite event, final EvaluationContext context)
6565
String url = URL.build(path).decode().toURL()
6666
+ QueryString.build(httpRewrite.getInboundAddress().getQuery()).toQueryString();
6767

68-
if (url.startsWith(httpRewrite.getContextPath()))
69-
url = url.substring(httpRewrite.getContextPath().length());
68+
String contextPath = httpRewrite.getContextPath();
69+
if (!contextPath.equals("/") && url.startsWith(contextPath))
70+
url = url.substring(contextPath.length());
7071

7172
if (rule.matches(url))
7273
{
@@ -86,8 +87,9 @@ public void perform(final Rewrite event, final EvaluationContext context)
8687
originalUrl = URL.build(originalUrl).decode().toURL()
8788
+ QueryString.build(httpRewrite.getInboundAddress().getQuery()).toQueryString();
8889

89-
if (originalUrl.startsWith(((HttpServletRewrite) event).getContextPath()))
90-
originalUrl = originalUrl.substring(((HttpServletRewrite) event).getContextPath().length());
90+
String contextPath = ((HttpServletRewrite) event).getContextPath();
91+
if (!contextPath.equals("/") && originalUrl.startsWith(contextPath))
92+
originalUrl = originalUrl.substring(contextPath.length());
9193

9294
String newUrl = engine.processInbound(((HttpServletRewrite) event).getRequest(),
9395
((HttpServletRewrite) event).getResponse(), rule, originalUrl);
@@ -110,7 +112,7 @@ public void perform(final Rewrite event, final EvaluationContext context)
110112
/*
111113
* Add context path and encode request using encodeRedirectURL().
112114
*/
113-
redirectURL = ((HttpServletRewrite) event).getContextPath() + newUrl;
115+
redirectURL = contextPath + newUrl;
114116
}
115117
else if (StringUtils.isNotBlank(rule.getUrl()))
116118
{

‎config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/OutboundRewriteRuleAdaptor.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public OutboundRewriteRuleAdaptor(final RewriteRule rule)
5252
*/
5353
private String stripContextPath(final String contextPath, String uri)
5454
{
55-
if (uri.startsWith(contextPath))
55+
if (!contextPath.equals("/") && uri.startsWith(contextPath))
5656
{
5757
uri = uri.substring(contextPath.length());
5858
}
@@ -73,8 +73,9 @@ public boolean evaluate(final Rewrite event, final EvaluationContext context)
7373
Address outboundResource = ((HttpOutboundServletRewrite) event).getAddress();
7474
String outboundUrl = outboundResource.toString();
7575

76-
if (outboundUrl.startsWith(((HttpServletRewrite) event).getContextPath()))
77-
outboundUrl = outboundUrl.substring(((HttpServletRewrite) event).getContextPath().length());
76+
String contextPath = ((HttpServletRewrite) event).getContextPath();
77+
if (!contextPath.equals("/") && outboundUrl.startsWith(contextPath))
78+
outboundUrl = outboundUrl.substring(contextPath.length());
7879

7980
if (rule.matches(URL.build(outboundUrl).decode().toURL()))
8081
return true;

‎config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/UrlMappingRuleAdaptor.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ public boolean evaluate(final Rewrite event, final EvaluationContext ec)
203203
else if ((event instanceof HttpOutboundServletRewrite)
204204
&& mapping.isOutbound()) {
205205
String outboundURL = ((HttpOutboundServletRewrite) event).getOutboundAddress().toString();
206-
if (outboundURL.startsWith(((HttpServletRewrite) event).getContextPath()))
206+
String contextPath = ((HttpServletRewrite) event).getContextPath();
207+
if (!contextPath.equals("/") && outboundURL.startsWith(contextPath))
207208
{
208-
outboundURL = outboundURL.substring(((HttpServletRewrite) event).getContextPath().length());
209+
outboundURL = outboundURL.substring(contextPath.length());
209210
}
210211

211212
return evaluateOutbound(outboundURL);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
131131
else
132132
url = URLBuilder.createFrom(event.getInboundAddress().getPath()).decode().toPath();
133133

134-
if (url.startsWith(event.getContextPath()))
135-
url = url.substring(event.getContextPath().length());
134+
String contextPath = event.getContextPath();
135+
if (!contextPath.equals("/") && url.startsWith(contextPath))
136+
url = url.substring(contextPath.length());
136137

137138
return (expression.matches(event, context, url));
138139
}

‎config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/PathAndQuery.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
134134
else
135135
url = URLBuilder.createFrom(event.getInboundAddress().getPathAndQuery()).decode().toURL();
136136

137-
if (url.startsWith(event.getContextPath()))
138-
url = url.substring(event.getContextPath().length());
137+
String contextPath = event.getContextPath();
138+
if (!contextPath.equals("/") && url.startsWith(contextPath))
139+
url = url.substring(contextPath.length());
139140

140141
return (expression.matches(event, context, url));
141142
}

‎config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/URL.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
128128
if (event instanceof HttpOutboundServletRewrite)
129129
{
130130
requestURL = ((HttpOutboundServletRewrite) event).getOutboundAddress().toString();
131-
if (requestURL.startsWith(event.getContextPath()))
131+
String contextPath = event.getContextPath();
132+
if (!contextPath.equals("/") && requestURL.startsWith(contextPath))
132133
{
133134
requestURL = requestURL.substring(event.getContextPath().length());
134135
}

‎config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/rule/Join.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ else if (event instanceof HttpOutboundServletRewrite)
293293

294294
Address rewrittenAddress = ((HttpOutboundServletRewrite) event).getOutboundAddress();
295295
String rewrittenPath = rewrittenAddress.getPath();
296-
if (rewrittenPath.startsWith(((HttpServletRewrite) event).getContextPath()))
297-
rewrittenPath = rewrittenPath.substring(((HttpServletRewrite) event).getContextPath().length());
296+
String contextPath = ((HttpServletRewrite) event).getContextPath();
297+
if (!contextPath.equals("/") && rewrittenPath.startsWith(contextPath))
298+
rewrittenPath = rewrittenPath.substring(contextPath.length());
298299

299300
if (!outboundAddress.equals(rewrittenAddress)
300301
&& !requestPath.getExpression().matches(event, context, rewrittenPath))

0 commit comments

Comments
 (0)