Skip to content

Commit 3a6546e

Browse files
committed
Fix ocpsoft#213: Performance issue with URLBuilder
1 parent b06c00c commit 3a6546e

5 files changed

Lines changed: 11 additions & 47 deletions

File tree

‎addressbuilder/src/main/java/org/ocpsoft/urlbuilder/AddressBuilder.java‎

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,12 @@ protected Address buildLiteral()
8585

8686
/**
8787
* Create a new {@link Address} from the given URL. Improperly formatted or encoded URLs are not parse-able and will
88-
* result in an exception.
88+
* result in an exception. No builder parameterization is possible using this method.
8989
*
9090
* @see http://en.wikipedia.org/wiki/URI_scheme
9191
* @throws IllegalArgumentException when the input URL or URL fragment is not valid.
9292
*/
9393
public static Address create(String url) throws IllegalArgumentException
94-
{
95-
try {
96-
URI u = new URI(url);
97-
String scheme = u.getScheme();
98-
String host = u.getHost();
99-
if (scheme != null && host == null)
100-
return AddressBuilder.begin().scheme(u.getScheme()).schemeSpecificPart(u.getRawSchemeSpecificPart())
101-
.build();
102-
else
103-
return AddressBuilder.begin().scheme(scheme).domain(host).port(u.getPort())
104-
.path(u.getRawPath()).queryLiteral(u.getRawQuery()).anchor(u.getRawFragment()).build();
105-
}
106-
catch (URISyntaxException e) {
107-
throw new IllegalArgumentException(
108-
"[" + url + "] is not a valid URL fragment. Consider encoding relevant portions of the URL with ["
109-
+ Encoder.class
110-
+ "], or use the provided builder pattern via this class to specify part encoding.", e);
111-
}
112-
}
113-
114-
/**
115-
* Create a new {@link Address} from the given URL. Improperly formatted or encoded URLs are not parse-able and will
116-
* result in an exception.
117-
* <p>
118-
*
119-
* @see http://en.wikipedia.org/wiki/URI_scheme
120-
* @throws IllegalArgumentException when the input URL or URL fragment is not valid.
121-
*/
122-
public static Address createLiteral(String url) throws IllegalArgumentException
12394
{
12495
try {
12596
URI u = new URI(url);
@@ -130,14 +101,13 @@ public static Address createLiteral(String url) throws IllegalArgumentException
130101
.buildLiteral();
131102
else
132103
return AddressBuilder.begin().scheme(scheme).domain(host).port(u.getPort())
133-
.path(u.getRawPath()).queryLiteral(u.getRawQuery()).anchor(u.getRawFragment())
134-
.buildLiteral();
104+
.path(u.getRawPath()).queryLiteral(u.getRawQuery()).anchor(u.getRawFragment()).buildLiteral();
135105
}
136106
catch (URISyntaxException e) {
137107
throw new IllegalArgumentException(
138108
"[" + url + "] is not a valid URL fragment. Consider encoding relevant portions of the URL with ["
139109
+ Encoder.class
140-
+ "], or use the provided builder pattern via this class to specify part encoding", e);
110+
+ "], or use the provided builder pattern via this class to specify part encoding.", e);
141111
}
142112
}
143113

