Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to httpclient5 #13

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ You can download the artifact manually from [Sonatype Nexus](https://oss.sonatyp

The library itself has the following dependencies:
- Apache Commons IO 2.11.0
- Apache Http Client 4.5.13
- Apache Http Client 5.1.3
- Jackson 2.13.3
- Slf4j (use an appropriate Logger implementation)

Expand Down Expand Up @@ -135,6 +135,7 @@ The result data is in the `GetNewsForApp` POJO. You can access it using the acce

## History

- Version 1.4: Upgrade to httpclient5 and change Java version to 8 for broader compatibility
- Version 1.3: Upgraded dependencies and bump Java version to 11
- Version 1.2: Bugfix Release. Fixed #3, updated dependencies and fixed non-compiling code in docs.
- Version 1.1: Minor bugfix release. Fixed #1 and updated dependencies.
Expand Down
22 changes: 9 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@
<jsonschema2pojo-maven-plugin.version>1.1.2</jsonschema2pojo-maven-plugin.version>

<!-- Dependencies versions -->
<javax.annotation.version>1.3.2</javax.annotation.version>
<commons-io.version>2.11.0</commons-io.version>
<slf4j.version>1.7.36</slf4j.version>
<jackson.version>2.13.3</jackson.version>
<httpclient.version>4.5.13</httpclient.version>
<httpclient.version>5.1.3</httpclient.version>
<cobertura.version>2.1.1</cobertura.version>
<jaxb-api.version>2.3.1</jaxb-api.version>

Expand Down Expand Up @@ -82,8 +81,8 @@
<version>${maven-compiler-plugin.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>11</source>
<target>11</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -98,9 +97,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<!-- for Java 9+ (replace "-@-" with two hyphen, because it is not allowed in comments)
<configuration>
<argLine>${argLine} --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
<argLine>${argLine} -@-add-opens java.base/java.lang=ALL-UNNAMED -@-add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
-->
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -140,6 +141,7 @@
<sourceType>jsonschema</sourceType>
<outputEncoding>${project.build.sourceEncoding}</outputEncoding>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<includeGeneratedAnnotation>false</includeGeneratedAnnotation>
<annotationStyle>jackson2</annotationStyle>
<generateBuilders>true</generateBuilders>
<initializeCollections>true</initializeCollections>
Expand Down Expand Up @@ -450,12 +452,6 @@
</profiles>

<dependencies>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand All @@ -472,8 +468,8 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.net.URIBuilder;

import com.lukaspradel.steamapi.core.SteamApiRequestHandler;
import com.lukaspradel.steamapi.core.exception.SteamApiException;
Expand Down Expand Up @@ -63,14 +63,11 @@ String getRequestPath(SteamWebApiRequest request) {

List<NameValuePair> getRequestParameters(Map<String, String> parametersMap) {

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
List<NameValuePair> nvps = new ArrayList<>();

nvps.add(new BasicNameValuePair("key", getKey()));

for (Map.Entry<String, String> param : parametersMap.entrySet()) {

nvps.add(new BasicNameValuePair(param.getKey(), param.getValue()));
}
parametersMap.entrySet().forEach(param -> nvps.add(new BasicNameValuePair(param.getKey(), param.getValue())));

return nvps;
}
Expand All @@ -94,28 +91,25 @@ String getWebApiResponse(URI requestUrl) throws SteamApiException {
HttpClient client = getHttpClient();
HttpGet getRequest = new HttpGet(requestUrl);

try {
HttpResponse response = client.execute(getRequest);

Integer statusCode = response.getStatusLine().getStatusCode();
// this try-with-resources statement closes the connection
// no need to close the client (ref https://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html)
try (ClassicHttpResponse response = client.execute(null, getRequest)) {
Integer statusCode = response.getCode();

if (!statusCode.toString().startsWith("20")) {
if (statusCode.equals(HttpStatus.SC_UNAUTHORIZED)) {
throw new SteamApiException(
SteamApiException.Cause.FORBIDDEN, statusCode,
response.getStatusLine().getReasonPhrase());
response.getReasonPhrase());
}
throw new SteamApiException(SteamApiException.Cause.HTTP_ERROR,
statusCode, response.getStatusLine().getReasonPhrase());
statusCode, response.getReasonPhrase());
}

return getHttpResponseAsString(response);
} catch (IOException e) {
} catch (IOException | ParseException e) {
throw new SteamApiException(
"The Web API request failed due to the following error: "
+ e.getMessage(), e);
} finally {
getRequest.releaseConnection();
}
}

Expand All @@ -124,7 +118,7 @@ HttpClient getHttpClient() {
return HttpClientBuilder.create().build();
}

String getHttpResponseAsString(HttpResponse response)
String getHttpResponseAsString(ClassicHttpResponse response)
throws ParseException, IOException {

return EntityUtils.toString(response.getEntity());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lukaspradel.steamapi.webapi.request;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -16,15 +17,15 @@
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicNameValuePair;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
Expand Down Expand Up @@ -52,10 +53,7 @@ public class SteamWebApiRequestHandlerTest extends BaseTest {
private HttpClient httpClientMock;

@Mock
private HttpResponse httpResponseMock;

@Mock
private StatusLine statusLineMock;
private ClassicHttpResponse httpResponseMock;

@Mock
private HttpEntity httpEntityMock;
Expand Down Expand Up @@ -152,10 +150,9 @@ public void testGetRequestUri() throws SteamApiException {
public void testGetWebApiResponseUnauthorized()
throws ClientProtocolException, IOException, SteamApiException {

when(statusLineMock.getStatusCode()).thenReturn(
when(httpResponseMock.getCode()).thenReturn(
HttpStatus.SC_UNAUTHORIZED);
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(
when(httpClientMock.execute(isNull(), any(HttpUriRequest.class))).thenReturn(
httpResponseMock);
when(requestHandlerHttpsSpy.getHttpClient()).thenReturn(httpClientMock);

Expand All @@ -171,10 +168,9 @@ public void testGetWebApiResponseErrorCode()
throws ClientProtocolException, IOException, SteamApiException,
URISyntaxException {

when(statusLineMock.getStatusCode()).thenReturn(
when(httpResponseMock.getCode()).thenReturn(
HttpStatus.SC_INTERNAL_SERVER_ERROR);
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(
when(httpClientMock.execute(isNull(), any(HttpUriRequest.class))).thenReturn(
httpResponseMock);
when(requestHandlerHttpsSpy.getHttpClient()).thenReturn(httpClientMock);

Expand All @@ -189,7 +185,7 @@ public void testGetWebApiResponseErrorCode()
public void testGetWebApiResponseIOException()
throws ClientProtocolException, IOException, SteamApiException {

when(httpClientMock.execute(any(HttpUriRequest.class))).thenThrow(
when(httpClientMock.execute(isNull(), any(HttpUriRequest.class))).thenThrow(
new IOException("intended-io-exception"));
when(requestHandlerHttpsSpy.getHttpClient()).thenReturn(httpClientMock);

Expand All @@ -202,7 +198,7 @@ public void testGetWebApiResponseIOException()

@Test
public void testGetWebApiResponse() throws ClientProtocolException,
IOException, SteamApiException {
IOException, SteamApiException, ParseException {

when(requestMock.getBaseUrl()).thenReturn("api.steampowered.com");
when(requestMock.getApiInterface()).thenReturn(
Expand All @@ -212,10 +208,9 @@ public void testGetWebApiResponse() throws ClientProtocolException,
when(requestMock.getVersion()).thenReturn(
SteamWebApiVersion.VERSION_TWO);

when(statusLineMock.getStatusCode()).thenReturn(HttpStatus.SC_OK);
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
when(httpResponseMock.getCode()).thenReturn(HttpStatus.SC_OK);
when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(
when(httpClientMock.execute(isNull(), any(HttpUriRequest.class))).thenReturn(
httpResponseMock);

when(requestHandlerHttpsSpy.getHttpClient()).thenReturn(httpClientMock);
Expand Down