Skip to content

Commit 6d34621

Browse files
committed
Added Decoder, added pathEncoded, and fixed some encoding problems.
1 parent fc4a8af commit 6d34621

11 files changed

Lines changed: 236 additions & 121 deletions

File tree

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package org.ocpsoft.urlbuilder;
22

3-
import java.net.MalformedURLException;
4-
import java.net.URL;
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
55
import java.util.ArrayList;
6-
import java.util.HashMap;
76
import java.util.LinkedHashMap;
87
import java.util.List;
98
import java.util.Map;
109
import java.util.Map.Entry;
1110

11+
import org.ocpsoft.urlbuilder.util.Decoder;
1212
import org.ocpsoft.urlbuilder.util.Encoder;
1313

14+
/**
15+
* Internal state is not encoded, plain UTF-8.
16+
*
17+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
18+
*/
1419
public class AddressBuilder
1520
{
1621
private volatile Address address;
@@ -39,32 +44,22 @@ protected Address build()
3944
return address;
4045
}
4146

42-
public static Address create(String url)
47+
/**
48+
* Must be given a fully encoded URL. Otherwise such URLs are not parse-able.
49+
*
50+
* @throws IllegalArgumentException when the input URL or URL fragment is not valid.
51+
*/
52+
public static Address create(String url) throws IllegalArgumentException
4353
{
4454
try {
45-
46-
boolean startWithPath = url.startsWith("/");
47-
48-
URL u = null;
49-
if (startWithPath) {
50-
u = new URL("http://127.0.0.1" + url);
51-
}
52-
else {
53-
u = new URL(url);
54-
}
55-
56-
AddressBuilderBase builder = AddressBuilder.begin();
57-
if (!startWithPath) {
58-
builder.protocol(u.getProtocol()).host(u.getHost());
59-
if (u.getPort() != -1) {
60-
builder.port(u.getPort());
61-
}
62-
}
63-
64-
return builder.path(u.getPath()).queryLiteral(u.getQuery()).anchor(u.getRef()).build();
55+
URI u = new URI(url);
56+
return AddressBuilder.begin().protocol(u.getScheme()).host(u.getHost()).port(u.getPort())
57+
.pathEncoded(u.getRawPath()).queryLiteral(u.getRawQuery()).anchor(u.getRawFragment()).build();
6558
}
66-
catch (MalformedURLException e) {
67-
throw new RuntimeException(e);
59+
catch (URISyntaxException e) {
60+
throw new IllegalArgumentException(
61+
"Must pass a properly encoded URL or URL fragment. Consider encoding relevant portions of the URL with ["
62+
+ Encoder.class + "]", e);
6863
}
6964
}
7065

@@ -82,7 +77,8 @@ AddressBuilderHost host(CharSequence host)
8277

8378
AddressBuilderPort port(int port)
8479
{
85-
this.port = port;
80+
if (port >= 0)
81+
this.port = port;
8682
return new AddressBuilderPort(this);
8783
}
8884

@@ -92,6 +88,12 @@ AddressBuilderPath path(CharSequence path)
9288
return new AddressBuilderPath(this);
9389
}
9490

91+
AddressBuilderPath pathEncoded(CharSequence path)
92+
{
93+
this.path = Decoder.path(path);
94+
return new AddressBuilderPath(this);
95+
}
96+
9597
AddressBuilderQuery query(CharSequence name, Object... values)
9698
{
9799
this.queries.put(Encoder.query(name.toString()), Parameter.create(name.toString(), true, values));
@@ -111,7 +113,10 @@ AddressBuilderQuery queryLiteral(String query)
111113
if (query.startsWith("?"))
112114
query = query.substring(1);
113115

114-
Map<CharSequence, List<CharSequence>> params = new HashMap<CharSequence, List<CharSequence>>();
116+
/*
117+
* Linked hash map is important here in order to retain the order of query parameters.
118+
*/
119+
Map<CharSequence, List<CharSequence>> params = new LinkedHashMap<CharSequence, List<CharSequence>>();
115120
query = decodeHTMLAmpersands(query);
116121
int index = 0;
117122

@@ -143,7 +148,6 @@ AddressBuilderQuery queryLiteral(String query)
143148
value = pair.substring(pos + 1, pair.length());
144149
}
145150

146-
queryEncoded(name, value);
147151
List<CharSequence> list = params.get(name);
148152
if (list == null)
149153
{

‎src/main/java/org/ocpsoft/urlbuilder/AddressBuilderBase.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public AddressBuilderPath path(CharSequence path)
3434
return parent.path(path);
3535
}
3636

37+
public AddressBuilderPath pathEncoded(CharSequence path)
38+
{
39+
return parent.pathEncoded(path);
40+
}
41+
3742
public AddressBuilderQuery query(CharSequence name, Object... values)
3843
{
3944
return parent.query(name, values);

‎src/main/java/org/ocpsoft/urlbuilder/AddressBuilderHost.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public AddressBuilderPath path(CharSequence path)
2424
return parent.path(path);
2525
}
2626

