| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -20,22 +20,14 @@ | |
| */ | ||
| package com.inrupt.client.solid; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| import com.inrupt.client.Client; | ||
| import com.inrupt.client.ClientProvider; | ||
| import com.inrupt.client.Headers; | ||
| import com.inrupt.client.RDFSource; | ||
| import com.inrupt.client.Request; | ||
| import com.inrupt.client.Resource; | ||
| import com.inrupt.client.Response; | ||
| import com.inrupt.client.ValidationResult; | ||
| import com.inrupt.client.*; | ||
| import com.inrupt.client.auth.Session; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| Expand Down Expand Up | @@ -131,36 +123,43 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final | |
| headers.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent)); | ||
|
|
||
| final Request request = builder.build(); | ||
| return client.send(request, Response.BodyHandlers.ofByteArray()) | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis looks like a large change, but essentially the error branch of the initial if has been moved to the wrapping throwOnError
Sorry, something went wrong.
All reactions
|
||
| .thenApply(response -> { | ||
| if (response.statusCode() >= ERROR_STATUS) { | ||
| throw SolidClientException.handle("Unable to read resource at " + request.uri(), request.uri(), | ||
| response.statusCode(), response.headers(), new String(response.body())); | ||
| } else { | ||
| final String contentType = response.headers().firstValue(CONTENT_TYPE) | ||
| .orElse("application/octet-stream"); | ||
| try { | ||
| // Check that this is an RDFSoure | ||
| if (RDFSource.class.isAssignableFrom(clazz)) { | ||
| final Dataset dataset = SolidResourceHandlers.buildDataset(contentType, response.body(), | ||
| request.uri().toString()).orElse(null); | ||
| final T obj = construct(request.uri(), clazz, dataset, response.headers()); | ||
| final ValidationResult res = RDFSource.class.cast(obj).validate(); | ||
| if (!res.isValid()) { | ||
| throw new DataMappingException( | ||
| "Unable to map resource into type: [" + clazz.getSimpleName() + "] ", | ||
| res.getResults()); | ||
| } | ||
| return obj; | ||
| // Otherwise, create a non-RDF-bearing resource | ||
| } else { | ||
| return construct(request.uri(), clazz, contentType, | ||
| new ByteArrayInputStream(response.body()), response.headers()); | ||
| return client.send( | ||
| request, | ||
| Response.BodyHandlers.ofByteArray() | ||
| ).thenApply(response -> { | ||
| if (!Response.isSuccess(response.statusCode())) { | ||
| throw SolidClientException.handle( | ||
| "Reading resource failed.", | ||
| response.uri(), | ||
| response.statusCode(), | ||
| response.headers(), | ||
| new String(response.body(), StandardCharsets.UTF_8) | ||
| ); | ||
| } | ||
|
|
||
| final String contentType = response.headers().firstValue(CONTENT_TYPE) | ||
| .orElse("application/octet-stream"); | ||
| try { | ||
| // Check that this is an RDFSoure | ||
| if (RDFSource.class.isAssignableFrom(clazz)) { | ||
| final Dataset dataset = SolidResourceHandlers.buildDataset(contentType, response.body(), | ||
| request.uri().toString()).orElse(null); | ||
| final T obj = construct(request.uri(), clazz, dataset, response.headers()); | ||
| final ValidationResult res = RDFSource.class.cast(obj).validate(); | ||
| if (!res.isValid()) { | ||
| throw new DataMappingException( | ||
| "Unable to map resource into type: [" + clazz.getSimpleName() + "] ", | ||
| res.getResults()); | ||
| } | ||
| } catch (final ReflectiveOperationException ex) { | ||
| throw new SolidResourceException("Unable to read resource into type " + clazz.getName(), | ||
| ex); | ||
| return obj; | ||
| // Otherwise, create a non-RDF-bearing resource | ||
| } else { | ||
| return construct(request.uri(), clazz, contentType, | ||
| new ByteArrayInputStream(response.body()), response.headers()); | ||
| } | ||
| } catch (final ReflectiveOperationException ex) { | ||
| throw new SolidResourceException("Unable to read resource into type " + clazz.getName(), | ||
| ex); | ||
| } | ||
| }); | ||
| } | ||
| Expand Down Expand Up | @@ -280,13 +279,21 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final | |
| defaultHeaders.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent)); | ||
| headers.firstValue(USER_AGENT).ifPresent(agent -> builder.setHeader(USER_AGENT, agent)); | ||
|
|
||
| return client.send(builder.build(), Response.BodyHandlers.ofByteArray()).thenApply(res -> { | ||
| if (isSuccess(res.statusCode())) { | ||
| return null; | ||
| } else { | ||
| throw SolidClientException.handle("Unable to delete resource", resource.getIdentifier(), | ||
| res.statusCode(), res.headers(), new String(res.body(), UTF_8)); | ||
|
Comment thread
NSeydoux marked this conversation as resolved.
Resolved
|
||
|
|
||
| return client.send( | ||
| builder.build(), | ||
| Response.BodyHandlers.ofByteArray() | ||
| ).thenApply(response -> { | ||
| if (!Response.isSuccess(response.statusCode())) { | ||
| throw SolidClientException.handle( | ||
| "Deleting resource failed.", | ||
| response.uri(), | ||
| response.statusCode(), | ||
| response.headers(), | ||
| new String(response.body(), StandardCharsets.UTF_8) | ||
| ); | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| Expand Down Expand Up | @@ -370,9 +377,14 @@ public SolidClient build() { | |
| <T extends Resource> Function<Response<byte[]>, CompletionStage<T>> handleResponse(final T resource, | ||
| final Headers headers, final String message) { | ||
| return res -> { | ||
| if (!isSuccess(res.statusCode())) { | ||
| throw SolidClientException.handle(message, resource.getIdentifier(), | ||
| res.statusCode(), res.headers(), new String(res.body(), UTF_8)); | ||
| if (!Response.isSuccess(res.statusCode())) { | ||
| throw SolidClientException.handle( | ||
| message, | ||
| resource.getIdentifier(), | ||
| res.statusCode(), | ||
| res.headers(), | ||
| new String(res.body(), StandardCharsets.UTF_8) | ||
| ); | ||
| } | ||
|
|
||
| if (!fetchAfterWrite) { | ||
| Expand All | @@ -382,7 +394,6 @@ <T extends Resource> Function<Response<byte[]>, CompletionStage<T>> handleRespon | |
| @SuppressWarnings("unchecked") | ||
| final Class<T> clazz = (Class<T>) resource.getClass(); | ||
| return read(resource.getIdentifier(), headers, clazz); | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| Expand Down Expand Up | @@ -445,10 +456,6 @@ static void decorateHeaders(final Request.Builder builder, final Headers headers | |
| } | ||
| } | ||
|
|
||
| static boolean isSuccess(final int statusCode) { | ||
| return statusCode >= 200 && statusCode < 300; | ||
| } | ||
|
|
||
| static Request.BodyPublisher cast(final Resource resource) { | ||
| try { | ||
| return Request.BodyPublishers.ofInputStream(resource.getEntity()); | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityDo we really need this method?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt avoids some duplication, and my impression was that it is a quite common pattern to have this affordance on HTTP response object: JS Response has a .ok getter, and OkHttp has isSuccessful. I'm not strongly opposed to removing this, but I find it reads slightly better than checking the status range each time.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.