Skip to content

Commit f80b972

Browse files
committed
factorize request URI extraction methods in HttpUtils
1 parent 1f1d902 commit f80b972

File tree

3 files changed

+175
-5
lines changed

3 files changed

+175
-5
lines changed

core/src/main/java/com/xatkit/core/server/HttpHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.apache.http.HttpResponse;
1515
import org.apache.http.HttpStatus;
1616
import org.apache.http.NameValuePair;
17-
import org.apache.http.client.utils.URIBuilder;
1817
import org.apache.http.protocol.HttpContext;
1918
import org.apache.http.protocol.HttpRequestHandler;
2019

@@ -112,9 +111,8 @@ public void handle(final HttpRequest request, final HttpResponse response, final
112111
List<NameValuePair> parameters = null;
113112
String path;
114113
try {
115-
URIBuilder uriBuilder = new URIBuilder(request.getRequestLine().getUri());
116-
path = uriBuilder.getPath();
117-
parameters = uriBuilder.getQueryParams();
114+
path = HttpUtils.getPath(request);
115+
parameters = HttpUtils.getParameters(request);
118116
} catch (URISyntaxException e) {
119117
throw new XatkitException(MessageFormat.format("Cannot parse the provided URI {0}, see attached exception",
120118
request.getRequestLine().getUri()), e);

core/src/main/java/com/xatkit/core/server/HttpUtils.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,69 @@
11
package com.xatkit.core.server;
22

3+
import org.apache.http.HttpRequest;
34
import org.apache.http.NameValuePair;
5+
import org.apache.http.client.utils.URIBuilder;
46

7+
import javax.annotation.Nonnull;
58
import javax.annotation.Nullable;
9+
import java.net.URISyntaxException;
610
import java.util.List;
711

12+
import static fr.inria.atlanmod.commons.Preconditions.checkNotNull;
13+
814
/**
915
* Provides utility methods to manipulate Http-related objects.
1016
*/
1117
public class HttpUtils {
1218

19+
/**
20+
* Returns a {@link URIBuilder} from the provided {@code httpRequest}.
21+
*
22+
* @param httpRequest the {@link HttpRequest} to get an {@link URIBuilder} from
23+
* @return the created {@link URIBuilder}
24+
* @throws URISyntaxException if the provided {@code httpRequest}'s URI is invalid
25+
*/
26+
public static URIBuilder getURIBuilderFrom(@Nonnull HttpRequest httpRequest) throws URISyntaxException {
27+
checkNotNull(httpRequest, "Cannot create an %s from the provided %s %s", URIBuilder.class.getSimpleName(),
28+
HttpRequest.class.getSimpleName(), httpRequest);
29+
return new URIBuilder(httpRequest.getRequestLine().getUri());
30+
}
31+
32+
/**
33+
* Returns the path associated to the provided {@code httpRequest}.
34+
* <p>
35+
* The returned path corresponds to the target of the provided {@code httpRequest} without the requests
36+
* parameters. For example, calling this method on a request with the URI {@code /target?param=value} will return
37+
* {@code /target}.
38+
*
39+
* @param httpRequest the {@link HttpRequest} to get the path from
40+
* @return the path associated to the provided {@code httpRequest}
41+
* @throws URISyntaxException if the provided {@code httpRequest}'s URI is invalid
42+
*/
43+
public static String getPath(@Nonnull HttpRequest httpRequest) throws URISyntaxException {
44+
checkNotNull(httpRequest, "Cannot retrieve the path from the provided %s %s", HttpRequest.class.getSimpleName()
45+
, httpRequest);
46+
URIBuilder uriBuilder = getURIBuilderFrom(httpRequest);
47+
return uriBuilder.getPath();
48+
}
49+
50+
/**
51+
* Returns a {@link List} containing the parameters associated to the provided {@code httpRequest}.
52+
* <p>
53+
* Calling this method on a {@code httpRequest} instance that does not define any parameter in its request URI
54+
* returns an empty {@link List}.
55+
*
56+
* @param httpRequest the {@link HttpRequest} to get the parameters from
57+
* @return a {@link List} containing the parameters associated to the provided {@code httpRequest}
58+
* @throws URISyntaxException if the provided {@code httpRequest}'s URI is invalid
59+
*/
60+
public static List<NameValuePair> getParameters(@Nonnull HttpRequest httpRequest) throws URISyntaxException {
61+
checkNotNull(httpRequest, "Cannot retrieve the parameters from the provided %s %s",
62+
HttpRequest.class.getSimpleName(), httpRequest);
63+
URIBuilder uriBuilder = getURIBuilderFrom(httpRequest);
64+
return uriBuilder.getQueryParams();
65+
}
66+
1367
/**
1468
* Returns the value associated to the provided {@code parameterName} from the given {@code parameters} list.
1569
*
@@ -18,7 +72,9 @@ public class HttpUtils {
1872
* @return the value associated to the provided {@code parameterName} from the given {@code parameters} list
1973
*/
2074
public static @Nullable
21-
String getParameterValue(String parameterName, List<NameValuePair> parameters) {
75+
String getParameterValue(@Nonnull String parameterName, @Nonnull List<NameValuePair> parameters) {
76+
checkNotNull(parameterName, "Cannot retrieve the value of parameter %s", parameterName);
77+
checkNotNull(parameters, "Cannot retrieve parameters from the provided list: %s", parameters);
2278
return parameters.stream().filter(p -> p.getName().equals(parameterName))
2379
.map(NameValuePair::getValue).findFirst().orElse(null);
2480
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.xatkit.core.server;
2+
3+
import com.xatkit.AbstractXatkitTest;
4+
import org.apache.http.HttpRequest;
5+
import org.apache.http.NameValuePair;
6+
import org.apache.http.client.utils.URIBuilder;
7+
import org.apache.http.message.BasicHttpRequest;
8+
import org.apache.http.message.BasicNameValuePair;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import java.net.URISyntaxException;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
public class HttpUtilsTest extends AbstractXatkitTest {
19+
20+
private static String VALID_HTTP_REQUEST_PATH = "/target";
21+
22+
private static HttpRequest VALID_HTTP_REQUEST;
23+
24+
private static HttpRequest INVALID_HTTP_REQUEST;
25+
26+
private static List<NameValuePair> TEST_PARAMETERS = Arrays.asList(new BasicNameValuePair("param1", "abc"),
27+
new BasicNameValuePair("param2", "def"));
28+
29+
@BeforeClass
30+
public static void setUpBeforeClass() {
31+
VALID_HTTP_REQUEST = new BasicHttpRequest("GET", VALID_HTTP_REQUEST_PATH);
32+
INVALID_HTTP_REQUEST = new BasicHttpRequest("GET", "i\nvalid");
33+
}
34+
35+
@Test(expected = NullPointerException.class)
36+
public void getURIBuilderFromNullRequest() throws URISyntaxException {
37+
HttpUtils.getURIBuilderFrom(null);
38+
}
39+
40+
@Test(expected = URISyntaxException.class)
41+
public void getURIBuilderFromInvalidURI() throws URISyntaxException {
42+
HttpUtils.getURIBuilderFrom(INVALID_HTTP_REQUEST);
43+
}
44+
45+
@Test
46+
public void getURIBuilderFromValidURI() throws URISyntaxException {
47+
URIBuilder uriBuilder = HttpUtils.getURIBuilderFrom(VALID_HTTP_REQUEST);
48+
assertThat(uriBuilder.getPath()).as("Builder contains the correct path").isEqualTo(VALID_HTTP_REQUEST_PATH);
49+
}
50+
51+
@Test(expected = NullPointerException.class)
52+
public void getPathNullRequest() throws URISyntaxException {
53+
HttpUtils.getPath(null);
54+
}
55+
56+
@Test(expected = URISyntaxException.class)
57+
public void getPathInvalidURI() throws URISyntaxException {
58+
HttpUtils.getPath(INVALID_HTTP_REQUEST);
59+
}
60+
61+
@Test
62+
public void getPathValidURI() throws URISyntaxException {
63+
String path = HttpUtils.getPath(VALID_HTTP_REQUEST);
64+
assertThat(path).as("Correct path").isEqualTo(VALID_HTTP_REQUEST_PATH);
65+
}
66+
67+
@Test(expected = NullPointerException.class)
68+
public void getParametersNullRequest() throws URISyntaxException {
69+
HttpUtils.getParameters(null);
70+
}
71+
72+
@Test(expected = URISyntaxException.class)
73+
public void getParametersInvalidURI() throws URISyntaxException {
74+
HttpUtils.getParameters(INVALID_HTTP_REQUEST);
75+
}
76+
77+
@Test
78+
public void getParametersValidURINoParameters() throws URISyntaxException {
79+
List<NameValuePair> parameters = HttpUtils.getParameters(VALID_HTTP_REQUEST);
80+
assertThat(parameters).as("Parameter list is empty").isEmpty();
81+
}
82+
83+
@Test
84+
public void getParametersValidURIWithParameters() throws URISyntaxException {
85+
List<NameValuePair> parameters = HttpUtils.getParameters(new BasicHttpRequest("GET",
86+
VALID_HTTP_REQUEST_PATH + "?param1=abc&param2=def"));
87+
assertThat(parameters).as("Parameters list contains two parameters").hasSize(2);
88+
assertThat(parameters.stream()).as("Parameter list contains param1").anyMatch(p -> p.getName().equals("param1"
89+
) && p.getValue().equals("abc"));
90+
assertThat(parameters.stream()).as("Parameter list contains param2").anyMatch(p -> p.getName().equals("param2"
91+
) && p.getValue().equals("def"));
92+
}
93+
94+
@Test(expected = NullPointerException.class)
95+
public void getParameterValueNullName() {
96+
HttpUtils.getParameterValue(null, TEST_PARAMETERS);
97+
}
98+
99+
@Test(expected = NullPointerException.class)
100+
public void getParameterValueNullParameters() {
101+
HttpUtils.getParameterValue("param1", null);
102+
}
103+
104+
@Test
105+
public void getParameterValueExistingParameter() {
106+
String value = HttpUtils.getParameterValue("param1", TEST_PARAMETERS);
107+
assertThat(value).as("Valid parameter value").isEqualTo("abc");
108+
}
109+
110+
@Test
111+
public void getParameterValueNotExistingParameter() {
112+
String value = HttpUtils.getParameterValue("invalid", TEST_PARAMETERS);
113+
assertThat(value).as("Parameter value is null").isNull();
114+
}
115+
116+
}

0 commit comments

Comments
 (0)