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
98 changes: 98 additions & 0 deletions api/src/main/java/com/inrupt/client/ClientHttpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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;

import java.net.URI;

/**
* A runtime exception representing an HTTP error response carrying a structured representation of the problem. The
* problem description is embedded in a {@link ProblemDetails} instance.
*/
public class ClientHttpException extends InruptClientException {
private final ProblemDetails problemDetails;
private final URI uri;
private final int statusCode;
private final String body;
private final transient Headers headers;

/**
* Create a ClientHttpException.
* @param message the exception message
* @param uri the error response URI
* @param statusCode the error response status code
* @param headers the error response headers
* @param body the error response body
*/
public ClientHttpException(final String message, final URI uri, final int statusCode,
final Headers headers, final String body) {
super(message);
this.uri = uri;
this.statusCode = statusCode;
this.headers = headers;
this.body = body;
this.problemDetails = ProblemDetails.fromErrorResponse(statusCode, headers, body.getBytes());
}

/**
* Retrieve the URI associated with this exception.
*
* @return the uri
*/
public URI getUri() {
return uri;
}

/**
* Retrieve the status code associated with this exception.
*
* @return the status code
*/
public int getStatusCode() {
return statusCode;
}

/**
* Retrieve the headers associated with this exception.
*
* @return the headers
*/
public Headers getHeaders() {
return headers;
}

/**
* Retrieve the body associated with this exception.
*
* @return the body
*/
public String getBody() {
return body;
}

/**
* Retrieve the {@link ProblemDetails} instance describing the HTTP error response.
* @return the problem details object
*/
public ProblemDetails getProblemDetails() {
return this.problemDetails;
}

}
80 changes: 80 additions & 0 deletions api/src/main/java/com/inrupt/client/HttpStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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;

import java.util.Arrays;

public final class HttpStatus {

public static final int BAD_REQUEST = 400;
public static final int UNAUTHORIZED = 401;
public static final int FORBIDDEN = 403;
public static final int NOT_FOUND = 404;
public static final int METHOD_NOT_ALLOWED = 405;
public static final int NOT_ACCEPTABLE = 406;
public static final int CONFLICT = 409;
public static final int GONE = 410;
public static final int PRECONDITION_FAILED = 412;
public static final int UNSUPPORTED_MEDIA_TYPE = 415;
public static final int TOO_MANY_REQUESTS = 429;
public static final int INTERNAL_SERVER_ERROR = 500;

enum StatusMessages {
BAD_REQUEST(HttpStatus.BAD_REQUEST, "Bad Request"),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "Unauthorized"),
FORBIDDEN(HttpStatus.FORBIDDEN, "Forbidden"),
NOT_FOUND(HttpStatus.NOT_FOUND, "Not Found"),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "Method Not Allowed"),
NOT_ACCEPTABLE(HttpStatus.NOT_ACCEPTABLE, "Not Acceptable"),
CONFLICT(HttpStatus.CONFLICT, "Conflict"),
GONE(HttpStatus.GONE, "Gone"),
PRECONDITION_FAILED(HttpStatus.PRECONDITION_FAILED, "Precondition Failed"),
UNSUPPORTED_MEDIA_TYPE(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type"),
TOO_MANY_REQUESTS(HttpStatus.TOO_MANY_REQUESTS, "Too Many Requests"),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error");

private final int code;
final String message;

StatusMessages(final int code, final String message) {
this.code = code;
this.message = message;
}

static String getStatusMessage(final int statusCode) {
return Arrays.stream(StatusMessages.values())
.filter(status -> status.code == statusCode)
.findFirst()
.map(knownStatus -> knownStatus.message)
.orElseGet(() -> {
// If the status is unknown, default to 400 for client errors and 500 for server errors
if (statusCode >= 400 && statusCode <= 499) {
return "Unknown Client Error";
}
return "Unknown Server Error";
});
}
}

private HttpStatus() {
// noop
}
}
140 changes: 140 additions & 0 deletions api/src/main/java/com/inrupt/client/ProblemDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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;

