11package org .ocpsoft .urlbuilder ;
22
3- import java .net .MalformedURLException ;
4- import java .net .URL ;
3+ import java .net .URI ;
4+ import java .net .URISyntaxException ;
55import java .util .ArrayList ;
6- import java .util .HashMap ;
76import java .util .LinkedHashMap ;
87import java .util .List ;
98import java .util .Map ;
109import java .util .Map .Entry ;
1110
11+ import org .ocpsoft .urlbuilder .util .Decoder ;
1212import 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+ */
1419public 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 {
0 commit comments