Class BasicHttpException
- All Implemented Interfaces:
Serializable,HttpMessage,HttpResponse
- Direct Known Subclasses:
BadRequest,BasicHttpException.Unmodifiable,Conflict,ExpectationFailed,FailedDependency,Forbidden,Gone,HttpVersionNotSupported,InsufficientStorage,InternalServerError,LengthRequired,Locked,LoopDetected,MethodNotAllowed,MisdirectedRequest,NetworkAuthenticationRequired,NotAcceptable,NotExtended,NotFound,NotImplemented,PayloadTooLarge,PreconditionFailed,PreconditionRequired,RangeNotSatisfiable,RequestHeaderFieldsTooLarge,ServiceUnavailable,TooManyRequests,Unauthorized,UnavailableForLegalReasons,UnprocessableEntity,UnsupportedMediaType,UpgradeRequired,UriTooLong,VariantAlsoNegotiates
HttpResponse interface for error responses.
Although this class implements the various setters defined on the HttpResponse interface, it's in general
going to be more efficient to set the status/headers/content of this bean through the builder.
Immutability is expressed with the "funnel + nested Unmodifiable snapshot" paradigm: every mutator (fluent
setter and void interface mutator) routes through the single protected modify(Runnable) choke-point,
and each concrete leaf gets a nested X.Unmodifiable whose only behavioral override is a throwing
modify(...). A leaf's unmodifiable() factory returns a point-in-time snapshot of type
X.Unmodifiable; any attempt to set a property on it throws an UnsupportedOperationException.
This is a Throwable-rooted hierarchy, so (unlike BasicHttpResponse) it cannot be a self-typed (CRTP)
root — JLS §8.1.2 forbids a generic Throwable. The funnel therefore returns the base type and each leaf
keeps its covariant setter overrides.
Notes:
- Beans are not thread safe unless they're marked as unmodifiable.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classUnmodifiable point-in-time snapshot of the enclosingBasicHttpException. -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructor.BasicHttpException(int statusCode) Constructor.BasicHttpException(int statusCode, String msg, Object... args) Constructor.BasicHttpException(int statusCode, Throwable causedBy) Constructor.BasicHttpException(int statusCode, Throwable cause, String msg, Object... args) Constructor.BasicHttpException(HttpResponse response) Constructor.protectedBasicHttpException(BasicHttpException copyFrom) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidprotected voidassertStatusCode(HttpResponse response) Asserts that the specified HTTP response has the same status code as the one on the status line of this bean.booleancontainsHeader(String name) booleanprotected voidfreeze()Deep-freeze hook invoked from the nestedUnmodifiableconstructor after the snapshot copy completes.Header[]getFirstHeader(String name) getFullStackMessage(boolean scrubForXssVulnerabilities) Returns all error messages from all errors in this stack.Returns access to the underlying builder for the headers.Header[]getHeaders(String name) getLastHeader(String name) Returns the root cause of this exception.inthashCode()headerIterator(String name) booleanReturnstrue if this bean is unmodifiable.protected BasicHttpExceptionSingle mutation funnel — the only choke-point through which all state changes on this bean flow.voidremoveHeader(Header value) voidremoveHeaders(String name) setContent(String value) Sets the body on this response.setContent(HttpEntity value) Sets the body on this response.voidsetEntity(HttpEntity entity) voidvoidsetHeader2(String name, Object value) Sets a header on this response.setHeaders(List<Header> values) Sets the specified headers on this response.voidsetHeaders(Header[] values) setHeaders(HeaderList value) Sets the specified headers on this response.setHeaders2(Header... values) Sets multiple headers on this response.voidsetLocale2(Locale value) Sets the locale used to retrieve reason phrases.setMessage(String message, Object... args) Sets the detail message on this exception.voidsetParams(HttpParams params) Sets the protocol version on the status line.voidsetReasonPhrase(String reason) setReasonPhrase2(String value) Sets the reason phrase on the status line.Sets the reason phrase catalog used to retrieve reason phrases.voidsetStatusCode(int code) setStatusCode2(int code) Same assetStatusCode(int)but returns this object.voidsetStatusLine(ProtocolVersion ver, int code) voidsetStatusLine(ProtocolVersion ver, int code, String reason) voidsetStatusLine(StatusLine value) setStatusLine(BasicStatusLine value) Sets the protocol version on the status line.toString()Returns an unmodifiable snapshot of this bean.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
Constructor Details
-
BasicHttpException
public BasicHttpException()Constructor. -
BasicHttpException
Constructor.This is the constructor used when parsing an HTTP response.
- Parameters:
response- The HTTP response being parsed. Must not benull .
-
BasicHttpException
Constructor.- Parameters:
statusCode- The HTTP status code.
-
BasicHttpException
Constructor.- Parameters:
statusCode- The HTTP status code.msg- The message. Can benull , in which case the reason phrase is used as the message.args- Optionalprintf -style ("%s" ) arguments in the message.
-
BasicHttpException
Constructor.- Parameters:
statusCode- The HTTP status code.causedBy- The cause. Can benull , in which case no cause is chained and the reason phrase is used as the message.
-
BasicHttpException
Constructor.- Parameters:
statusCode- The HTTP status code.cause- The caused-by exception. Can benull (no cause is chained).msg- The message. Can benull , in which case the cause's message is used, or the reason phrase if the cause is alsonull or has no message.args- The message arguments.
-
BasicHttpException
Copy constructor.The mutable sub-beans are deep-copied by direct field assignment, never through a funneled setter. This is the snapshot path used by
unmodifiable(): the constructor runs inside anUnmodifiableinstance whosemodify(Runnable)already throws, so a setter-based copy would blow up during construction.- Parameters:
copyFrom- The bean to copy. Must not benull .
-
-
Method Details
-
addHeader
- Specified by:
addHeaderin interfaceHttpMessage
-
addHeader
- Specified by:
addHeaderin interfaceHttpMessage
-
containsHeader
- Specified by:
containsHeaderin interfaceHttpMessage
-
getAllHeaders
- Specified by:
getAllHeadersin interfaceHttpMessage
-
getEntity
- Specified by:
getEntityin interfaceHttpResponse
-
getFirstHeader
- Specified by:
getFirstHeaderin interfaceHttpMessage
-
getFullStackMessage
Returns all error messages from all errors in this stack.Typically useful if you want to render all the error messages in the stack, but don't want to render all the stack traces too.
- Parameters:
scrubForXssVulnerabilities- Iftrue , replaces'<' ,'>' , and'&' characters with spaces.- Returns:
- All error messages from all errors in this stack.
-
getHeaders
Returns access to the underlying builder for the headers.- Returns:
- The underlying builder for the headers.
-
getHeaders
- Specified by:
getHeadersin interfaceHttpMessage
-
getLastHeader
- Specified by:
getLastHeaderin interfaceHttpMessage
-
getLocale
- Specified by:
getLocalein interfaceHttpResponse
-
getMessage
- Overrides:
getMessagein classBasicRuntimeException
-
getParams
- Specified by:
getParamsin interfaceHttpMessage
-
getProtocolVersion
- Specified by:
getProtocolVersionin interfaceHttpMessage
-
getRootCause
Returns the root cause of this exception.The root cause is the first exception in the init-cause parent chain that's not one of the following:
- Returns:
- The root cause of this exception, or
null if no root cause was found.
-
getStatusLine
- Specified by:
getStatusLinein interfaceHttpResponse
-
equals
-
hashCode
-
headerIterator
- Specified by:
headerIteratorin interfaceHttpMessage
-
headerIterator
- Specified by:
headerIteratorin interfaceHttpMessage
-
isUnmodifiable
Returnstrue if this bean is unmodifiable.- Returns:
true if this bean is anUnmodifiableBeansnapshot.
-
removeHeader
- Specified by:
removeHeaderin interfaceHttpMessage
-
removeHeaders
- Specified by:
removeHeadersin interfaceHttpMessage
-
setContent
Sets the body on this response.- Parameters:
value- The body on this response. Can benull to unset the property.- Returns:
- This object.
-
setContent
Sets the body on this response.- Parameters:
value- The body on this response. Can benull (treated as an empty string).- Returns:
- This object.
-
setEntity
- Specified by:
setEntityin interfaceHttpResponse
-
setHeader
- Specified by:
setHeaderin interfaceHttpMessage
-
setHeader
- Specified by:
setHeaderin interfaceHttpMessage
-
setHeader2
Sets a header on this response.- Parameters:
name- The header name.value- The header value.- Returns:
- This object.
-
setHeaders
- Specified by:
setHeadersin interfaceHttpMessage
-
setHeaders
Sets the specified headers on this response.- Parameters:
value- The new value. Must not benull .- Returns:
- This object.
-
setHeaders
Sets the specified headers on this response.- Parameters:
values- The headers to set.null values are ignored.- Returns:
- This object.
-
setHeaders2
Sets multiple headers on this response.- Parameters:
values- The headers to add.- Returns:
- This object.
-
setLocale
- Specified by:
setLocalein interfaceHttpResponse
-
setLocale2
Sets the locale used to retrieve reason phrases.If not specified, uses
Locale.getDefault().- Parameters:
value- The new value. Can benull . The effect is implementation-dependent, depending on the configuredReasonPhraseCatalog(e.g. ignored byEnglishReasonPhraseCatalog).- Returns:
- This object.
-
setMessage
Description copied from class:BasicRuntimeExceptionSets the detail message on this exception.- Overrides:
setMessagein classBasicRuntimeException- Parameters:
message- The message.args- The message args.- Returns:
- This object.
-
setParams
- Specified by:
setParamsin interfaceHttpMessage
-
setProtocolVersion
Sets the protocol version on the status line.If not specified,
"HTTP/1.1" will be used.- Parameters:
value- The new value. Can benull , in which case the default protocol version is cleared andBasicStatusLine.getProtocolVersion()returnsnull .- Returns:
- This object.
-
setReasonPhrase
- Specified by:
setReasonPhrasein interfaceHttpResponse- Throws:
IllegalStateException
-
setReasonPhrase2
Sets the reason phrase on the status line.If not specified, the reason phrase will be retrieved from the reason phrase catalog using the locale on this builder.
- Parameters:
value- The new value. Can benull to fall back to the reason phrase catalog.- Returns:
- This object.
-
setReasonPhraseCatalog
Sets the reason phrase catalog used to retrieve reason phrases.If not specified, uses
EnglishReasonPhraseCatalog.- Parameters:
value- The new value. Can benull to fall back toEnglishReasonPhraseCatalog.INSTANCE.- Returns:
- This object.
-
setStatusCode
- Specified by:
setStatusCodein interfaceHttpResponse- Throws:
IllegalStateException
-
setStatusCode2
Same assetStatusCode(int)but returns this object.- Parameters:
code- The new status code.- Returns:
- This object.
- Throws:
IllegalStateException- If status code could not be set.
-
setStatusLine
Sets the protocol version on the status line.If not specified,
"HTTP/1.1" will be used.- Parameters:
value- The new value. Must not benull .- Returns:
- This object.
-
setStatusLine
- Specified by:
setStatusLinein interfaceHttpResponse
-
setStatusLine
- Specified by:
setStatusLinein interfaceHttpResponse
-
setStatusLine
- Specified by:
setStatusLinein interfaceHttpResponse
-
unmodifiable
Returns an unmodifiable snapshot of this bean.The returned instance is a point-in-time copy of the leaf's nested
Unmodifiabletype; any attempt to set a property on it throws anUnsupportedOperationException. This method is idempotent: if this bean is already unmodifiable it returns itself rather than taking another snapshot. The receiver is left unchanged (and still modifiable unless it was already unmodifiable).- Returns:
- An unmodifiable snapshot of this bean, or this bean if it is already unmodifiable.
-
toString
-
assertStatusCode
Asserts that the specified HTTP response has the same status code as the one on the status line of this bean.- Parameters:
response- The HTTP response to check. Must not benull .- Throws:
IllegalArgumentException- If status code is not what was expected.
-
modify
Single mutation funnel — the only choke-point through which all state changes on this bean flow.Every mutator (fluent setter and
voidinterface mutator) routes through this method. The nestedUnmodifiablesnapshot overrides only this method to throw, which freezes the entire mutation surface with a single override. Because this is aThrowable-rooted hierarchy (no CRTP self-type), the funnel returns the base type.- Parameters:
mutation- The state change to apply.- Returns:
- This object.
-
freeze
Deep-freeze hook invoked from the nestedUnmodifiableconstructor after the snapshot copy completes.The mutable sub-beans are frozen here by direct field assignment (never via a setter, which would route through the throwing
modify(Runnable)and blow up during construction). A leaf that adds its own mutable sub-bean overrides this method (callingsuper.freeze()first).
-