Skip to content

Commit 63be5e6

Browse files
committed
Remove usage of throwOnError body handler
1 parent 362276c commit 63be5e6

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

jena/src/main/java/com/inrupt/client/jena/JenaBodyHandlers.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
*/
2121
package com.inrupt.client.jena;
2222

23+
import com.inrupt.client.ClientHttpException;
2324
import com.inrupt.client.Response;
2425

2526
import java.io.ByteArrayInputStream;
2627
import java.io.IOException;
2728
import java.io.UncheckedIOException;
29+
import java.nio.charset.StandardCharsets;
2830

2931
import org.apache.jena.atlas.web.ContentType;
3032
import org.apache.jena.graph.Graph;
@@ -44,6 +46,18 @@ public final class JenaBodyHandlers {
4446

4547
private static final String CONTENT_TYPE = "Content-Type";
4648

49+
private static void throwOnError(final Response.ResponseInfo responseInfo) {
50+
if (!Response.isSuccess(responseInfo.statusCode())) {
51+
throw new ClientHttpException(
52+
"Could not map to a Jena entity.",
53+
responseInfo.uri(),
54+
responseInfo.statusCode(),
55+
responseInfo.headers(),
56+
new String(responseInfo.body().array(), StandardCharsets.UTF_8)
57+
);
58+
}
59+
}
60+
4761
private static Model responseToModel(final Response.ResponseInfo responseInfo) {
4862
return responseInfo.headers().firstValue(CONTENT_TYPE)
4963
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
@@ -75,10 +89,10 @@ public static Response.BodyHandler<Model> ofModel() {
7589
* @return an HTTP body handler
7690
*/
7791
public static Response.BodyHandler<Model> ofJenaModel() {
78-
return Response.BodyHandlers.throwOnError(
79-
JenaBodyHandlers::responseToModel,
80-
(r) -> Response.isSuccess(r.statusCode())
81-
);
92+
return responseInfo -> {
93+
JenaBodyHandlers.throwOnError(responseInfo);
94+
return JenaBodyHandlers.responseToModel(responseInfo);
95+
};
8296
}
8397

8498
private static Graph responseToGraph(final Response.ResponseInfo responseInfo) {
@@ -112,10 +126,10 @@ public static Response.BodyHandler<Graph> ofGraph() {
112126
* @return an HTTP body handler
113127
*/
114128
public static Response.BodyHandler<Graph> ofJenaGraph() {
115-
return Response.BodyHandlers.throwOnError(
116-
JenaBodyHandlers::responseToGraph,
117-
(r) -> Response.isSuccess(r.statusCode())
118-
);
129+
return responseInfo -> {
130+
JenaBodyHandlers.throwOnError(responseInfo);
131+
return JenaBodyHandlers.responseToGraph(responseInfo);
132+
};
119133
}
120134

121135
private static Dataset responseToDataset(final Response.ResponseInfo responseInfo) {
@@ -149,10 +163,10 @@ public static Response.BodyHandler<Dataset> ofDataset() {
149163
* @return an HTTP body handler
150164
*/
151165
public static Response.BodyHandler<Dataset> ofJenaDataset() {
152-
return Response.BodyHandlers.throwOnError(
153-
JenaBodyHandlers::responseToDataset,
154-
(r) -> Response.isSuccess(r.statusCode())
155-
);
166+
return responseInfo -> {
167+
JenaBodyHandlers.throwOnError(responseInfo);
168+
return JenaBodyHandlers.responseToDataset(responseInfo);
169+
};
156170
}
157171

158172
static Lang toJenaLang(final String mediaType) {

0 commit comments

Comments
 (0)