Skip to content

Commit f816724

Browse files
Matt FletcherMatt Fletcher
authored andcommitted
Deprecate URLBuilder in favor of AddressBuilder.
These are the last two internal use cases for URLBuilder. And replacing them makes my IDE happy. Both are similar to the create case but different. InboundRewriteRuleAdaptor needs the encoding. RewriteViewHandler needs to merge two query strings.
1 parent b988297 commit f816724

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
*/
2222
package org.ocpsoft.rewrite.prettyfaces;
2323

24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
2427
import org.ocpsoft.rewrite.config.Rule;
2528
import org.ocpsoft.rewrite.context.EvaluationContext;
2629
import org.ocpsoft.rewrite.event.Rewrite;
2730
import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
2831
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
29-
import org.ocpsoft.rewrite.servlet.util.URLBuilder;
32+
import org.ocpsoft.urlbuilder.Address;
33+
import org.ocpsoft.urlbuilder.AddressBuilder;
34+
import org.ocpsoft.urlbuilder.util.Encoder;
3035

3136
import com.ocpsoft.pretty.faces.config.rewrite.Redirect;
3237
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
@@ -88,8 +93,7 @@ public void perform(final Rewrite event, final EvaluationContext context)
8893
+ QueryString.build(httpRewrite.getInboundAddress().getQuery()).toQueryString();
8994

9095
String contextPath = ((HttpServletRewrite) event).getContextPath();
91-
if (!contextPath.equals("/") && originalUrl.startsWith(contextPath))
92-
originalUrl = originalUrl.substring(contextPath.length());
96+
originalUrl = StringUtils.removePathPrefix(contextPath, originalUrl);
9397

9498
String newUrl = engine.processInbound(((HttpServletRewrite) event).getRequest(),
9599
((HttpServletRewrite) event).getResponse(), rule, originalUrl);
@@ -110,14 +114,14 @@ public void perform(final Rewrite event, final EvaluationContext context)
110114
{
111115

112116
/*
113-
* Add context path and encode request using encodeRedirectURL().
117+
* Add context path.
114118
*/
115119
redirectURL = contextPath + newUrl;
116120
}
117121
else if (StringUtils.isNotBlank(rule.getUrl()))
118122
{
119123
/*
120-
* This is a custom location - don't call encodeRedirectURL() and don't add context path, just
124+
* This is a custom location - don't add context path, just
121125
* redirect to the encoded URL
122126
*/
123127
redirectURL = newUrl.trim();
@@ -126,8 +130,30 @@ else if (StringUtils.isNotBlank(rule.getUrl()))
126130

127131
if (redirectURL != null)
128132
{
129-
URLBuilder encodedRedirectUrl = URLBuilder.createFrom(redirectURL).encode();
130-
redirectURL = encodedRedirectUrl.toString();
133+
try
134+
{
135+
URI uri = new URI(redirectURL);
136+
if (uri.getScheme() == null && uri.getHost() != null) {
137+
Address encodedRedirectAddress
138+
= AddressBuilder.begin()
139+
.scheme(uri.getScheme())
140+
.domain(uri.getHost())
141+
.port(uri.getPort())
142+
.pathEncoded(uri.getPath())
143+
.queryLiteral(QueryString.build(uri.getQuery()).toQueryString())
144+
.anchor(uri.getRawFragment())
145+
.buildLiteral();
146+
redirectURL = encodedRedirectAddress.toString();
147+
}
148+
}
149+
catch (URISyntaxException e)
150+
{
151+
throw new IllegalArgumentException(
152+
"[" + redirectURL + "] is not a valid URL fragment. Consider encoding relevant portions of the URL with ["
153+
+ Encoder.class
154+
+ "], or use the provided builder pattern to specify part encoding.", e);
155+
}
156+
131157
if (Redirect.PERMANENT.equals(rule.getRedirect()))
132158
((HttpInboundServletRewrite) event).redirectPermanent(redirectURL);
133159
if (Redirect.TEMPORARY.equals(rule.getRedirect()))

integration-faces/src/main/java/org/ocpsoft/rewrite/faces/RewriteViewHandler.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.ocpsoft.rewrite.faces;
1717

1818
import java.io.IOException;
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.Collections;
2022
import java.util.List;
2123
import java.util.Locale;
@@ -32,7 +34,9 @@
3234
import org.ocpsoft.common.services.ServiceLoader;
3335
import org.ocpsoft.common.util.Iterators;
3436
import org.ocpsoft.rewrite.faces.spi.FacesActionUrlProvider;
35-
import org.ocpsoft.rewrite.servlet.util.URLBuilder;
37+
import org.ocpsoft.urlbuilder.Address;
38+
import org.ocpsoft.urlbuilder.AddressBuilder;
39+
import org.ocpsoft.urlbuilder.util.Encoder;
3640

3741
/**
3842
* @author Lincoln Baxter, III <[email protected]>
@@ -131,9 +135,37 @@ public String getActionURL(final FacesContext context, final String viewId)
131135
String parentActionURL = parent.getActionURL(context, viewId);
132136
if (parentActionURL.contains("?"))
133137
{
134-
URLBuilder builder = URLBuilder.createFrom(result);
135-
builder.getQueryStringBuilder().addParameters(parentActionURL);
136-
result = builder.toURL();
138+
try
139+
{
140+
URI uri = new URI(result);
141+
URI action = new URI(parentActionURL);
142+
StringBuilder query = new StringBuilder(uri.getQuery());
143+
if (query.length() > 0 && action.getQuery() != null) {
144+
query.append('&');
145+
}
146+
query.append(action.getQuery());
147+
148+
if (uri.getScheme() == null && uri.getHost() != null) {
149+
Address address
150+
= AddressBuilder.begin()
151+
.scheme(uri.getScheme())
152+
.domain(uri.getHost())
153+
.port(uri.getPort())
154+
.pathEncoded(uri.getPath())
155+
.queryLiteral(query.toString())
156+
.anchor(uri.getRawFragment())
157+
.buildLiteral();
158+
159+
result = address.toString();
160+
}
161+
}
162+
catch (URISyntaxException e)
163+
{
164+
throw new IllegalArgumentException(
165+
"[" + result + "] is not a valid URL fragment. Consider encoding relevant portions of the URL with ["
166+
+ Encoder.class
167+
+ "], or use the provided builder pattern to specify part encoding.", e);
168+
}
137169
}
138170
}
139171
}

0 commit comments

Comments
 (0)