Skip to content

Commit 1f1f638

Browse files
authored
Merge pull request #18 from sashirestela/17-issue-handling-errors-when-response-is-inputstream
Issue handling errors when response is inputstream
2 parents 0eb33d9 + 4d366a5 commit 1f1f638

15 files changed

+45
-16
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sashirestela</groupId>
88
<artifactId>cleverclient</artifactId>
9-
<version>0.8.0</version>
9+
<version>0.8.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>cleverclient</name>

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncBinarySender.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.sashirestela.cleverclient.sender;
22

3+
import java.io.InputStream;
34
import java.net.http.HttpClient;
45
import java.net.http.HttpRequest;
56
import java.net.http.HttpResponse.BodyHandlers;
@@ -15,7 +16,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1516

1617
return httpResponseFuture.thenApply(response -> {
1718

18-
throwExceptionIfErrorIsPresent(response, false);
19+
throwExceptionIfErrorIsPresent(response, InputStream.class);
1920

2021
logger.debug("Response : {}", response.body());
2122

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncGenericSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1616

1717
return httpResponseFuture.thenApply(response -> {
1818

19-
throwExceptionIfErrorIsPresent(response, false);
19+
throwExceptionIfErrorIsPresent(response, null);
2020

2121
logger.debug("Response : {}", response.body());
2222

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncListSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1616

1717
return httpResponseFuture.thenApply(response -> {
1818

19-
throwExceptionIfErrorIsPresent(response, false);
19+
throwExceptionIfErrorIsPresent(response, null);
2020

2121
logger.debug("Response : {}", response.body());
2222

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncObjectSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1616

1717
return httpResponseFuture.thenApply(response -> {
1818

19-
throwExceptionIfErrorIsPresent(response, false);
19+
throwExceptionIfErrorIsPresent(response, null);
2020

2121
logger.debug("Response : {}", response.body());
2222

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncPlainTextSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1515

1616
return httpResponseFuture.thenApply(response -> {
1717

18-
throwExceptionIfErrorIsPresent(response, false);
18+
throwExceptionIfErrorIsPresent(response, null);
1919

2020
logger.debug("Response : {}", response.body());
2121

src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.net.http.HttpClient;
44
import java.net.http.HttpRequest;
55
import java.net.http.HttpResponse.BodyHandlers;
6+
import java.util.stream.Stream;
67

78
import io.github.sashirestela.cleverclient.support.CleverClientSSE;
89
import io.github.sashirestela.cleverclient.util.JsonUtil;
@@ -17,7 +18,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1718

1819
return httpResponseFuture.thenApply(response -> {
1920

20-
throwExceptionIfErrorIsPresent(response, true);
21+
throwExceptionIfErrorIsPresent(response, Stream.class);
2122

2223
return response.body()
2324
.peek(rawData -> logger.debug("Response : {}", rawData))

src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.sashirestela.cleverclient.sender;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
35
import java.net.HttpURLConnection;
46
import java.net.http.HttpClient;
57
import java.net.http.HttpRequest;
68
import java.net.http.HttpResponse;
9+
import java.nio.charset.StandardCharsets;
710
import java.util.stream.Collectors;
811
import java.util.stream.Stream;
912

@@ -20,13 +23,19 @@ public abstract <S, T> Object sendRequest(HttpClient httpClient, HttpRequest htt
2023
Class<S> genericClass);
2124

2225
@SuppressWarnings("unchecked")
23-
protected void throwExceptionIfErrorIsPresent(HttpResponse<?> response, boolean isStream) {
26+
protected void throwExceptionIfErrorIsPresent(HttpResponse<?> response, Class<?> clazz) {
2427
logger.debug("Response Code : {}", response.statusCode());
2528
if (!CommonUtil.isInHundredsOf(response.statusCode(), HttpURLConnection.HTTP_OK)) {
2629
var data = "";
27-
if (isStream) {
30+
if (Stream.class.equals(clazz)) {
2831
data = ((Stream<String>) response.body())
2932
.collect(Collectors.joining(System.getProperty("line.separator")));
33+
} else if (InputStream.class.equals(clazz)) {
34+
try {
35+
data = new String(((InputStream) response.body()).readAllBytes(), StandardCharsets.UTF_8);
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
3039
} else {
3140
data = (String) response.body();
3241
}

src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncBinarySender.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.sashirestela.cleverclient.sender;
22

33
import java.io.IOException;
4+
import java.io.InputStream;
45
import java.net.http.HttpClient;
56
import java.net.http.HttpRequest;
67
import java.net.http.HttpResponse.BodyHandlers;
@@ -16,7 +17,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1617

1718
var httpResponse = httpClient.send(httpRequest, BodyHandlers.ofInputStream());
1819

19-
throwExceptionIfErrorIsPresent(httpResponse, false);
20+
throwExceptionIfErrorIsPresent(httpResponse, InputStream.class);
2021

2122
var rawData = httpResponse.body();
2223

src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncGenericSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public <S, T> Object sendRequest(HttpClient httpClient, HttpRequest httpRequest,
1717

1818
var httpResponse = httpClient.send(httpRequest, BodyHandlers.ofString());
1919

20-
throwExceptionIfErrorIsPresent(httpResponse, false);
20+
throwExceptionIfErrorIsPresent(httpResponse, null);
2121

2222
var rawData = httpResponse.body();
2323

0 commit comments

Comments
 (0)