Class BasicHttpResponse<SELF extends BasicHttpResponse<SELF>>

java.lang.Object
org.apache.juneau.http.response.BasicHttpResponse<SELF>
Type Parameters:
SELF - The self type for fluent setters.
All Implemented Interfaces:
HttpResponseMessage
Direct Known Subclasses:
Accepted, AlreadyReported, Continue, Created, EarlyHints, Found, IMUsed, MovedPermanently, MultipleChoices, MultiStatus, NoContent, NonAuthoritativeInformation, NotModified, Ok, PartialContent, PermanentRedirect, Processing, ResetContent, SeeOther, SwitchingProtocols, TemporaryRedirect, UseProxy

public abstract class BasicHttpResponse<SELF extends BasicHttpResponse<SELF>> extends Object implements HttpResponseMessage
Base class for concrete HTTP response objects in the org.apache.juneau.http stack.

Holds a status line, an optional body, and an ordered list of headers. Provides classic-style fluent setters that mutate state and return this.

Immutability is expressed with the "funnel + nested Unmodifiable snapshot" paradigm: every 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 self-typed (CRTP) root: a leaf declares class Ok extends BasicHttpResponse<Ok> and inherits leaf-typed fluent setters with no covariant overrides.

See Also:
Since:
9.2.1
  • Constructor Details

    • BasicHttpResponse

      protected BasicHttpResponse(HttpStatusLine statusLine)
      Constructor with no body.
      Parameters:
      statusLine - The status line. Must not be null.
    • BasicHttpResponse

      protected BasicHttpResponse(HttpStatusLine statusLine, HttpBody body)
      Constructor with a body.
      Parameters:
      statusLine - The status line. Must not be null.
      body - The response body. May be null.
    • BasicHttpResponse

      protected BasicHttpResponse(HttpStatusLine statusLine, String body)
      Constructor with a string body (UTF-8, text/plain).
      Parameters:
      statusLine - The status line. Must not be null.
      body - The response body as a plain-text string. May be null.
    • BasicHttpResponse

      protected BasicHttpResponse(HttpStatusLine statusLine, List<HttpHeader> headers, HttpBody body)
      Constructor with headers and an optional body.
      Parameters:
      statusLine - The status line. Must not be null.
      headers - The response headers. Can be null (treated as an empty list).
      body - The response body. May be null.
    • BasicHttpResponse

      protected BasicHttpResponse(BasicHttpResponse<?> copyFrom)
      Copy constructor.
      Parameters:
      copyFrom - The instance to copy. Must not be null.
  • Method Details

    • getStatusLine

      Description copied from interface: HttpResponseMessage
      Returns the status line of this response.
      Specified by:
      getStatusLine in interface HttpResponseMessage
      Returns:
      The status line. Never null.
    • getHeaders

      Description copied from interface: HttpResponseMessage
      Returns all response headers, in the order they were received.
      Specified by:
      getHeaders in interface HttpResponseMessage
      Returns:
      An unmodifiable list of response headers. Never null.
    • getBody

      public HttpBody getBody()
      Description copied from interface: HttpResponseMessage
      Returns the response body, or null if the response has no body.
      Specified by:
      getBody in interface HttpResponseMessage
      Returns:
      The response body, possibly null.
    • getStatusCode

      public int getStatusCode()
      Returns the HTTP status code from the status line.
      Returns:
      The status code.
    • getLocale

      public Locale getLocale()
      Returns the locale associated with this response, if any.
      Returns:
      The locale, possibly null.
    • setStatusLine

      Sets the status line.
      Parameters:
      value - New status line. Must not be null.
      Returns:
      This object.
    • setStatusCode

      public SELF setStatusCode(int value)
      Sets the status code, preserving the existing protocol version and reason phrase.
      Parameters:
      value - New status code.
      Returns:
      This object.
    • setReasonPhrase

      public SELF setReasonPhrase(String value)
      Sets the reason phrase, preserving the existing protocol version and status code.
      Parameters:
      value - New reason phrase. May be null.
      Returns:
      This object.
    • setProtocolVersion

      Sets the protocol version, preserving the existing status code and reason phrase.
      Parameters:
      value - New protocol version. Must not be null.
      Returns:
      This object.
    • setHeaders

      public SELF setHeaders(List<HttpHeader> values)
      Replaces all headers with the given list.
      Parameters:
      values - New headers. null is treated as an empty list.
      Returns:
      This object.
    • setHeaders

      public SELF setHeaders(HttpHeader... values)
      Replaces all headers with the given array.
      Parameters:
      values - New headers. null is treated as an empty array.
      Returns:
      This object.
    • setHeader

      public SELF setHeader(HttpHeader value)
      Sets a single header, replacing any existing header with the same name.
      Parameters:
      value - The header to set. null is ignored.
      Returns:
      This object.
    • setHeader

      public SELF setHeader(String name, String value)
      Sets a single header, replacing any existing header with the same name.
      Parameters:
      name - Header name. Must not be null.
      value - Header value. May be null.
      Returns:
      This object.
    • addHeader

      public SELF addHeader(HttpHeader value)
      Appends a header to the end of the header list.
      Parameters:
      value - The header to add. null is ignored.
      Returns:
      This object.
    • addHeader

      public SELF addHeader(String name, String value)
      Appends a header to the end of the header list.
      Parameters:
      name - Header name. Must not be null.
      value - Header value. May be null.
      Returns:
      This object.
    • setBody

      public SELF setBody(HttpBody value)
      Sets the response body.
      Parameters:
      value - New body. May be null.
      Returns:
      This object.
    • setContent

      public SELF setContent(String value)
      Sets the response body to a plain-text string body (UTF-8, text/plain).
      Parameters:
      value - Body text. May be null.
      Returns:
      This object.
    • setContent

      public SELF setContent(HttpBody value)
      Sets the response body.
      Parameters:
      value - New body. May be null.
      Returns:
      This object.
    • setLocale

      public SELF setLocale(Locale value)
      Sets the locale associated with this response.
      Parameters:
      value - New locale. May be null.
      Returns:
      This object.
    • setLocation

      public SELF setLocation(String value)
      Sets the value of the Location header.
      Parameters:
      value - The new location value. May be null.
      Returns:
      This object.
    • setLocation

      public SELF setLocation(URI value)
      Sets the value of the Location header.
      Parameters:
      value - The new location value. May be null.
      Returns:
      This object.
    • isUnmodifiable

      public boolean isUnmodifiable()
      Returns whether this instance is an unmodifiable snapshot.
      Returns:
      true if this bean is an UnmodifiableBean snapshot.
    • unmodifiable

      public abstract SELF unmodifiable()
      Returns an unmodifiable snapshot of this bean.

      The returned instance is a point-in-time copy of the leaf's nested Unmodifiable type; any attempt to set a property on it throws an UnsupportedOperationException. 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.
    • withBody

      public SELF withBody(HttpBody value)
      Sets the body on this response.
      Parameters:
      value - The new body. May be null.
      Returns:
      This object.
    • withBody

      public SELF withBody(String value)
      Sets the body on this response as a plain-text string.
      Parameters:
      value - The new body as a plain-text string. May be null.
      Returns:
      This object.
    • withHeader

      public SELF withHeader(HttpHeader header)
      Appends a header to this response.
      Parameters:
      header - The header to add. Must not be null.
      Returns:
      This object.
    • withHeader

      public SELF withHeader(String name, String value)
      Appends a header to this response.
      Parameters:
      name - The header name. Must not be null.
      value - The header value. May be null.
      Returns:
      This object.
    • self

      protected final SELF self()
      Returns this object cast to the self type.
      Returns:
      This object.
    • modify

      protected SELF modify(Runnable mutation)
      Single mutation funnel — the only choke-point through which all state changes on this bean flow.

      Every fluent setter routes through this method. The nested Unmodifiable snapshot overrides only this method to throw, which freezes the entire mutation surface with a single override.

      Parameters:
      mutation - The state change to apply.
      Returns:
      This object.
    • freeze

      protected void freeze()
      Deep-freeze hook invoked from the nested Unmodifiable constructor 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 (calling super.freeze() first).

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object