|
22 | 22 |
|
23 | 23 | import com.inrupt.client.*; |
24 | 24 | import com.inrupt.client.auth.Session; |
25 | | -import com.inrupt.client.spi.JsonService; |
26 | | -import com.inrupt.client.spi.ServiceProvider; |
27 | 25 |
|
28 | 26 | import java.io.ByteArrayInputStream; |
29 | 27 | import java.io.IOException; |
@@ -54,21 +52,11 @@ public class SolidClient { |
54 | 52 | private final Client client; |
55 | 53 | private final Headers defaultHeaders; |
56 | 54 | private final boolean fetchAfterWrite; |
57 | | - private final JsonService jsonService; |
58 | 55 |
|
59 | 56 | SolidClient(final Client client, final Headers headers, final boolean fetchAfterWrite) { |
60 | 57 | this.client = Objects.requireNonNull(client, "Client may not be null!"); |
61 | 58 | this.defaultHeaders = Objects.requireNonNull(headers, "Headers may not be null!"); |
62 | 59 | this.fetchAfterWrite = fetchAfterWrite; |
63 | | - JsonService js; |
64 | | - try { |
65 | | - // It is acceptable for a SolidClient instance to be in a classpath without any implementation for |
66 | | - // JsonService, in which case the ProblemDetails exceptions will fallback to default and not be parsed. |
67 | | - js = ServiceProvider.getJsonService(); |
68 | | - } catch (IllegalStateException e) { |
69 | | - js = null; |
70 | | - } |
71 | | - this.jsonService = js; |
72 | 60 | } |
73 | 61 |
|
74 | 62 | /** |
@@ -134,7 +122,7 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final |
134 | 122 | return client.send(request, Response.BodyHandlers.ofByteArray()) |
135 | 123 | .thenApply(response -> { |
136 | 124 | if (response.statusCode() >= ERROR_STATUS) { |
137 | | - throw this.exceptionFromErrorResponse( |
| 125 | + throw SolidClientException.fromErrorResponse( |
138 | 126 | "Unable to read resource at " + request.uri(), |
139 | 127 | response.uri(), |
140 | 128 | response.statusCode(), |
@@ -289,7 +277,7 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final |
289 | 277 | if (isSuccess(res.statusCode())) { |
290 | 278 | return null; |
291 | 279 | } else { |
292 | | - throw this.exceptionFromErrorResponse( |
| 280 | + throw SolidClientException.fromErrorResponse( |
293 | 281 | "Unable to delete resource", |
294 | 282 | resource.getIdentifier(), |
295 | 283 | res.statusCode(), |
@@ -381,7 +369,7 @@ <T extends Resource> Function<Response<byte[]>, CompletionStage<T>> handleRespon |
381 | 369 | final Headers headers, final String message) { |
382 | 370 | return res -> { |
383 | 371 | if (!isSuccess(res.statusCode())) { |
384 | | - throw this.exceptionFromErrorResponse( |
| 372 | + throw SolidClientException.fromErrorResponse( |
385 | 373 | message, |
386 | 374 | resource.getIdentifier(), |
387 | 375 | res.statusCode(), |
@@ -472,38 +460,4 @@ static Request.BodyPublisher cast(final Resource resource) { |
472 | 460 | " into Solid Resource", ex); |
473 | 461 | } |
474 | 462 | } |
475 | | - |
476 | | - private SolidClientException exceptionFromErrorResponse( |
477 | | - final String message, |
478 | | - final URI uri, |
479 | | - final int code, |
480 | | - final Headers headers, |
481 | | - final byte[] body |
482 | | - ) { |
483 | | - ProblemDetails pd; |
484 | | - if ( |
485 | | - this.jsonService == null |
486 | | - || (headers != null && !headers.allValues("Content-Type").contains(ProblemDetails.MIME_TYPE))) { |
487 | | - pd = new ProblemDetails(null, HttpStatus.getStatusMessage(code), null, code, null); |
488 | | - return SolidClientException.handle(message, pd, uri, headers, new String(body)); |
489 | | - } |
490 | | - try { |
491 | | - // ProblemDetails doesn't have a default constructor, and we can't use JSON mapping annotations because |
492 | | - // the JSON service is an abstraction over JSON-B and Jackson, so we deserialize the JSON object in a Map |
493 | | - // and build the ProblemDetails from the Map values. |
494 | | - final Map<String, Object> pdData = this.jsonService.fromJson( |
495 | | - new ByteArrayInputStream(body), |
496 | | - new HashMap<String, Object>(){}.getClass().getGenericSuperclass() |
497 | | - ); |
498 | | - final String title = (String) pdData.get("title"); |
499 | | - final String details = (String) pdData.get("details"); |
500 | | - final URI type = URI.create((String) pdData.get("type")); |
501 | | - final URI instance = URI.create((String) pdData.get("instance")); |
502 | | - final int status = (int) pdData.get("status"); |
503 | | - pd = new ProblemDetails(type, title, details, status, instance); |
504 | | - } catch (IOException e) { |
505 | | - pd = new ProblemDetails( null, HttpStatus.getStatusMessage(code), null, code, null); |
506 | | - } |
507 | | - return SolidClientException.handle(message, pd, uri, headers, new String(body)); |
508 | | - } |
509 | 463 | } |
0 commit comments