Skip to content

Commit 53f9e5c

Browse files
committed
fixing httpclienttest for the GET method
1 parent 560ba22 commit 53f9e5c

4 files changed

Lines changed: 53 additions & 50 deletions

File tree

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@
4545

4646
</dependencies>
4747

48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<version>3.3</version>
54+
<configuration>
55+
<source>1.7</source>
56+
<target>1.7</target>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
4862
<distributionManagement>
4963
<repository>
5064
<id>release</id>

src/main/java/httpClient/impl/HttpClientImpl.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import exception.*;
44
import httpClient.HttpClient;
5-
import org.apache.http.Consts;
6-
import org.apache.http.HttpEntity;
7-
import org.apache.http.HttpStatus;
8-
import org.apache.http.NameValuePair;
5+
import org.apache.http.*;
96
import org.apache.http.client.entity.UrlEncodedFormEntity;
107
import org.apache.http.client.methods.CloseableHttpResponse;
118
import org.apache.http.client.methods.HttpGet;
@@ -34,55 +31,63 @@ public HttpClientImpl() {
3431
closeableHttpClient = HttpClients.createDefault();
3532
}
3633

37-
public HttpEntity getResponse(HttpRequestBase httpRequest) throws IndixApiException, IOException {
34+
private CloseableHttpResponse getResponse(HttpRequestBase httpRequest) throws IndixApiException, IOException {
3835

3936
CloseableHttpResponse response = closeableHttpClient.execute(httpRequest);
40-
System.out.print(response);
41-
try {
42-
String message = response.getStatusLine().getReasonPhrase();
43-
int status = response.getStatusLine().getStatusCode();
44-
if (HttpStatus.SC_OK != status) {
45-
switch (status) {
46-
case HttpStatus.SC_BAD_REQUEST:
47-
throw new BadRequestException(message);
48-
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
49-
throw new InternalServerException(message);
50-
case HttpStatus.SC_UNAUTHORIZED:
51-
throw new UnauthorizedException(message);
52-
case HttpStatus.SC_PAYMENT_REQUIRED:
53-
throw new PaymentRequiredException(message);
54-
case 429: // too many requests - rate limited error
55-
throw new TooManyRequestsException(message);
56-
default:
57-
throw new IndixApiException(status, message);
58-
}
59-
}
6037

61-
return response.getEntity();
38+
String message = response.getStatusLine().getReasonPhrase();
39+
int status = response.getStatusLine().getStatusCode();
40+
41+
if (HttpStatus.SC_OK != status) {
6242

63-
} finally {
43+
// we need to close the resources before we throw an exception
44+
//
6445
response.close();
46+
47+
switch (status) {
48+
case HttpStatus.SC_BAD_REQUEST:
49+
throw new BadRequestException(message);
50+
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
51+
throw new InternalServerException(message);
52+
case HttpStatus.SC_UNAUTHORIZED:
53+
throw new UnauthorizedException(message);
54+
case HttpStatus.SC_PAYMENT_REQUIRED:
55+
throw new PaymentRequiredException(message);
56+
case 429: // too many requests - rate limited error
57+
throw new TooManyRequestsException(message);
58+
default:
59+
throw new IndixApiException(status, message);
60+
}
6561
}
62+
63+
return response;
6664
}
6765

6866
public String GET(URI uri) throws IOException, IndixApiException {
6967

7068
HttpGet httpGet = new HttpGet(uri);
71-
return EntityUtils.toString(getResponse(httpGet));
69+
70+
try(CloseableHttpResponse response = getResponse(httpGet)) {
71+
return EntityUtils.toString(response.getEntity());
72+
}
7273
}
7374

7475
public InputStream GETStream(URI uri) throws IOException, IndixApiException {
7576

7677
HttpGet httpGet = new HttpGet(uri);
77-
HttpEntity httpEntity = getResponse(httpGet);
78-
return httpEntity.getContent();
78+
79+
try(CloseableHttpResponse response = getResponse(httpGet)) {
80+
return response.getEntity().getContent();
81+
}
7982
}
8083

8184
public String POST(URI uri, List<NameValuePair> params) throws IndixApiException, IOException {
8285

8386
HttpPost httpPost = new HttpPost(uri);
8487
httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
85-
return EntityUtils.toString(getResponse(httpPost));
88+
try(CloseableHttpResponse response = getResponse(httpPost)) {
89+
return EntityUtils.toString(response.getEntity());
90+
}
8691
}
8792

8893
public String POST(URI uri, List<NameValuePair> params, File file) throws IOException, IndixApiException {
@@ -105,7 +110,9 @@ public String POST(URI uri, List<NameValuePair> params, File file) throws IOExce
105110

106111
// process request
107112
//
108-
return EntityUtils.toString(getResponse(httpPost));
113+
try(CloseableHttpResponse response = getResponse(httpPost)) {
114+
return EntityUtils.toString(response.getEntity());
115+
}
109116
}
110117

111118
public void close() throws IOException {

src/test/java/httpClient/HttpClientTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22

33
import exception.IndixApiException;
44
import httpClient.impl.HttpClientFactory;
5-
import org.apache.http.NameValuePair;
65
import org.apache.http.client.utils.URIBuilder;
7-
import org.apache.http.message.BasicNameValuePair;
86
import org.junit.Test;
97

10-
import java.io.File;
11-
import java.io.FileNotFoundException;
128
import java.io.IOException;
139
import java.net.URI;
1410
import java.net.URISyntaxException;
15-
import java.util.ArrayList;
16-
import java.util.List;
1711

1812
public class HttpClientTest {
1913

@@ -24,15 +18,4 @@ public void httpClientGetTest() throws URISyntaxException, IOException, IndixApi
2418
String content = httpClient.GET(uri);
2519
System.out.println(content.length());
2620
}
27-
28-
@Test(expected = FileNotFoundException.class)
29-
public void httpClientPostFileTest() throws URISyntaxException, IOException, IndixApiException {
30-
HttpClient httpClient = HttpClientFactory.newHttpClient();
31-
URI uri = new URIBuilder("http://www.example.com").build();
32-
List<NameValuePair> expectedValue = new ArrayList<NameValuePair>();
33-
expectedValue.add(new BasicNameValuePair("app_id", "123"));
34-
String content = httpClient.POST(uri,expectedValue,new File("lookup.jsonl"));
35-
System.out.println(content.length());
36-
}
37-
3821
}

src/test/java/query/BulkQueryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public void testBasicBulkQuery() {
6565
@Test
6666
public void testBasicBulkLookupQuery() throws IOException {
6767

68-
System.out.println();
6968
File file = ResourceUtils.getTestFile(getClass().getClassLoader(), "bulkQuery-json-responses0/bulkLookupInput.jsonl");
7069
FileInputStream inputStream = new FileInputStream(file);
7170
StringBuilder builder = new StringBuilder();

0 commit comments

Comments
 (0)