Skip to content

Commit

Permalink
Remove usage of throwOnError body handler
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Apr 23, 2024
1 parent 3383af2 commit b8e402f
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions jena/src/main/java/com/inrupt/client/jena/JenaBodyHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/
package com.inrupt.client.jena;

import com.inrupt.client.ClientHttpException;
import com.inrupt.client.Response;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;

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

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

private static void throwOnError(final Response.ResponseInfo responseInfo) {
if (!Response.isSuccess(responseInfo.statusCode())) {
throw new ClientHttpException(
"Could not map to a Jena entity.",
responseInfo.uri(),
responseInfo.statusCode(),
responseInfo.headers(),
new String(responseInfo.body().array(), StandardCharsets.UTF_8)
);
}
}

private static Model responseToModel(final Response.ResponseInfo responseInfo) {
return responseInfo.headers().firstValue(CONTENT_TYPE)
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
Expand Down Expand Up @@ -75,10 +89,10 @@ public static Response.BodyHandler<Model> ofModel() {
* @return an HTTP body handler
*/
public static Response.BodyHandler<Model> ofJenaModel() {
return Response.BodyHandlers.throwOnError(
JenaBodyHandlers::responseToModel,
(r) -> Response.isSuccess(r.statusCode())
);
return responseInfo -> {
JenaBodyHandlers.throwOnError(responseInfo);
return JenaBodyHandlers.responseToModel(responseInfo);
};
}

private static Graph responseToGraph(final Response.ResponseInfo responseInfo) {
Expand Down Expand Up @@ -112,10 +126,10 @@ public static Response.BodyHandler<Graph> ofGraph() {
* @return an HTTP body handler
*/
public static Response.BodyHandler<Graph> ofJenaGraph() {
return Response.BodyHandlers.throwOnError(
JenaBodyHandlers::responseToGraph,
(r) -> Response.isSuccess(r.statusCode())
);
return responseInfo -> {
JenaBodyHandlers.throwOnError(responseInfo);
return JenaBodyHandlers.responseToGraph(responseInfo);
};
}

private static Dataset responseToDataset(final Response.ResponseInfo responseInfo) {
Expand Down Expand Up @@ -149,10 +163,10 @@ public static Response.BodyHandler<Dataset> ofDataset() {
* @return an HTTP body handler
*/
public static Response.BodyHandler<Dataset> ofJenaDataset() {
return Response.BodyHandlers.throwOnError(
JenaBodyHandlers::responseToDataset,
(r) -> Response.isSuccess(r.statusCode())
);
return responseInfo -> {
JenaBodyHandlers.throwOnError(responseInfo);
return JenaBodyHandlers.responseToDataset(responseInfo);
};
}

static Lang toJenaLang(final String mediaType) {
Expand Down

0 comments on commit b8e402f

Please sign in to comment.