Skip to content

Commit

Permalink
Code Review before release (#3)
Browse files Browse the repository at this point in the history
Code Review before release (#3)
  • Loading branch information
mjacoby authored Dec 12, 2024
1 parent ab66570 commit 846aacd
Show file tree
Hide file tree
Showing 40 changed files with 1,781 additions and 1,127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -23,7 +22,13 @@
* (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing).
*/
public class BadRequestException extends StatusCodeException {
public BadRequestException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public BadRequestException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class ClientException extends Exception {
*
* @param message the detail message
*/
public ClientException(String message) {
protected ClientException(String message) {
super(message);
}

Expand All @@ -35,7 +35,7 @@ public ClientException(String message) {
* @param message the detail message
* @param cause the cause
*/
public ClientException(String message, Throwable cause) {
protected ClientException(String message, Throwable cause) {
super(message, cause);
}

Expand All @@ -45,7 +45,7 @@ public ClientException(String message, Throwable cause) {
*
* @param cause the cause
*/
public ClientException(Throwable cause) {
protected ClientException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -23,7 +22,13 @@
* such as an edit conflict between multiple simultaneous updates.
*/
public class ConflictException extends StatusCodeException {
public ConflictException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public ConflictException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -25,7 +24,13 @@
* The request should not be repeated.
*/
public class ForbiddenException extends StatusCodeException {
public ForbiddenException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public ForbiddenException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


/**
* A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
*/
public class InternalServerErrorException extends StatusCodeException {
public InternalServerErrorException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public InternalServerErrorException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException;
import org.json.JSONException;


/**
* This exception is thrown if the server responds with a body that cannot be deserialized.
*/
public class InvalidPayloadException extends RuntimeException {

/**
* Constructs a new exception.
*
* @param cause the cause of the exception
*/
public InvalidPayloadException(Throwable cause) {
super(cause);
}


/**
* Constructs a new exception.
*
* @param message the message
* @param cause the cause of the exception
*/
public InvalidPayloadException(String message, Throwable cause) {
super(message, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -24,7 +23,13 @@
* resource.
*/
public class MethodNotAllowedException extends StatusCodeException {
public MethodNotAllowedException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public MethodNotAllowedException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -23,7 +22,13 @@
* permissible.
*/
public class NotFoundException extends StatusCodeException {
public NotFoundException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public NotFoundException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -24,64 +23,61 @@
*/
public abstract class StatusCodeException extends ClientException {

private final HttpResponse<?> response;
private final HttpRequest request;
private final URI uri;
private final int statusCode;
private final String body;

/**
* Constructor.
* Constructs a new exception.
*
* @param request The http Request
* @param response The http Response
* @param response the response representing the exception
*/
public StatusCodeException(HttpRequest request, HttpResponse<String> response) {
super("httpMethod='" + request.method() + "',\n" +
"requestUri='" + request.uri() + "',\n" +
"ResponseUri='" + response.uri() + "',\n" +
"statusCode='" + response.statusCode() + "',\n" +
"requestBody=\n" + request.bodyPublisher().toString() + "',\n" +
"responseBody=\n" + response.body());

this.response = response;
this.request = request;
protected StatusCodeException(HttpResponse<String> response) {
this(response.uri(), response.statusCode(), response.body());
}


/**
* The URI that generated the failure response.
* Constructs a new exception.
*
* @return the URI that generated the failure response
* @param uri the uri called
* @param statusCode the status code received
* @param body the body of the response
*/
public URI getServiceUri() {
return response.uri();
protected StatusCodeException(URI uri, int statusCode, String body) {
super(String.format("Received HTTP status code %d (uri: %s uri, response body: %s)", statusCode, uri, body));
this.uri = uri;
this.statusCode = statusCode;
this.body = body;
}


/**
* The status code returned by the server.
* The URI that generated the failure response.
*
* @return the statusCode
* @return the URI that generated the failure response
*/
public int getStatusCode() {
return response.statusCode();
public URI getUri() {
return uri;
}


/**
* The content returned by the server.
* The status code returned by the server.
*
* @return the response body
* @return the statusCode
*/
public HttpResponse<?> getResponse() {
return response;
public int getStatusCode() {
return statusCode;
}


/**
* The content sent to the server.
* The body of the response.
*
* @return the response body
* @return the body of the response
*/
public HttpRequest getRequest() {
return request;
public String getBody() {
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


Expand All @@ -25,7 +24,13 @@
* resource.
*/
public class UnauthorizedException extends StatusCodeException {
public UnauthorizedException(HttpRequest request, HttpResponse<String> response) {
super(request, response);

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public UnauthorizedException(HttpResponse<String> response) {
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@
*/
package de.fraunhofer.iosb.ilt.faaast.client.exception;

import java.net.http.HttpRequest;
import java.net.URI;
import java.net.http.HttpResponse;


/**
* This exception is thrown if the server responds with an error code that is not handled by the client.
*/
public class UnsupportedStatusCodeException extends RuntimeException {
public UnsupportedStatusCodeException(HttpRequest request, HttpResponse<String> response) {
super("httpMethod='" + request.method() + "',\n" +
"requestUri='" + request.uri() + "',\n" +
"ResponseUri='" + response.uri() + "',\n" +
"statusCode='" + response.statusCode() + "',\n" +
"requestBody=\n" + request.bodyPublisher().toString() + "',\n" +
"responseBody=\n" + response.body());

/**
* Constructs a new exception.
*
* @param response the response representing the exception
*/
public UnsupportedStatusCodeException(HttpResponse<String> response) {
this(response.uri(), response.statusCode(), response.body());
}


/**
* Constructs a new exception.
*
* @param uri the uri called
* @param statusCode the status code received
* @param body the body of the response
*/
public UnsupportedStatusCodeException(URI uri, int statusCode, String body) {
super(String.format("Received HTTP status code %d (uri: %s uri, response body: %s)", statusCode, uri, body));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.fraunhofer.iosb.ilt.faaast.client.util;
package de.fraunhofer.iosb.ilt.faaast.client.http;

/**
* Enum describing supported HTTP methods.
Expand Down
Loading

0 comments on commit 846aacd

Please sign in to comment.