27+
public AddressBuilderPath pathEncoded(CharSequence path)
28+
{
29+
return parent.pathEncoded(path);
30+
}
31+
2732
public AddressBuilderQuery query(CharSequence name, Object... values)
2833
{
2934
return parent.query(name, values);

‎src/main/java/org/ocpsoft/urlbuilder/AddressBuilderPort.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public AddressBuilderPath path(CharSequence path)
1919
return parent.path(path);
2020
}
2121

22+
public AddressBuilderPath pathEncoded(CharSequence path)
23+
{
24+
return parent.pathEncoded(path);
25+
}
26+
2227
public AddressBuilderQuery query(CharSequence name, Object... values)
2328
{
2429
return parent.query(name, values);

‎src/main/java/org/ocpsoft/urlbuilder/AddressBuilderProtocol.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public AddressBuilderPath path(CharSequence path)
2929
return parent.path(path);
3030
}
3131

32+
public AddressBuilderPath pathEncoded(CharSequence path)
33+
{
34+
return parent.pathEncoded(path);
35+
}
36+
3237
public AddressBuilderQuery query(CharSequence name, Object... values)
3338
{
3439
return parent.query(name, values);

‎src/main/java/org/ocpsoft/urlbuilder/AddressResult.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.ocpsoft.urlbuilder.util.CaptureType;
66
import org.ocpsoft.urlbuilder.util.CapturingGroup;
7+
import org.ocpsoft.urlbuilder.util.Encoder;
78
import org.ocpsoft.urlbuilder.util.ParseTools;
89

910
class AddressResult implements Address
@@ -71,7 +72,11 @@ private CharSequence toQuery(Map<CharSequence, Parameter> queries)
7172
if (parameter.getValueCount() > 0)
7273
{
7374
for (int i = 0; i < parameter.getValueCount(); i++) {
74-
result.append('=').append(parameter.getValueAsQueryParam(i));
75+
String value = parameter.getValueAsQueryParam(i);
76+
77+
if (value != null)
78+
result.append('=').append(value);
79+
7580
if (i < parameter.getValueCount() - 1)
7681
{
7782
result.append('&').append(name);
@@ -123,7 +128,7 @@ private CharSequence parameterize(Map<CharSequence, Parameter> parameters, CharS
123128
switch (sequence.charAt(cursor))
124129
{
125130
case '{':
126-
result.append(sequence.subSequence(lastEnd, cursor));
131+
result.append(Encoder.path(sequence.subSequence(lastEnd, cursor)));
127132

128133
int startPos = cursor;
129134
CapturingGroup group = ParseTools.balancedCapture(sequence, startPos, sequence.length() - 1,
@@ -150,7 +155,7 @@ private CharSequence parameterize(Map<CharSequence, Parameter> parameters, CharS
150155
}
151156

152157
if (cursor >= lastEnd)
153-
result.append(sequence.subSequence(lastEnd, cursor));
158+
result.append(Encoder.path(sequence.subSequence(lastEnd, cursor)));
154159
return result;
155160
}
156161

‎src/main/java/org/ocpsoft/urlbuilder/Parameter.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ public String getValueAsPathParam(int index)
6161

6262
public String getValueAsQueryParam(int index)
6363
{
64+
Object value = values.get(index);
6465
if (encode) {
65-
return Encoder.query(values.get(index).toString());
66+
return Encoder.query(value == null ? null : value.toString());
6667
}
6768
else {
68-
return values.get(index).toString();
69+
return value == null ? null : value.toString();
6970
}
7071
}
7172

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.ocpsoft.urlbuilder.util;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.net.URLDecoder;
7+
8+
public class Decoder
9+
{
10+
public static String path(final CharSequence path)
11+
{
12+
try
13+
{
14+
final URI uri = new URI("http://0.0.0.0/" + path);
15+
return uri.getPath().substring(1);
16+
}
17+
catch (URISyntaxException e)
18+
{
19+
throw new IllegalArgumentException(e);
20+
}
21+
}
22+
23+
public static String query(final CharSequence query)
24+
{
25+
try
26+
{
27+
return URLDecoder.decode(query.toString(), "UTF-8");
28+
}
29+
catch (UnsupportedEncodingException e)
30+
{
31+
throw new IllegalArgumentException(e);
32+
}
33+
}
34+
}

‎src/main/java/org/ocpsoft/urlbuilder/util/Encoder.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class Encoder
1414
/**
1515
* Encodes the given string as described in RFC 2396
1616
*/
17-
public static String path(String s)
17+
public static String path(CharSequence s)
1818
{
1919
try
2020
{
21-
final URI uri = new URI("http", "localhost", "/" + s, null);
22-
return uri.toASCIIString().substring(17);
21+
final URI uri = new URI("http", "0.0.0.0", "/" + s, null);
22+
return uri.toASCIIString().substring(15);
2323
}
2424
catch (URISyntaxException e)
2525
{
@@ -30,10 +30,10 @@ public static String path(String s)
3030
/**
3131
* Encodes the given string using HTML form encoding
3232
*/
33-
public static String query(String s)
33+
public static String query(CharSequence s)
3434
{
3535
try {
36-
return URLEncoder.encode(s, UTF8.name());
36+
return URLEncoder.encode(s.toString(), UTF8.name());
3737
}
3838
catch (UnsupportedEncodingException e) {
3939
throw new IllegalStateException(e);

0 commit comments

Comments
 (0)