Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/src/main/java/com/inrupt/client/core/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could recover be a method reference or are they not available in 8?

Copy link
Contributor

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.

.thenCompose(res -> {
if (res.statusCode() == UNAUTHORIZED) {
final List<Challenge> challenges = WwwAuthenticate
Expand Down Expand Up @@ -126,6 +128,25 @@ Request upgradeRequest(final Request request, final Credential token) {
return builder.build();
}

@SuppressWarnings("unchecked")
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
I'm sure this is accurate vis a vis our architecture, but it feels off.
I think an explanation with inline comments would be good.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new functionality is quite complex and branching.
Would it not make sense to add tests that highlight its behavior specifically?


public static Client.Builder newBuilder() {
return new Builder();
}
Expand Down
59 changes: 59 additions & 0 deletions core/src/main/java/com/inrupt/client/core/NoBodyResponse.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void testSendOfModelProtectedResource() {
.GET()
.build();

final Response<Model> response = client.send(request, JenaBodyHandlers.ofModel())
final Response<Model> response = client.send(request, JenaBodyHandlers.ofJenaModel())
.toCompletableFuture().join();
assertEquals(200, response.statusCode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void testSendOfModelProtectedResource() {
.GET()
.build();

final Response<Model> response = client.send(request, RDF4JBodyHandlers.ofModel())
final Response<Model> response = client.send(request, RDF4JBodyHandlers.ofRDF4JModel())
.toCompletableFuture().join();
assertEquals(200, response.statusCode());

Expand Down