|
20 | 20 | */ |
21 | 21 | package com.inrupt.client.jena; |
22 | 22 |
|
23 | | -import com.inrupt.client.ClientHttpException; |
24 | | -import com.inrupt.client.ProblemDetails; |
25 | 23 | import com.inrupt.client.Response; |
26 | | -import com.inrupt.client.spi.JsonService; |
27 | | -import com.inrupt.client.spi.ServiceProvider; |
28 | 24 |
|
29 | 25 | import java.io.ByteArrayInputStream; |
30 | 26 | import java.io.IOException; |
|
47 | 43 | public final class JenaBodyHandlers { |
48 | 44 |
|
49 | 45 | 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; |
68 | 49 | } |
69 | 50 |
|
70 | 51 | private static Model responseToModel(final Response.ResponseInfo responseInfo) { |
@@ -98,20 +79,10 @@ public static Response.BodyHandler<Model> ofModel() { |
98 | 79 | * @return an HTTP body handler |
99 | 80 | */ |
100 | 81 | 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 | + ); |
115 | 86 | } |
116 | 87 |
|
117 | 88 | private static Graph responseToGraph(final Response.ResponseInfo responseInfo) { |
@@ -145,20 +116,10 @@ public static Response.BodyHandler<Graph> ofGraph() { |
145 | 116 | * @return an HTTP body handler |
146 | 117 | */ |
147 | 118 | 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 | + ); |
162 | 123 | } |
163 | 124 |
|
164 | 125 | private static Dataset responseToDataset(final Response.ResponseInfo responseInfo) { |
@@ -192,20 +153,10 @@ public static Response.BodyHandler<Dataset> ofDataset() { |
192 | 153 | * @return an HTTP body handler |
193 | 154 | */ |
194 | 155 | 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 | + ); |
209 | 160 | } |
210 | 161 |
|
211 | 162 | static Lang toJenaLang(final String mediaType) { |
|
0 commit comments