‎addressbuilder/src/test/java/org/ocpsoft/urlbuilder/AddressBuilderTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void testEmptyAnchorOnly()
309309
@Test
310310
public void shouldCreateAddressFromUrlWithCurlyBrace()
311311
{
312-
Address address = AddressBuilder.createLiteral("http://localhost/somepath/%7Bsomething%7D");
312+
Address address = AddressBuilder.create("http://localhost/somepath/%7Bsomething%7D");
313313
assertEquals("/somepath/%7Bsomething%7D", address.getPath());
314314
assertEquals("http://localhost/somepath/%7Bsomething%7D", address.toString());
315315
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@
3636
import org.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
3737
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
3838
import org.ocpsoft.rewrite.servlet.spi.RequestParameterProvider;
39-
import org.ocpsoft.rewrite.servlet.util.URLBuilder;
4039
import org.ocpsoft.urlbuilder.Address;
40+
import org.ocpsoft.urlbuilder.AddressBuilder;
4141

4242
/**
4343
* A {@link Condition} that inspects the value of {@link HttpServletRewrite#getRequestPath()}
4444
*
4545
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4646
*/
47-
@SuppressWarnings("deprecation")
4847
public abstract class Path extends HttpCondition implements Parameterized
4948
{
5049
private final ParameterizedPatternParser expression;
@@ -129,7 +128,7 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
129128
return false;
130129
}
131130
else
132-
url = URLBuilder.createFrom(event.getInboundAddress().getPath()).decode().toPath();
131+
url = AddressBuilder.begin().pathDecoded(event.getInboundAddress().getPath()).buildLiteral().toString();
133132

134133
String contextPath = event.getContextPath();
135134
if (!contextPath.equals("/") && url.startsWith(contextPath))

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,18 @@
3232
import org.ocpsoft.rewrite.param.ParameterizedPatternParser;
3333
import org.ocpsoft.rewrite.param.RegexConstraint;
3434
import org.ocpsoft.rewrite.param.RegexParameterizedPatternParser;
35-
import org.ocpsoft.rewrite.servlet.config.HttpCondition;
3635
import org.ocpsoft.rewrite.servlet.config.bind.RequestBinding;
3736
import org.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
3837
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
3938
import org.ocpsoft.rewrite.servlet.spi.RequestParameterProvider;
40-
import org.ocpsoft.rewrite.servlet.util.URLBuilder;
4139
import org.ocpsoft.urlbuilder.Address;
40+
import org.ocpsoft.urlbuilder.AddressBuilder;
4241

4342
/**
4443
* A {@link Condition} that inspects the value of {@link HttpServletRewrite#getAddress()} path and query string.
4544
*
4645
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4746
*/
48-
@SuppressWarnings("deprecation")
4947
public abstract class PathAndQuery extends HttpCondition implements Parameterized
5048
{
5149
private final ParameterizedPatternParser expression;
@@ -132,7 +130,8 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
132130
return false;
133131
}
134132
else
135-
url = URLBuilder.createFrom(event.getInboundAddress().getPathAndQuery()).decode().toURL();
133+
url = AddressBuilder.begin().pathDecoded(event.getInboundAddress().getPath())
134+
.queryDecoded(event.getInboundAddress().getQuery()).build().toString();
136135

137136
String contextPath = event.getContextPath();
138137
if (!contextPath.equals("/") && url.startsWith(contextPath))

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/impl/HttpRewriteWrappedResponse.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@
4949
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
5050
import org.ocpsoft.rewrite.servlet.spi.OutboundRewriteProducer;
5151
import org.ocpsoft.rewrite.servlet.spi.RewriteLifecycleListener;
52-
import org.ocpsoft.rewrite.servlet.util.URLBuilder;
5352
import org.ocpsoft.rewrite.spi.RewriteProvider;
5453
import org.ocpsoft.urlbuilder.Address;
5554
import org.ocpsoft.urlbuilder.AddressBuilder;
5655

5756
/**
5857
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
5958
*/
60-
@SuppressWarnings("deprecation")
6159
public class HttpRewriteWrappedResponse extends RewriteWrappedResponse
6260
{
6361
private final HttpServletRequest request;
@@ -378,8 +376,7 @@ public String encodeUrl(final String url)
378376
@Override
379377
public String encodeRedirectURL(final String url)
380378
{
381-
String step = URLBuilder.createFrom(url).toURL();
382-
Address address = AddressBuilder.createLiteral(step);
379+
Address address = AddressBuilder.create(url);
383380
OutboundServletRewrite<ServletRequest, ServletResponse, Address> event = rewrite(address);
384381

385382
if (event.getFlow().is(ServletRewriteFlow.ABORT_REQUEST))
@@ -392,8 +389,7 @@ public String encodeRedirectURL(final String url)
392389
@Override
393390
public String encodeURL(final String url)
394391
{
395-
String step = URLBuilder.createFrom(url).toURL();
396-
Address address = AddressBuilder.createLiteral(step);
392+
Address address = AddressBuilder.create(url);
397393
OutboundServletRewrite<ServletRequest, ServletResponse, Address> event = rewrite(address);
398394

399395
if (event.getFlow().is(ServletRewriteFlow.ABORT_REQUEST))

0 commit comments

Comments
 (0)