19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
21
import java .io .UnsupportedEncodingException ;
22
+ import java .net .MalformedURLException ;
22
23
import java .net .URI ;
23
24
import java .net .URISyntaxException ;
24
- import java .util .HashMap ;
25
+ import java .net .URL ;
26
+ import java .util .ArrayList ;
25
27
import java .util .Map ;
26
28
27
29
import org .apache .commons .codec .binary .Base64 ;
28
30
import org .apache .commons .io .FileUtils ;
29
31
import org .apache .commons .lang3 .StringUtils ;
32
+ import org .apache .http .Consts ;
30
33
import org .apache .http .Header ;
31
34
import org .apache .http .HttpHeaders ;
35
+ import org .apache .http .NameValuePair ;
32
36
import org .apache .http .client .ClientProtocolException ;
33
37
import org .apache .http .client .methods .HttpDelete ;
34
38
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
35
39
import org .apache .http .client .methods .HttpGet ;
36
40
import org .apache .http .client .methods .HttpPost ;
37
41
import org .apache .http .client .methods .HttpPut ;
38
42
import org .apache .http .client .methods .HttpRequestBase ;
39
- import org .apache .http .client .utils .URIBuilder ;
43
+ import org .apache .http .client .utils .URLEncodedUtils ;
40
44
import org .apache .http .entity .ContentType ;
41
45
import org .apache .http .entity .StringEntity ;
42
46
import org .apache .http .message .BasicHeader ;
47
+ import org .apache .http .message .BasicNameValuePair ;
43
48
44
49
import lombok .Getter ;
45
50
import lombok .Setter ;
@@ -62,10 +67,10 @@ public enum HttpMethod {
62
67
protected String endpoint ;
63
68
64
69
@ Getter
65
- protected Map < String , String > params ;
70
+ protected ArrayList < NameValuePair > params ;
66
71
67
72
public HttpRequest () {
68
- params = new HashMap < >();
73
+ params = new ArrayList < NameValuePair >();
69
74
initExecutor (false );
70
75
}
71
76
@@ -79,7 +84,7 @@ public HttpRequest(HttpMethod method, HttpExecutor executor) {
79
84
}
80
85
81
86
public HttpRequest (HttpMethod method , boolean persistentClient ) {
82
- params = new HashMap < >();
87
+ params = new ArrayList < NameValuePair >();
83
88
setHttpMethod (method );
84
89
initExecutor (persistentClient );
85
90
}
@@ -205,7 +210,7 @@ public void addParameters(Map<String, String> parameters) {
205
210
* @return HttpRequest
206
211
*/
207
212
public HttpRequest addParameter (String key , String value ) {
208
- params .put ( key , value );
213
+ params .add ( new BasicNameValuePair ( key , value ) );
209
214
return this ;
210
215
}
211
216
@@ -291,9 +296,9 @@ public HttpRequest setBasicAuth(String username, String password)
291
296
*/
292
297
public ResponseEntity performRequest ()
293
298
throws URISyntaxException , ClientProtocolException , IOException {
294
- URIBuilder builder = createURIBuilder ();
295
- builder = addParametersToURIBuilder ( builder );
296
- request .setURI (builder . build () );
299
+ URI uri = createURI ();
300
+ uri = addParametersToURI ( uri );
301
+ request .setURI (uri );
297
302
return executor .executeRequest (request );
298
303
}
299
304
@@ -302,43 +307,44 @@ public ResponseEntity performRequest()
302
307
*
303
308
* @return URI
304
309
* @throws URISyntaxException
310
+ * @throws MalformedURLException
305
311
*/
306
- public URI getURI () throws URISyntaxException {
307
- URIBuilder builder = createURIBuilder ();
308
- return builder . build ();
312
+ @ Deprecated
313
+ public URI getURI () throws MalformedURLException , URISyntaxException {
314
+ return createURI ();
309
315
}
310
316
311
317
/**
312
- * Function that adds parameters to the URIBuilder
318
+ * Function that creates the URI from the baseUrl and endpoint
313
319
*
314
- * @param URIBuilder
315
- * @return URIBuilder
320
+ * @param
321
+ * @return URI
322
+ * @throws MalformedURLException
323
+ * @throws URISyntaxException
316
324
*/
317
- private URIBuilder addParametersToURIBuilder (URIBuilder builder ) {
318
- if (!params .isEmpty ()) {
319
- for (Map .Entry <String , String > entry : params .entrySet ()) {
320
- builder .addParameter (entry .getKey (), entry .getValue ());
321
- }
325
+ public URI createURI () throws MalformedURLException , URISyntaxException {
326
+ if (!StringUtils .isEmpty (endpoint ) && endpoint .startsWith ("/" )) {
327
+ return new URL (baseUrl + endpoint ).toURI ();
328
+ } else if (!StringUtils .isEmpty (endpoint )) {
329
+ return new URL (baseUrl + "/" + endpoint ).toURI ();
330
+ } else {
331
+ return new URL (baseUrl ).toURI ();
322
332
}
323
333
324
- return builder ;
325
334
}
326
335
327
336
/**
328
- * Function that creates the URI from the baseUrl and endpoint
337
+ * Function that adds parameters to the URI
329
338
*
330
- * @param
331
- * @return URIBuilder
339
+ * @param oldUri
340
+ * @return URI
332
341
* @throws URISyntaxException
333
342
*/
334
- private URIBuilder createURIBuilder () throws URISyntaxException {
335
- if (!StringUtils .isEmpty (endpoint ) && endpoint .startsWith ("/" )) {
336
- return new URIBuilder (baseUrl + endpoint );
337
- } else if (!StringUtils .isEmpty (endpoint )) {
338
- return new URIBuilder (baseUrl + "/" + endpoint );
339
- } else {
340
- return new URIBuilder (baseUrl );
341
- }
343
+ private URI addParametersToURI (URI oldUri ) throws URISyntaxException {
344
+ String query = URLEncodedUtils .format (params , Consts .UTF_8 );
345
+ URI newUri = new URI (oldUri .getScheme (), oldUri .getAuthority (), oldUri .getPath (), query ,
346
+ oldUri .getFragment ());
347
+ return newUri ;
342
348
}
343
349
344
350
/**
0 commit comments