Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

import com.ericsson.eiffelcommons.helpers.MediaType;
import com.ericsson.eiffelcommons.http.HttpRequest;
import com.ericsson.eiffelcommons.http.HttpRequest.HttpMethod;
import com.ericsson.eiffelcommons.http.ResponseEntity;
import com.ericsson.eiffelcommons.http.HttpRequest.HttpMethod;

public class JenkinsManager {

Expand Down
70 changes: 38 additions & 32 deletions src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,32 @@
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.util.HashMap;
import java.net.URL;
import java.util.ArrayList;
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;
import org.apache.http.client.methods.HttpGet;
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.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
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;
Expand All @@ -62,10 +67,10 @@ public enum HttpMethod {
protected String endpoint;

@Getter
protected Map<String, String> params;
protected ArrayList<NameValuePair> params;

public HttpRequest() {
params = new HashMap<>();
params = new ArrayList<NameValuePair>();
initExecutor(false);
}

Expand All @@ -79,7 +84,7 @@ public HttpRequest(HttpMethod method, HttpExecutor executor) {
}

public HttpRequest(HttpMethod method, boolean persistentClient) {
params = new HashMap<>();
params = new ArrayList<NameValuePair>();
setHttpMethod(method);
initExecutor(persistentClient);
}
Expand Down Expand Up @@ -205,7 +210,7 @@ public void addParameters(Map<String, String> parameters) {
* @return HttpRequest
*/
public HttpRequest addParameter(String key, String value) {
params.put(key, value);
params.add(new BasicNameValuePair(key, value));
return this;
}

Expand Down Expand Up @@ -291,9 +296,9 @@ public HttpRequest setBasicAuth(String username, String password)
*/
public ResponseEntity performRequest()
throws URISyntaxException, ClientProtocolException, IOException {
URIBuilder builder = createURIBuilder();
builder = addParametersToURIBuilder(builder);
request.setURI(builder.build());
URI uri = createURI();
uri = addParametersToURI(uri);
request.setURI(uri);
return executor.executeRequest(request);
}

Expand All @@ -302,43 +307,44 @@ public ResponseEntity performRequest()
*
* @return URI
* @throws URISyntaxException
* @throws MalformedURLException
*/
public URI getURI() throws URISyntaxException {
URIBuilder builder = createURIBuilder();
return builder.build();
@Deprecated
public URI getURI() throws MalformedURLException, URISyntaxException {
return createURI();
}

/**
* Function that adds parameters to the URIBuilder
* Function that creates the URI from the baseUrl and endpoint
*
* @param URIBuilder
* @return URIBuilder
* @param
* @return URI
* @throws MalformedURLException
* @throws URISyntaxException
*/
private URIBuilder addParametersToURIBuilder(URIBuilder builder) {
if (!params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
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();
}

return builder;
}

/**
* Function that creates the URI from the baseUrl and endpoint
* Function that adds parameters to the URI
*
* @param
* @return URIBuilder
* @param oldUri
* @return URI
* @throws URISyntaxException
*/
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);
}
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand All @@ -21,7 +22,6 @@
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;
Expand Down Expand Up @@ -56,29 +56,28 @@ public class HttpRequestTest {

@Test
public void testBuildingOfURI() throws Exception {

HttpRequest request = new HttpRequest(HttpMethod.POST);

request.setBaseUrl(URL_1);
request.setEndpoint(ENDPOINT_1);
URIBuilder builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());

request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_2);
request.setEndpoint(ENDPOINT_1);
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());

request = new HttpRequest(HttpMethod.DELETE);
request.setBaseUrl(URL_2);
request.setEndpoint(ENDPOINT_2);
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());

request = new HttpRequest(HttpMethod.PUT);
request.setBaseUrl(URL_1);
request.setEndpoint(ENDPOINT_2);
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());
}

@Test
Expand Down Expand Up @@ -186,6 +185,18 @@ public void testParameterProperty() {
}

@Test
public void testAddParametersToURI() throws Exception {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_1)
.addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1)
.addParameter(PARAMETER_KEY_2, PARAMETER_VALUE_2);
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
URI newUri = (URI) Whitebox.invokeMethod(request, "addParametersToURI", uri);
String expectedURI = URL_1 + "?" + PARAMETER_KEY_1 + "=" + PARAMETER_VALUE_1 + "&"
+ PARAMETER_KEY_2 + "=" + PARAMETER_VALUE_2;
assertEquals(expectedURI, newUri.toString());
}

public void testHeaderProperty() {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.addHeader(HEADER_KEY_1, HEADER_VALUE_1);
Expand All @@ -206,10 +217,10 @@ public void testHeaderProperty() {
}

@Test
public void testGetURI() throws URISyntaxException {
public void testGetURI() throws MalformedURLException, URISyntaxException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_1).setEndpoint(ENDPOINT_1);
URI uri = request.getURI();
URI uri = request.createURI();
String fullURI = uri.toString();
assertEquals(URL_1 + ENDPOINT_1, fullURI);
}
Expand All @@ -222,9 +233,8 @@ public void testPerformRequestUnknownHost()
request.performRequest();
}

@Test(expected = ClientProtocolException.class)
public void testPerformRequestBadProtocol()
throws ClientProtocolException, URISyntaxException, IOException {
@Test(expected = MalformedURLException.class)
public void testPerformRequestBadProtocol() throws ClientProtocolException, URISyntaxException, IOException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_BAD_PROTOCOL);
request.performRequest();
Expand Down