Skip to content

Commit 9843837

Browse files
Allow null body in response entity and persistent client true or false (#15)
* Allow null body in response entity * HttpRequest can be set to use a persistent/singleton client or a new one
1 parent b63fbf8 commit 9843837

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/main/java/com/ericsson/eiffelcommons/utils/HttpExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class HttpExecutor {
3030
private CloseableHttpClient client = HttpClientBuilder.create()
3131
.build();
3232

33-
private HttpExecutor() {
33+
public HttpExecutor() {
3434

3535
}
3636

src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class HttpRequest {
4242

4343
private HttpRequestBase request;
44-
private HttpExecutor executor = HttpExecutor.getInstance();
44+
private HttpExecutor executor;
4545

4646
public enum HttpMethod {
4747
GET, POST, DELETE, PUT
@@ -57,6 +57,10 @@ public enum HttpMethod {
5757
protected Map<String, String> params;
5858

5959
public HttpRequest(HttpMethod method) {
60+
this(method, false);
61+
}
62+
63+
public HttpRequest(HttpMethod method, boolean persistentClient) {
6064
params = new HashMap<>();
6165

6266
switch (method) {
@@ -73,6 +77,16 @@ public HttpRequest(HttpMethod method) {
7377
request = new HttpPut();
7478
break;
7579
}
80+
81+
initExecutor(persistentClient);
82+
}
83+
84+
private void initExecutor(boolean persistentClient) {
85+
if(persistentClient) {
86+
executor = HttpExecutor.getInstance();
87+
} else {
88+
executor = new HttpExecutor();
89+
}
7690
}
7791

7892
/**

src/main/java/com/ericsson/eiffelcommons/utils/ResponseEntity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public class ResponseEntity {
3030

3131
private int statusCode;
32-
private String body;
32+
private String body = "";
3333
private Header[] headers;
3434

3535
/**
@@ -42,7 +42,9 @@ public class ResponseEntity {
4242
public ResponseEntity(HttpResponse httpResponse) throws ParseException, IOException {
4343
this.statusCode = httpResponse.getStatusLine()
4444
.getStatusCode();
45-
this.body = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
45+
if(httpResponse.getEntity() != null) {
46+
this.body = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
47+
}
4648
this.headers = httpResponse.getAllHeaders();
4749
}
4850

0 commit comments

Comments
 (0)