Class RFC9457Reporter<T,E extends java.lang.Exception,R>
- java.lang.Object
-
- org.eclipse.aether.spi.connector.transport.http.RFC9457.RFC9457Reporter<T,E,R>
-
- Type Parameters:
T- The type of the response.E- The base exception type to throw if the response is not a RFC9457 message.R- The type of the request or request builder (which allows to modify headers)
public abstract class RFC9457Reporter<T,E extends java.lang.Exception,R> extends java.lang.Object
A reporter for RFC 9457 messages. RFC 9457 is a standard for reporting problems in HTTP responses as a JSON object. There are members specified in the RFC but none of those appear to be required, see rfc9457 section 3.7 Given the JSON fields are not mandatory, this reporter simply extracts the body of the response without validation. A RFC 9457 message is detected by the content type "application/problem+json" in the response header.- See Also:
- RFC 9457
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCONTENT_TYPE_PROBLEM_DETAILS_JSONstatic java.lang.StringCONTENT_TYPE_PROBLEM_DETAILS_JSON_AND_ANYstatic intMAX_BODY_BYTESMaximum number of bytes read from an error response body.
-
Constructor Summary
Constructors Constructor Description RFC9457Reporter()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidgenerateException(T response, BiConsumerChecked<java.lang.Integer,java.lang.String,E> baseException)Generates aHttpRFC9457Exceptionif the response type is a RFC 9457 message.protected abstract java.lang.StringgetBody(T response)protected abstract java.lang.StringgetReasonPhrase(T response)protected abstract intgetStatusCode(T response)protected booleanhasRFC9457ContentType(java.lang.String contentType)protected abstract booleanisRFC9457Message(T response)abstract voidprepareRequest(R request)Prepares the request to accept RFC 9457 responses.protected static java.lang.StringreadBody(java.io.InputStream is)Reads the given stream into a UTF-8 string, consuming at mostMAX_BODY_BYTESbytes.
-
-
-
Field Detail
-
CONTENT_TYPE_PROBLEM_DETAILS_JSON
public static final java.lang.String CONTENT_TYPE_PROBLEM_DETAILS_JSON
- See Also:
- Constant Field Values
-
CONTENT_TYPE_PROBLEM_DETAILS_JSON_AND_ANY
public static final java.lang.String CONTENT_TYPE_PROBLEM_DETAILS_JSON_AND_ANY
- See Also:
- Constant Field Values
-
MAX_BODY_BYTES
public static final int MAX_BODY_BYTES
Maximum number of bytes read from an error response body. Legitimate RFC 9457 payloads are tiny, while the body of an error response is attacker influenced data (the content type header alone triggers consumption) and may be transparently decompressed by the transport, so it must never be buffered unbounded. Bodies larger than this limit are truncated, not rejected.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RFC9457Reporter
public RFC9457Reporter()
-
-
Method Detail
-
isRFC9457Message
protected abstract boolean isRFC9457Message(T response)
-
getStatusCode
protected abstract int getStatusCode(T response)
-
getReasonPhrase
protected abstract java.lang.String getReasonPhrase(T response)
-
getBody
protected abstract java.lang.String getBody(T response) throws java.io.IOException
- Throws:
java.io.IOException
-
prepareRequest
public abstract void prepareRequest(R request)
Prepares the request to accept RFC 9457 responses. This involves setting/updating the "Accept" header to include "application/problem+json".- Parameters:
request- The request or request builder to prepare- See Also:
- RFC 9457 section 3.2
-
readBody
protected static java.lang.String readBody(java.io.InputStream is) throws java.io.IOException
Reads the given stream into a UTF-8 string, consuming at mostMAX_BODY_BYTESbytes. Any remaining bytes are left unread, so an oversized (or decompression amplified) body is truncated instead of exhausting memory. The caller remains responsible for closing the stream.- Parameters:
is- The stream to read the body from, must not benull.- Returns:
- The body as UTF-8 string, truncated to
MAX_BODY_BYTESbytes, nevernull. - Throws:
java.io.IOException- If reading the stream fails.
-
hasRFC9457ContentType
protected boolean hasRFC9457ContentType(java.lang.String contentType)
-
generateException
public void generateException(T response, BiConsumerChecked<java.lang.Integer,java.lang.String,E> baseException) throws E extends java.lang.Exception, HttpRFC9457Exception
Generates aHttpRFC9457Exceptionif the response type is a RFC 9457 message. Otherwise, it throws the base exception- Parameters:
response- The response to check for RFC 9457 messages.baseException- The base exception to throw if the response is not a RFC 9457 message.- Throws:
E extends java.lang.ExceptionHttpRFC9457Exception
-
-