|
20 | 20 | */ |
21 | 21 | package com.inrupt.client.rdf4j; |
22 | 22 |
|
| 23 | +import com.inrupt.client.ClientHttpException; |
23 | 24 | import com.inrupt.client.Response; |
24 | 25 |
|
25 | 26 | import java.io.ByteArrayInputStream; |
26 | 27 | import java.io.IOException; |
27 | 28 | import java.io.InputStream; |
28 | 29 | import java.io.UncheckedIOException; |
| 30 | +import java.nio.charset.StandardCharsets; |
29 | 31 |
|
30 | 32 | import org.eclipse.rdf4j.model.Model; |
31 | 33 | import org.eclipse.rdf4j.model.impl.DynamicModelFactory; |
|
41 | 43 | */ |
42 | 44 | public final class RDF4JBodyHandlers { |
43 | 45 |
|
| 46 | + private static void throwOnError(final Response.ResponseInfo responseInfo) { |
| 47 | + if (!Response.isSuccess(responseInfo.statusCode())) { |
| 48 | + throw new ClientHttpException( |
| 49 | + "Could not map to a Jena entity.", |
| 50 | + responseInfo.uri(), |
| 51 | + responseInfo.statusCode(), |
| 52 | + responseInfo.headers(), |
| 53 | + new String(responseInfo.body().array(), StandardCharsets.UTF_8) |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
44 | 58 | private static Model responseToModel(final Response.ResponseInfo responseInfo) { |
45 | 59 | return responseInfo.headers().firstValue("Content-Type") |
46 | 60 | .map(RDF4JBodyHandlers::toRDF4JFormat).map(format -> { |
@@ -70,10 +84,10 @@ public static Response.BodyHandler<Model> ofModel() { |
70 | 84 | * @return an HTTP body handler |
71 | 85 | */ |
72 | 86 | public static Response.BodyHandler<Model> ofRDF4JModel() { |
73 | | - return Response.BodyHandlers.throwOnError( |
74 | | - RDF4JBodyHandlers::responseToModel, |
75 | | - (r) -> Response.isSuccess(r.statusCode()) |
76 | | - ); |
| 87 | + return responseInfo -> { |
| 88 | + RDF4JBodyHandlers.throwOnError(responseInfo); |
| 89 | + return RDF4JBodyHandlers.responseToModel(responseInfo); |
| 90 | + }; |
77 | 91 | } |
78 | 92 |
|
79 | 93 | private static Repository responseToRepository(final Response.ResponseInfo responseInfo) { |
@@ -108,10 +122,10 @@ public static Response.BodyHandler<Repository> ofRepository() { |
108 | 122 | * @return an HTTP body handler |
109 | 123 | */ |
110 | 124 | public static Response.BodyHandler<Repository> ofRDF4JRepository() { |
111 | | - return Response.BodyHandlers.throwOnError( |
112 | | - RDF4JBodyHandlers::responseToRepository, |
113 | | - (r) -> Response.isSuccess(r.statusCode()) |
114 | | - ); |
| 125 | + return responseInfo -> { |
| 126 | + RDF4JBodyHandlers.throwOnError(responseInfo); |
| 127 | + return RDF4JBodyHandlers.responseToRepository(responseInfo); |
| 128 | + }; |
115 | 129 | } |
116 | 130 |
|
117 | 131 | static RDFFormat toRDF4JFormat(final String mediaType) { |
|
0 commit comments