Skip to content

Commit 39299b6

Browse files
committed
Use throwing body handler
1 parent b23c1d6 commit 39299b6

File tree

1 file changed

+15
-64
lines changed

1 file changed

+15
-64
lines changed

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

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

23-
import com.inrupt.client.ClientHttpException;
24-
import com.inrupt.client.ProblemDetails;
2523
import com.inrupt.client.Response;
26-
import com.inrupt.client.spi.JsonService;
27-
import com.inrupt.client.spi.ServiceProvider;
2824

2925
import java.io.ByteArrayInputStream;
3026
import java.io.IOException;
@@ -47,24 +43,9 @@
4743
public final class JenaBodyHandlers {
4844

4945
private static final String CONTENT_TYPE = "Content-Type";
50-
private static JsonService jsonService;
51-
private static boolean isJsonServiceInitialized = false;
52-
53-
private static JsonService getJsonService() {
54-
if (JenaBodyHandlers.isJsonServiceInitialized) {
55-
return JenaBodyHandlers.jsonService;
56-
}
57-
// It is acceptable for a JenaBodyHandlers instance to be in a classpath without any implementation for
58-
// JsonService, in which case the ProblemDetails exceptions will fallback to default and not be parsed.
59-
JsonService js;
60-
try {
61-
js = ServiceProvider.getJsonService();
62-
} catch (IllegalStateException e) {
63-
js = null;
64-
}
65-
JenaBodyHandlers.jsonService = js;
66-
JenaBodyHandlers.isJsonServiceInitialized = true;
67-
return JenaBodyHandlers.jsonService;
46+
47+
private static Boolean isSuccess(final Response.ResponseInfo responseInfo) {
48+
return responseInfo.statusCode() < 300;
6849
}
6950

7051
private static Model responseToModel(final Response.ResponseInfo responseInfo) {
@@ -98,20 +79,10 @@ public static Response.BodyHandler<Model> ofModel() {
9879
* @return an HTTP body handler
9980
*/
10081
public static Response.BodyHandler<Model> ofJenaModel() {
101-
return responseInfo -> {
102-
if (responseInfo.statusCode() >= 300) {
103-
throw new ClientHttpException(
104-
ProblemDetails.fromErrorResponse(
105-
responseInfo.statusCode(),
106-
responseInfo.headers(),
107-
responseInfo.body().array(),
108-
getJsonService()
109-
),
110-
"Deserializing the RDF from " + responseInfo.uri() + " failed"
111-
);
112-
}
113-
return responseToModel(responseInfo);
114-
};
82+
return Response.BodyHandlers.throwOnError(
83+
JenaBodyHandlers::responseToModel,
84+
JenaBodyHandlers::isSuccess
85+
);
11586
}
11687

11788
private static Graph responseToGraph(final Response.ResponseInfo responseInfo) {
@@ -145,20 +116,10 @@ public static Response.BodyHandler<Graph> ofGraph() {
145116
* @return an HTTP body handler
146117
*/
147118
public static Response.BodyHandler<Graph> ofJenaGraph() {
148-
return responseInfo -> {
149-
if (responseInfo.statusCode() > 300) {
150-
throw new ClientHttpException(
151-
ProblemDetails.fromErrorResponse(
152-
responseInfo.statusCode(),
153-
responseInfo.headers(),
154-
responseInfo.body().array(),
155-
getJsonService()
156-
),
157-
"Deserializing the RDF from " + responseInfo.uri() + " failed"
158-
);
159-
}
160-
return JenaBodyHandlers.responseToGraph(responseInfo);
161-
};
119+
return Response.BodyHandlers.throwOnError(
120+
JenaBodyHandlers::responseToGraph,
121+
JenaBodyHandlers::isSuccess
122+
);
162123
}
163124

164125
private static Dataset responseToDataset(final Response.ResponseInfo responseInfo) {
@@ -192,20 +153,10 @@ public static Response.BodyHandler<Dataset> ofDataset() {
192153
* @return an HTTP body handler
193154
*/
194155
public static Response.BodyHandler<Dataset> ofJenaDataset() {
195-
return responseInfo -> {
196-
if (responseInfo.statusCode() > 300) {
197-
throw new ClientHttpException(
198-
ProblemDetails.fromErrorResponse(
199-
responseInfo.statusCode(),
200-
responseInfo.headers(),
201-
responseInfo.body().array(),
202-
getJsonService()
203-
),
204-
"Deserializing the RDF from " + responseInfo.uri() + " failed"
205-
);
206-
}
207-
return JenaBodyHandlers.responseToDataset(responseInfo);
208-
};
156+
return Response.BodyHandlers.throwOnError(
157+
JenaBodyHandlers::responseToDataset,
158+
JenaBodyHandlers::isSuccess
159+
);
209160
}
210161

211162
static Lang toJenaLang(final String mediaType) {

0 commit comments

Comments
 (0)