import com.inrupt.client.spi.JsonService;
import com.inrupt.client.spi.ServiceProvider;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;

/**
* A data class representing a structured problem description sent by the server on error response.
*
* @see <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC 9457 Problem Details for HTTP APIs</a>
*/
public class ProblemDetails {
public static final String MIME_TYPE = "application/problem+json";
public static final String DEFAULT_TYPE = "about:blank";
private final URI type;
private final String title;
private final String details;
private final int status;
private final URI instance;
private static JsonService jsonService;
private static boolean isJsonServiceInitialized;

public ProblemDetails(
final URI type,
final String title,
final String details,
final int status,
final URI instance
) {
this.type = type;
this.title = title;
this.details = details;
this.status = status;
this.instance = instance;
}

public URI getType() {
return this.type;
};

public String getTitle() {
return this.title;
};

public String getDetails() {
return this.details;
};

public int getStatus() {
return this.status;
};

public URI getInstance() {
return this.instance;
};

private static JsonService getJsonService() {
if (ProblemDetails.isJsonServiceInitialized) {
return ProblemDetails.jsonService;
}
// It is acceptable for a JenaBodyHandlers instance to be in a classpath without any implementation for
// JsonService, in which case the ProblemDetails exceptions will fallback to default and not be parsed.
try {
ProblemDetails.jsonService = ServiceProvider.getJsonService();
} catch (IllegalStateException e) {
ProblemDetails.jsonService = null;
}
ProblemDetails.isJsonServiceInitialized = true;
return ProblemDetails.jsonService;
}

public static ProblemDetails fromErrorResponse(
final int statusCode,
final Headers headers,
final byte[] body
) {
final JsonService jsonService = getJsonService();
if (jsonService == null
|| (headers != null && !headers.allValues("Content-Type").contains(ProblemDetails.MIME_TYPE))) {
return new ProblemDetails(
URI.create(ProblemDetails.DEFAULT_TYPE),
HttpStatus.StatusMessages.getStatusMessage(statusCode),
null,
statusCode,
null
);
}
try {
final ProblemDetailsData pdData = jsonService.fromJson(
new ByteArrayInputStream(body),
ProblemDetailsData.class
);
final URI type = Optional.ofNullable(pdData.getType())
.orElse(URI.create(ProblemDetails.DEFAULT_TYPE));
final String title = Optional.ofNullable(pdData.getTitle())
.orElse(HttpStatus.StatusMessages.getStatusMessage(statusCode));
// JSON mappers map invalid integers to 0, which is an invalid value in our case anyway.
final int status = Optional.of(pdData.getStatus()).filter(s -> s != 0).orElse(statusCode);
return new ProblemDetails(
type,
title,
pdData.getDetails(),
status,
pdData.getInstance()
);
} catch (IOException e) {
return new ProblemDetails(
URI.create(ProblemDetails.DEFAULT_TYPE),
HttpStatus.StatusMessages.getStatusMessage(statusCode),
null,
statusCode,
null
);
}
}
}
55 changes: 55 additions & 0 deletions api/src/main/java/com/inrupt/client/ProblemDetailsData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.inrupt.client;

import java.net.URI;

/**
* This package-private mutable class is used for JSON deserialization.
* Once instantiated, it is used to build an immutable {@link ProblemDetails}.
*/
class ProblemDetailsData {
private URI type;
private String title;
private String details;
private int status;
private URI instance;

public URI getType() {
return this.type;
};

public String getTitle() {
return this.title;
};

public String getDetails() {
return this.details;
};

public int getStatus() {
return this.status;
};

public URI getInstance() {
return this.instance;
};

public void setType(final URI type) {
this.type = type;
}

public void setTitle(final String title) {
this.title = title;
}

public void setDetails(final String details) {
this.details = details;
}

public void setStatus(final int status) {
this.status = status;
}

public void setInstance(final URI instance) {
this.instance = instance;
}
}
Loading