Skip to content

Commit b13bc8d

Browse files
committed
Use throwing body mapper
1 parent aee1f69 commit b13bc8d

File tree

2 files changed

+73
-45
lines changed

2 files changed

+73
-45
lines changed

solid/src/main/java/com/inrupt/client/solid/SolidClient.java

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -125,42 +125,39 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
125125
headers.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent));
126126

127127
final Request request = builder.build();
128-
return client.send(request, Response.BodyHandlers.ofByteArray())
129-
.thenApply(response -> {
130-
if (response.statusCode() >= ERROR_STATUS) {
131-
throw SolidClientException.handle(
132-
"Unable to read resource at " + request.uri(),
133-
response.uri(),
134-
response.statusCode(),
135-
response.headers(),
136-
new String(response.body())
137-
);
138-
} else {
139-
final String contentType = response.headers().firstValue(CONTENT_TYPE)
140-
.orElse("application/octet-stream");
141-
try {
142-
// Check that this is an RDFSoure
143-
if (RDFSource.class.isAssignableFrom(clazz)) {
144-
final Dataset dataset = SolidResourceHandlers.buildDataset(contentType, response.body(),
145-
request.uri().toString()).orElse(null);
146-
final T obj = construct(request.uri(), clazz, dataset, response.headers());
147-
final ValidationResult res = RDFSource.class.cast(obj).validate();
148-
if (!res.isValid()) {
149-
throw new DataMappingException(
150-
"Unable to map resource into type: [" + clazz.getSimpleName() + "] ",
151-
res.getResults());
152-
}
153-
return obj;
154-
// Otherwise, create a non-RDF-bearing resource
155-
} else {
156-
return construct(request.uri(), clazz, contentType,
157-
new ByteArrayInputStream(response.body()), response.headers());
128+
return client.send(
129+
request,
130+
Response.BodyHandlers.throwOnError(Response.BodyHandlers.ofByteArray(), (r) -> isSuccess(r.statusCode()))
131+
).thenApply(response -> {
132+
final String contentType = response.headers().firstValue(CONTENT_TYPE)
133+
.orElse("application/octet-stream");
134+
try {
135+
// Check that this is an RDFSoure
136+
if (RDFSource.class.isAssignableFrom(clazz)) {
137+
final Dataset dataset = SolidResourceHandlers.buildDataset(contentType, response.body(),
138+
request.uri().toString()).orElse(null);
139+
final T obj = construct(request.uri(), clazz, dataset, response.headers());
140+
final ValidationResult res = RDFSource.class.cast(obj).validate();
141+
if (!res.isValid()) {
142+
throw new DataMappingException(
143+
"Unable to map resource into type: [" + clazz.getSimpleName() + "] ",
144+
res.getResults());
158145
}
159-
} catch (final ReflectiveOperationException ex) {
160-
throw new SolidResourceException("Unable to read resource into type " + clazz.getName(),
161-
ex);
146+
return obj;
147+
// Otherwise, create a non-RDF-bearing resource
148+
} else {
149+
return construct(request.uri(), clazz, contentType,
150+
new ByteArrayInputStream(response.body()), response.headers());
162151
}
152+
} catch (final ReflectiveOperationException ex) {
153+
throw new SolidResourceException("Unable to read resource into type " + clazz.getName(),
154+
ex);
163155
}
156+
}).exceptionally(exception -> {
157+
if (exception instanceof ClientHttpException) {
158+
throw SolidClientException.handle((ClientHttpException) exception);
159+
}
160+
throw new RuntimeException("Something went wrong reading "+request.uri(), exception);
164161
});
165162
}
166163

@@ -279,19 +276,19 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final
279276
defaultHeaders.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent));
280277
headers.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent));
281278

282-
return client.send(builder.build(), Response.BodyHandlers.ofByteArray()).thenApply(res -> {
283-
if (isSuccess(res.statusCode())) {
284-
return null;
285-
} else {
286-
throw SolidClientException.handle(
287-
"Unable to delete resource",
288-
resource.getIdentifier(),
289-
res.statusCode(),
290-
res.headers(),
291-
new String(res.body(), StandardCharsets.UTF_8)
292-
);
279+
return client.send(
280+
builder.build(),
281+
Response.BodyHandlers.throwOnError(
282+
Response.BodyHandlers.ofByteArray(),
283+
(r) -> isSuccess(r.statusCode())
284+
)
285+
).exceptionally(exception -> {
286+
if (exception instanceof ClientHttpException) {
287+
throw SolidClientException.handle((ClientHttpException) exception);
293288
}
294-
});
289+
throw new RuntimeException("Something went wrong reading "+resource.getIdentifier(), exception);
290+
// FIXME I don't understand why the following is required.
291+
}).thenAccept(o -> {});
295292
}
296293

297294
/**

solid/src/main/java/com/inrupt/client/solid/SolidClientException.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,36 @@ public static SolidClientException handle(
9090
return new SolidClientException(message, uri, statusCode, headers, body);
9191
}
9292
}
93+
94+
public static SolidClientException handle(final ClientHttpException exception){
95+
switch (exception.getStatusCode()) {
96+
case BadRequestException.STATUS_CODE:
97+
return (BadRequestException) exception;
98+
case UnauthorizedException.STATUS_CODE:
99+
return (UnauthorizedException) exception;
100+
case ForbiddenException.STATUS_CODE:
101+
return (ForbiddenException) exception;
102+
case NotFoundException.STATUS_CODE:
103+
return (NotFoundException) exception;
104+
case MethodNotAllowedException.STATUS_CODE:
105+
return (MethodNotAllowedException) exception;
106+
case NotAcceptableException.STATUS_CODE:
107+
return (NotAcceptableException) exception;
108+
case ConflictException.STATUS_CODE:
109+
return (ConflictException) exception;
110+
case GoneException.STATUS_CODE:
111+
return (GoneException) exception;
112+
case PreconditionFailedException.STATUS_CODE:
113+
return (PreconditionFailedException) exception;
114+
case UnsupportedMediaTypeException.STATUS_CODE:
115+
return (UnsupportedMediaTypeException) exception;
116+
case TooManyRequestsException.STATUS_CODE:
117+
return (TooManyRequestsException) exception;
118+
case InternalServerErrorException.STATUS_CODE:
119+
return (InternalServerErrorException) exception;
120+
default:
121+
return (SolidClientException) exception;
122+
}
123+
}
93124
}
94125

0 commit comments

Comments
 (0)