1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package groovyx.net.http; |
23 | |
|
24 | |
import java.net.MalformedURLException; |
25 | |
import java.net.URI; |
26 | |
import java.net.URISyntaxException; |
27 | |
import java.net.URL; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
import org.apache.http.NameValuePair; |
34 | |
import org.apache.http.client.utils.URLEncodedUtils; |
35 | |
import org.apache.http.message.BasicNameValuePair; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public class URIBuilder { |
60 | |
protected URI base; |
61 | 8 | private final String ENC = "UTF-8"; |
62 | |
|
63 | 3 | public URIBuilder( String url ) throws URISyntaxException { |
64 | 3 | base = new URI(url); |
65 | 3 | } |
66 | |
|
67 | 0 | public URIBuilder( URL url ) throws URISyntaxException { |
68 | 0 | this.base = url.toURI(); |
69 | 0 | } |
70 | |
|
71 | 5 | public URIBuilder( URI url ) { |
72 | 5 | this.base = url; |
73 | 5 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static URI convertToURI( Object uri ) throws URISyntaxException { |
83 | 6 | if ( uri instanceof URI ) ; |
84 | 5 | else if ( uri instanceof URL ) uri = ((URL)uri).toURI(); |
85 | 5 | else uri = new URI( uri.toString() ); |
86 | 6 | return (URI)uri; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public URIBuilder setScheme( String scheme ) throws URISyntaxException { |
94 | 1 | this.base = new URI( scheme, base.getUserInfo(), |
95 | |
base.getHost(), base.getPort(), base.getPath(), |
96 | |
base.getQuery(), base.getFragment() ); |
97 | 1 | return this; |
98 | |
} |
99 | |
|
100 | |
public URIBuilder setPort( int port ) throws URISyntaxException { |
101 | 1 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
102 | |
base.getHost(), port, base.getPath(), |
103 | |
base.getQuery(), base.getFragment() ); |
104 | 1 | return this; |
105 | |
} |
106 | |
|
107 | |
public URIBuilder setHost( String host ) throws URISyntaxException { |
108 | 1 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
109 | |
host, base.getPort(), base.getPath(), |
110 | |
base.getQuery(), base.getFragment() ); |
111 | 1 | return this; |
112 | |
} |
113 | |
|
114 | |
public URIBuilder setPath( String path ) throws URISyntaxException { |
115 | 7 | path = base.resolve( path ).getPath(); |
116 | 7 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
117 | |
base.getHost(), base.getPort(), path, |
118 | |
base.getQuery(), base.getFragment() ); |
119 | 7 | return this; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public URIBuilder setQuery( Map<String,?> params ) throws URISyntaxException { |
129 | 3 | List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size()); |
130 | 3 | for ( Map.Entry<String, ?> entry : params.entrySet() ) { |
131 | 9 | String val = ( entry.getValue() != null ) ? |
132 | |
entry.getValue().toString() : ""; |
133 | 9 | pairs.add( new BasicNameValuePair( |
134 | |
entry.getKey(), val ) ); |
135 | 9 | } |
136 | 3 | String queryString = URLEncodedUtils.format( pairs, ENC ); |
137 | 3 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
138 | |
base.getHost(), base.getPort(), base.getPath(), |
139 | |
queryString, base.getFragment() ); |
140 | 3 | return this; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public Map<String,String> getQuery() { |
148 | 6 | Map<String,String> params = new HashMap<String, String>(); |
149 | 6 | List<NameValuePair> pairs = URLEncodedUtils.parse( this.base, ENC ); |
150 | 6 | for ( NameValuePair pair : pairs ) |
151 | 19 | params.put( pair.getName(), pair.getValue() ); |
152 | 6 | return params; |
153 | |
} |
154 | |
|
155 | |
public boolean hasQueryParam( String name ) { |
156 | 0 | return getQuery().get( name ) != null; |
157 | |
} |
158 | |
|
159 | |
public URIBuilder removeQueryParam( String param ) throws URISyntaxException { |
160 | 0 | Map<String,String> params = getQuery(); |
161 | 0 | params.remove( param ); |
162 | 0 | this.setQuery( params ); |
163 | 0 | return this; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public URIBuilder addQueryParam( String param, Object value ) throws URISyntaxException { |
174 | 1 | Map<String,String> params = getQuery(); |
175 | 1 | if ( value == null ) value = ""; |
176 | 1 | params.put( param, value.toString() ); |
177 | 1 | this.setQuery( params ); |
178 | 1 | return this; |
179 | |
} |
180 | |
|
181 | |
@SuppressWarnings("unchecked") |
182 | |
public URIBuilder addQueryParams( Map<String,?> params ) throws URISyntaxException { |
183 | 0 | Map existing = this.getQuery(); |
184 | 0 | existing.putAll( params ); |
185 | 0 | this.setQuery( existing ); |
186 | 0 | return this; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public URIBuilder setFragment( String fragment ) throws URISyntaxException { |
195 | 1 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
196 | |
base.getHost(), base.getPort(), base.getPath(), |
197 | |
base.getQuery(), fragment ); |
198 | 1 | return this; |
199 | |
} |
200 | |
|
201 | |
@Override public String toString() { |
202 | 10 | return base.toString(); |
203 | |
} |
204 | |
|
205 | |
public URL toURL() throws MalformedURLException { |
206 | 0 | return base.toURL(); |
207 | |
} |
208 | |
|
209 | 4 | public URI toURI() { return this.base; } |
210 | |
} |