Skip to content

Commit 25deaa6

Browse files
committed
Revert "JCL-402: HTTP error handling in JenaBodyHandlers (#1159)"
This reverts commit 89534b4.
1 parent a51c01b commit 25deaa6

File tree

4 files changed

+42
-195
lines changed

4 files changed

+42
-195
lines changed

jena/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@
6767
<version>${slf4j.version}</version>
6868
<scope>test</scope>
6969
</dependency>
70-
<dependency>
71-
<groupId>com.inrupt.client</groupId>
72-
<artifactId>inrupt-client-jackson</artifactId>
73-
<version>${project.version}</version>
74-
<scope>test</scope>
75-
</dependency>
7670
<dependency>
7771
<groupId>org.wiremock</groupId>
7872
<artifactId>wiremock</artifactId>

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

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

23-
import com.inrupt.client.ClientHttpException;
2423
import com.inrupt.client.Response;
2524

2625
import java.io.ByteArrayInputStream;
2726
import java.io.IOException;
2827
import java.io.UncheckedIOException;
29-
import java.nio.charset.StandardCharsets;
3028

3129
import org.apache.jena.atlas.web.ContentType;
3230
import org.apache.jena.graph.Graph;
@@ -46,20 +44,13 @@ public final class JenaBodyHandlers {
4644

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

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-
61-
private static Model responseToModel(final Response.ResponseInfo responseInfo) {
62-
return responseInfo.headers().firstValue(CONTENT_TYPE)
47+
/**
48+
* Populate a Jena {@link Model} with an HTTP response body.
49+
*
50+
* @return an HTTP body handler
51+
*/
52+
public static Response.BodyHandler<Model> ofModel() {
53+
return responseInfo -> responseInfo.headers().firstValue(CONTENT_TYPE)
6354
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
6455
try (final var input = new ByteArrayInputStream(responseInfo.body().array())) {
6556
final var model = ModelFactory.createDefaultModel();
@@ -74,29 +65,12 @@ private static Model responseToModel(final Response.ResponseInfo responseInfo) {
7465
}
7566

7667
/**
77-
* Populate a Jena {@link Model} with an HTTP response body.
78-
*
79-
* @return an HTTP body handler
80-
* @deprecated Use {@link JenaBodyHandlers#ofJenaModel()} instead for consistent HTTP error handling.
81-
*/
82-
public static Response.BodyHandler<Model> ofModel() {
83-
return JenaBodyHandlers::responseToModel;
84-
}
85-
86-
/**
87-
* Populate a Jena {@link Model} with an HTTP response body.
68+
* Populate a Jena {@link Graph} with an HTTP response.
8869
*
8970
* @return an HTTP body handler
9071
*/
91-
public static Response.BodyHandler<Model> ofJenaModel() {
92-
return responseInfo -> {
93-
JenaBodyHandlers.throwOnError(responseInfo);
94-
return JenaBodyHandlers.responseToModel(responseInfo);
95-
};
96-
}
97-
98-
private static Graph responseToGraph(final Response.ResponseInfo responseInfo) {
99-
return responseInfo.headers().firstValue(CONTENT_TYPE)
72+
public static Response.BodyHandler<Graph> ofGraph() {
73+
return responseInfo -> responseInfo.headers().firstValue(CONTENT_TYPE)
10074
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
10175
try (final var input = new ByteArrayInputStream(responseInfo.body().array())) {
10276
final var graph = GraphMemFactory.createDefaultGraph();
@@ -110,63 +84,24 @@ private static Graph responseToGraph(final Response.ResponseInfo responseInfo) {
11084
.orElseGet(GraphMemFactory::createDefaultGraph);
11185
}
11286

113-
/**
114-
* Populate a Jena {@link Graph} with an HTTP response.
115-
*
116-
* @return an HTTP body handler
117-
* @deprecated Use {@link JenaBodyHandlers#ofJenaGraph} instead for consistent HTTP error handling.
118-
*/
119-
public static Response.BodyHandler<Graph> ofGraph() {
120-
return JenaBodyHandlers::responseToGraph;
121-
}
122-
123-
/**
124-
* Populate a Jena {@link Graph} with an HTTP response.
125-
*
126-
* @return an HTTP body handler
127-
*/
128-
public static Response.BodyHandler<Graph> ofJenaGraph() {
129-
return responseInfo -> {
130-
JenaBodyHandlers.throwOnError(responseInfo);
131-
return JenaBodyHandlers.responseToGraph(responseInfo);
132-
};
133-
}
134-
135-
private static Dataset responseToDataset(final Response.ResponseInfo responseInfo) {
136-
return responseInfo.headers().firstValue(CONTENT_TYPE)
137-
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
138-
try (final var input = new ByteArrayInputStream(responseInfo.body().array())) {
139-
final var dataset = DatasetFactory.create();
140-
RDFDataMgr.read(dataset, input, responseInfo.uri().toString(), lang);
141-
return dataset;
142-
} catch (final IOException ex) {
143-
throw new UncheckedIOException(
144-
"An I/O error occurred while data was read from the InputStream into a Dataset", ex);
145-
}
146-
})
147-
.orElseGet(DatasetFactory::create);
148-
}
149-
15087
/**
15188
* Populate a Jena {@link Dataset} with an HTTP response.
15289
*
15390
* @return an HTTP body handler
154-
* @deprecated Use {@link JenaBodyHandlers#ofJenaDataset} instead for consistent HTTP error handling.
15591
*/
15692
public static Response.BodyHandler<Dataset> ofDataset() {
157-
return JenaBodyHandlers::responseToDataset;
158-
}
159-
160-
/**
161-
* Populate a Jena {@link Dataset} with an HTTP response.
162-
*
163-
* @return an HTTP body handler
164-
*/
165-
public static Response.BodyHandler<Dataset> ofJenaDataset() {
166-
return responseInfo -> {
167-
JenaBodyHandlers.throwOnError(responseInfo);
168-
return JenaBodyHandlers.responseToDataset(responseInfo);
169-
};
93+
return responseInfo -> responseInfo.headers().firstValue(CONTENT_TYPE)
94+
.map(JenaBodyHandlers::toJenaLang).map(lang -> {
95+
try (final var input = new ByteArrayInputStream(responseInfo.body().array())) {
96+
final var dataset = DatasetFactory.create();
97+
RDFDataMgr.read(dataset, input, responseInfo.uri().toString(), lang);
98+
return dataset;
99+
} catch (final IOException ex) {
100+
throw new UncheckedIOException(
101+
"An I/O error occurred while data was read from the InputStream into a Dataset", ex);
102+
}
103+
})
104+
.orElseGet(DatasetFactory::create);
170105
}
171106

172107
static Lang toJenaLang(final String mediaType) {

jena/src/test/java/com/inrupt/client/jena/JenaBodyHandlersTest.java

Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import static org.junit.jupiter.api.Assertions.*;
2424

25-
import com.inrupt.client.ClientHttpException;
2625
import com.inrupt.client.Request;
2726
import com.inrupt.client.Response;
2827
import com.inrupt.client.spi.HttpService;
@@ -33,7 +32,6 @@
3332
import java.net.URI;
3433
import java.util.HashMap;
3534
import java.util.Map;
36-
import java.util.concurrent.CompletionException;
3735
import java.util.concurrent.ExecutionException;
3836

3937
import org.apache.jena.graph.NodeFactory;
@@ -59,14 +57,14 @@ static void teardown() {
5957
}
6058

6159
@Test
62-
void testOfJenaModelHandler() throws IOException,
60+
void testOfModelHandler() throws IOException,
6361
InterruptedException {
6462
final Request request = Request.newBuilder()
6563
.uri(URI.create(config.get("rdf_uri") + "/oneTriple"))
6664
.GET()
6765
.build();
6866

69-
final var response = client.send(request, JenaBodyHandlers.ofJenaModel())
67+
final var response = client.send(request, JenaBodyHandlers.ofModel())
7068
.toCompletableFuture().join();
7169

7270
assertEquals(200, response.statusCode());
@@ -80,15 +78,15 @@ void testOfJenaModelHandler() throws IOException,
8078
}
8179

8280
@Test
83-
void testOfJenaModelHandlerAsync() throws IOException,
81+
void testOfModelHandlerAsync() throws IOException,
8482
InterruptedException, ExecutionException {
8583
final Request request = Request.newBuilder()
8684
.uri(URI.create(config.get("rdf_uri") + "/oneTriple"))
8785
.header("Accept", "text/turtle")
8886
.GET()
8987
.build();
9088

91-
final var asyncResponse = client.send(request, JenaBodyHandlers.ofJenaModel());
89+
final var asyncResponse = client.send(request, JenaBodyHandlers.ofModel());
9290

9391
final int statusCode = asyncResponse.thenApply(Response::statusCode).toCompletableFuture().join();
9492
assertEquals(200, statusCode);
@@ -103,13 +101,13 @@ void testOfJenaModelHandlerAsync() throws IOException,
103101
}
104102

105103
@Test
106-
void testOfJenaModelHandlerWithURL() throws IOException, InterruptedException {
104+
void testOfModelHandlerWithURL() throws IOException, InterruptedException {
107105
final Request request = Request.newBuilder()
108106
.uri(URI.create(config.get("rdf_uri") + "/example"))
109107
.GET()
110108
.build();
111109

112-
final var response = client.send(request, JenaBodyHandlers.ofJenaModel())
110+
final var response = client.send(request, JenaBodyHandlers.ofModel())
113111
.toCompletableFuture().join();
114112

115113
assertEquals(200, response.statusCode());
@@ -122,36 +120,14 @@ void testOfJenaModelHandlerWithURL() throws IOException, InterruptedException {
122120
}
123121

124122
@Test
125-
void testOfJenaModelHandlerError() throws IOException,
126-
InterruptedException {
127-
final Request request = Request.newBuilder()
128-
.uri(URI.create(config.get("rdf_uri") + "/error"))
129-
.GET()
130-
.build();
131-
132-
final CompletionException completionException = assertThrows(
133-
CompletionException.class,
134-
() -> client.send(request, JenaBodyHandlers.ofJenaModel()).toCompletableFuture().join()
135-
);
136-
137-
final ClientHttpException httpException = (ClientHttpException) completionException.getCause();
138-
139-
assertEquals(429, httpException.getProblemDetails().getStatus());
140-
assertEquals("Too Many Requests", httpException.getProblemDetails().getTitle());
141-
assertEquals("Some details", httpException.getProblemDetails().getDetails());
142-
assertEquals("https://example.org/type", httpException.getProblemDetails().getType().toString());
143-
assertEquals("https://example.org/instance", httpException.getProblemDetails().getInstance().toString());
144-
}
145-
146-
@Test
147-
void testOfJenaDatasetHandler() throws IOException,
123+
void testOfDatasetHandler() throws IOException,
148124
InterruptedException {
149125
final Request request = Request.newBuilder()
150126
.uri(URI.create(config.get("rdf_uri") + "/oneTriple"))
151127
.GET()
152128
.build();
153129

154-
final var response = client.send(request, JenaBodyHandlers.ofJenaDataset())
130+
final var response = client.send(request, JenaBodyHandlers.ofDataset())
155131
.toCompletableFuture().join();
156132

157133
assertEquals(200, response.statusCode());
@@ -166,13 +142,13 @@ void testOfJenaDatasetHandler() throws IOException,
166142
}
167143

168144
@Test
169-
void testOfJenaDatasetHandlerWithURL() throws IOException, InterruptedException {
145+
void testOfDatasetHandlerWithURL() throws IOException, InterruptedException {
170146
final Request request = Request.newBuilder()
171147
.uri(URI.create(config.get("rdf_uri") + "/example"))
172148
.GET()
173149
.build();
174150

175-
final var response = client.send(request, JenaBodyHandlers.ofJenaDataset())
151+
final var response = client.send(request, JenaBodyHandlers.ofDataset())
176152
.toCompletableFuture().join();
177153

178154
assertEquals(200, response.statusCode());
@@ -187,37 +163,15 @@ void testOfJenaDatasetHandlerWithURL() throws IOException, InterruptedException
187163
}
188164

189165
@Test
190-
void testOfJenaDatasetHandlerError() throws IOException,
191-
InterruptedException {
192-
final Request request = Request.newBuilder()
193-
.uri(URI.create(config.get("rdf_uri") + "/error"))
194-
.GET()
195-
.build();
196-
197-
final CompletionException completionException = assertThrows(
198-
CompletionException.class,
199-
() -> client.send(request, JenaBodyHandlers.ofJenaDataset()).toCompletableFuture().join()
200-
);
201-
202-
final ClientHttpException httpException = (ClientHttpException) completionException.getCause();
203-
204-
assertEquals(429, httpException.getProblemDetails().getStatus());
205-
assertEquals("Too Many Requests", httpException.getProblemDetails().getTitle());
206-
assertEquals("Some details", httpException.getProblemDetails().getDetails());
207-
assertEquals("https://example.org/type", httpException.getProblemDetails().getType().toString());
208-
assertEquals("https://example.org/instance", httpException.getProblemDetails().getInstance().toString());
209-
}
210-
211-
@Test
212-
void testOfJenaGraphHandlerAsync() throws IOException,
166+
void testOfGraphHandlerAsync() throws IOException,
213167
InterruptedException, ExecutionException {
214168
final Request request = Request.newBuilder()
215169
.uri(URI.create(config.get("rdf_uri") + "/oneTriple"))
216170
.header("Accept", "text/turtle")
217171
.GET()
218172
.build();
219173

220-
final var asyncResponse = client.send(request, JenaBodyHandlers.ofJenaGraph());
174+
final var asyncResponse = client.send(request, JenaBodyHandlers.ofGraph());
221175

222176
final int statusCode = asyncResponse.thenApply(Response::statusCode).toCompletableFuture().join();
223177
assertEquals(200, statusCode);
@@ -232,14 +186,14 @@ void testOfJenaGraphHandlerAsync() throws IOException,
232186
}
233187

234188
@Test
235-
void testOfJenaGraphHandler() throws IOException,
189+
void testOfGraphHandler() throws IOException,
236190
InterruptedException {
237191
final Request request = Request.newBuilder()
238192
.uri(URI.create(config.get("rdf_uri") + "/oneTriple"))
239193
.GET()
240194
.build();
241195

242-
final var response = client.send(request, JenaBodyHandlers.ofJenaGraph())
196+
final var response = client.send(request, JenaBodyHandlers.ofGraph())
243197
.toCompletableFuture().join();
244198

245199
assertEquals(200, response.statusCode());
@@ -253,13 +207,13 @@ void testOfJenaGraphHandler() throws IOException,
253207
}
254208

255209
@Test
256-
void testOfJenaGraphHandlerWithURL() throws IOException, InterruptedException {
210+
void testOfGraphHandlerWithURL() throws IOException, InterruptedException {
257211
final Request request = Request.newBuilder()
258212
.uri(URI.create(config.get("rdf_uri") + "/example"))
259213
.GET()
260214
.build();
261215

262-
final var response = client.send(request, JenaBodyHandlers.ofJenaGraph())
216+
final var response = client.send(request, JenaBodyHandlers.ofGraph())
263217
.toCompletableFuture().join();
264218

265219
assertEquals(200, response.statusCode());
@@ -271,26 +225,4 @@ void testOfJenaGraphHandlerWithURL() throws IOException, InterruptedException {
271225
null)
272226
);
273227
}
274-
275-
@Test
276-
void testOfJenaGraphHandlerError() throws IOException,
277-
InterruptedException {
278-
final Request request = Request.newBuilder()
279-
.uri(URI.create(config.get("rdf_uri") + "/error"))
280-
.GET()
281-
.build();
282-
283-
final CompletionException completionException = assertThrows(
284-
CompletionException.class,
285-
() -> client.send(request, JenaBodyHandlers.ofJenaGraph()).toCompletableFuture().join()
286-
);
287-
288-
final ClientHttpException httpException = (ClientHttpException) completionException.getCause();
289-
290-
assertEquals(429, httpException.getProblemDetails().getStatus());
291-
assertEquals("Too Many Requests", httpException.getProblemDetails().getTitle());
292-
assertEquals("Some details", httpException.getProblemDetails().getDetails());
293-
assertEquals("https://example.org/type", httpException.getProblemDetails().getType().toString());
294-
assertEquals("https://example.org/instance", httpException.getProblemDetails().getInstance().toString());
295-
}
296228
}

0 commit comments

Comments
 (0)