-
Notifications
You must be signed in to change notification settings - Fork 6
Support protected resources with RDF body handlers #1248
New issue
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| package com.inrupt.client.core; | ||
|
|
||
| import com.inrupt.client.Client; | ||
| import com.inrupt.client.ClientHttpException; | ||
| import com.inrupt.client.Headers.WwwAuthenticate; | ||
| import com.inrupt.client.Request; | ||
| import com.inrupt.client.Response; | ||
|
|
@@ -84,6 +85,7 @@ public <T> CompletionStage<Response<T>> send(final Request request, | |
| .map(token -> httpClient.send(upgradeRequest(request, token), responseBodyHandler)) | ||
| // Otherwise perform the regular HTTP authorization dance | ||
| .orElseGet(() -> httpClient.send(request, responseBodyHandler) | ||
| .handle((res, err) -> recover(res, err)) | ||
| .thenCompose(res -> { | ||
| if (res.statusCode() == UNAUTHORIZED) { | ||
| final List<Challenge> challenges = WwwAuthenticate | ||
|
|
@@ -126,6 +128,25 @@ Request upgradeRequest(final Request request, final Credential token) { | |
| return builder.build(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
|
Contributor
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. Could you reduce suppression from method scope to assignment scope. |
||
| static <T, R extends Throwable> Response<T> recover(final Response<T> res, final Throwable err) throws R { | ||
| if (err != null) { | ||
|
Contributor
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. Inverting this conditional would reduce indentation of whole method. |
||
| if (err instanceof ClientHttpException) { | ||
| final ClientHttpException ex = (ClientHttpException) err; | ||
| if (ex.getStatusCode() == UNAUTHORIZED) { | ||
| return new NoBodyResponse<T>(ex.getUri(), ex.getStatusCode(), ex.getHeaders()); | ||
| } | ||
| } else if (err.getCause() instanceof ClientHttpException) { | ||
|
Contributor
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. The error and its cause are inpected, but the hierarchy is not traversed further. |
||
| final ClientHttpException ex = (ClientHttpException) err.getCause(); | ||
| if (ex.getStatusCode() == UNAUTHORIZED) { | ||
| return new NoBodyResponse<T>(ex.getUri(), ex.getStatusCode(), ex.getHeaders()); | ||
| } | ||
|
Comment on lines
+141
to
+143
Contributor
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. Duplicate of above. Might be worth extracting for readability. |
||
| } | ||
| throw (R) err; | ||
| } | ||
| return res; | ||
| } | ||
|
Comment on lines
+132
to
+148
Contributor
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. This new functionality is quite complex and branching. |
||
|
|
||
| public static Client.Builder newBuilder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright Inrupt Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal in | ||
| * the Software without restriction, including without limitation the rights to use, | ||
| * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
| * Software, and to permit persons to whom the Software is furnished to do so, | ||
| * subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
| * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
| * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
| * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
| package com.inrupt.client.core; | ||
|
|
||
| import com.inrupt.client.Headers; | ||
| import com.inrupt.client.Response; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| class NoBodyResponse<T> implements Response<T> { | ||
|
|
||
| private final Headers responseHeaders; | ||
| private final int responseStatusCode; | ||
| private final URI responseUri; | ||
|
|
||
| public NoBodyResponse(final URI uri, final int statusCode, final Headers headers) { | ||
| this.responseHeaders = headers; | ||
| this.responseStatusCode = statusCode; | ||
| this.responseUri = uri; | ||
| } | ||
|
|
||
| @Override | ||
| public Headers headers() { | ||
| return responseHeaders; | ||
| } | ||
|
|
||
| @Override | ||
| public URI uri() { | ||
| return responseUri; | ||
| } | ||
|
|
||
| @Override | ||
| public int statusCode() { | ||
| return responseStatusCode; | ||
| } | ||
|
|
||
| @Override | ||
| public T body() { | ||
| return (T) null; | ||
| } | ||
| } |
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.
Could
recoverbe a method reference or are they not available in 8?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.
I also think an inline comment here would help, just like the explanatory notes above.