From c61bf8252d8a079859ba45169743c91dc1b26dab Mon Sep 17 00:00:00 2001 From: Jainad Chinta Date: Wed, 30 Apr 2025 01:58:01 +0530 Subject: [PATCH] Release 1.0.5 version --- pom.xml | 2 +- .../eiffelcommons/http/HttpRequest.java | 82 +++++++++---------- .../eiffelcommons/http/HttpRequestTest.java | 56 ++++++------- 3 files changed, 62 insertions(+), 78 deletions(-) diff --git a/pom.xml b/pom.xml index 229c387..9a81a10 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.github.ericsson eiffel-commons - 1.0.4 + 1.0.5 eiffel commons java A shared library for eiffel components diff --git a/src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java b/src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java index f8b8197..9fd9de1 100644 --- a/src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java +++ b/src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java @@ -19,20 +19,16 @@ import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; -import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.http.Consts; import org.apache.http.Header; import org.apache.http.HttpHeaders; -import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; @@ -40,11 +36,10 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; -import org.apache.http.message.BasicNameValuePair; import lombok.Getter; import lombok.Setter; @@ -67,10 +62,10 @@ public enum HttpMethod { protected String endpoint; @Getter - protected ArrayList params; + protected Map params; public HttpRequest() { - params = new ArrayList(); + params = new HashMap<>(); initExecutor(false); } @@ -84,7 +79,7 @@ public HttpRequest(HttpMethod method, HttpExecutor executor) { } public HttpRequest(HttpMethod method, boolean persistentClient) { - params = new ArrayList(); + params = new HashMap<>(); setHttpMethod(method); initExecutor(persistentClient); } @@ -136,7 +131,8 @@ public String getBaseUrl() { * @param baseUrl */ public HttpRequest setBaseUrl(String baseUrl) { - this.baseUrl = trimBaseUrl(baseUrl); + baseUrl = trimBaseUrl(baseUrl); + this.baseUrl = baseUrl; return this; } @@ -209,7 +205,7 @@ public void addParameters(Map parameters) { * @return HttpRequest */ public HttpRequest addParameter(String key, String value) { - params.add(new BasicNameValuePair(key, value)); + params.put(key, value); return this; } @@ -295,9 +291,9 @@ public HttpRequest setBasicAuth(String username, String password) */ public ResponseEntity performRequest() throws URISyntaxException, ClientProtocolException, IOException { - URI uri = createURI(); - uri = addParametersToURI(uri); - request.setURI(uri); + URIBuilder builder = createURIBuilder(); + builder = addParametersToURIBuilder(builder); + request.setURI(builder.build()); return executor.executeRequest(request); } @@ -306,57 +302,55 @@ public ResponseEntity performRequest() * * @return URI * @throws URISyntaxException - * @throws MalformedURLException */ - @Deprecated - public URI getURI() throws MalformedURLException, URISyntaxException { - return createURI(); + public URI getURI() throws URISyntaxException { + URIBuilder builder = createURIBuilder(); + return builder.build(); } /** - * Function that creates the URI from the baseUrl and endpoint + * Function that adds parameters to the URIBuilder * - * @param - * @return URI - * @throws MalformedURLException - * @throws URISyntaxException + * @param URIBuilder + * @return URIBuilder */ - public URI createURI() throws MalformedURLException, URISyntaxException { - if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) { - return new URL(baseUrl + endpoint).toURI(); - } else if (!StringUtils.isEmpty(endpoint)) { - return new URL(baseUrl + "/" + endpoint).toURI(); - } else { - return new URL(baseUrl).toURI(); + private URIBuilder addParametersToURIBuilder(URIBuilder builder) { + if (!params.isEmpty()) { + for (Map.Entry entry : params.entrySet()) { + builder.addParameter(entry.getKey(), entry.getValue()); + } } + return builder; } /** - * Function that adds parameters to the URI + * Function that creates the URI from the baseUrl and endpoint * - * @param oldUri - * @return URI + * @param + * @return URIBuilder * @throws URISyntaxException */ - private URI addParametersToURI(URI oldUri) throws URISyntaxException { - String query = URLEncodedUtils.format(params, Consts.UTF_8); - URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), query, - oldUri.getFragment()); - return newUri; + private URIBuilder createURIBuilder() throws URISyntaxException { + if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) { + return new URIBuilder(baseUrl + endpoint); + } else if (!StringUtils.isEmpty(endpoint)) { + return new URIBuilder(baseUrl + "/" + endpoint); + } else { + return new URIBuilder(baseUrl); + } } /** * Function that trims the base url for trailing slashes * - * @return trimmedUrl + * @return HttpRequest */ private String trimBaseUrl(String baseUrl) { - String trimmedUrl = baseUrl; if (baseUrl.endsWith("/")) { - trimmedUrl = baseUrl.substring(0, baseUrl.length() - 1); + baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } - return trimmedUrl; + return baseUrl; } -} \ No newline at end of file +} diff --git a/src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java b/src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java index 0800769..b83d67c 100644 --- a/src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java +++ b/src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java @@ -1,4 +1,4 @@ -package com.ericsson.eiffelcommons.http; +package com.ericsson.eiffelcommons.httptest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -9,7 +9,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; @@ -22,6 +21,7 @@ import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.junit.Test; import org.powermock.reflect.Whitebox; @@ -33,7 +33,6 @@ public class HttpRequestTest { private static final String URL_1 = "http://something.com"; private static final String URL_2 = "http://something.com/"; - private static final String URL_3 = "http://someothing.com"; private static final String URL_BAD_PROTOCOL = "httpl://something.com/"; private static final String URL_BAD_SYNTAX = "http:<