All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.http.EasimartHttpResponse Maven / Gradle / Ivy

package co.easimart.http;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * The http response we receive from parse server. Instances of this class are not immutable. The
 * response body may be consumed only once. The other fields are immutable.
 */
public final class EasimartHttpResponse {

  /**
   * Builder for {@code EasimartHttpResponse}.
   */
  public static final class Builder {

    private int statusCode;
    private InputStream content;
    private long totalSize;
    private String reasonPhrase;
    private Map headers;
    private String contentType;

    /**
     * Creates an empty {@code Builder}.
     */
    public Builder() {
      this.totalSize = -1;
      this.headers = new HashMap<>();
    }

    /**
     * Makes a new {@code Builder} based on the given {@code EasimartHttpResponse}.
     *
     * @param response
     *          The {@code EasimartHttpResponse} where the {@code Builder}'s values come from.
     */
    public Builder(EasimartHttpResponse response) {
      super();
      this.setStatusCode(response.getStatusCode());
      this.setContent(response.getContent());
      this.setTotalSize(response.getTotalSize());
      this.setContentType(response.getContentType());
      this.setHeaders(response.getAllHeaders());
      this.setReasonPhrase(response.getReasonPhrase());
    }

    /**
     * Sets the status code of this {@code Builder}.
     *
     * @param statusCode
     *          The status code of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setStatusCode(int statusCode) {
      this.statusCode = statusCode;
      return this;
    }

    /**
     * Sets the content of this {@code Builder}.
     *
     * @param content
     *          The content of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setContent(InputStream content) {
      this.content = content;
      return this;
    }

    /**
     * Sets the total size of this {@code Builder}.
     *
     * @param totalSize
     *          The total size of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setTotalSize(long totalSize) {
      this.totalSize = totalSize;
      return this;
    }

    /**
     * Sets the reason phrase of this {@code Builder}.
     *
     * @param reasonPhrase
     *          The reason phrase of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setReasonPhrase(String reasonPhrase) {
      this.reasonPhrase = reasonPhrase;
      return this;
    }

    /**
     * Sets headers of this {@code Builder}. All existing headers will be cleared.
     *
     * @param headers
     *          The headers of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setHeaders(Map headers) {
      this.headers = new HashMap<>(headers);
      return this;
    }

    /**
     * Adds headers to this {@code Builder}.
     *
     * @param headers
     *          The headers that need to be added.
     * @return This {@code Builder}.
     */
    public Builder addHeaders(Map headers) {
      this.headers.putAll(headers);
      return this;
    }

    /**
     * Adds a header to this {@code Builder}.
     *
     * @param name
     *          The name of the header.
     * @param value
     *          The value of the header.
     * @return This {@code Builder}.
     */
    public Builder addHeader(String name, String value) {
      headers.put(name, value);
      return this;
    }

    /**
     * Sets the content type of this {@code Builder}.
     *
     * @param contentType
     *          The {@code Content-Type} of this {@code Builder}.
     * @return This {@code Builder}.
     */
    public Builder setContentType(String contentType) {
      this.contentType = contentType;
      return this;
    }

    /**
     * Builds a {@link EasimartHttpResponse} by this {@code Builder}.
     *
     * @return A {@link EasimartHttpResponse} built on this {@code Builder}.
     */
    public EasimartHttpResponse build() {
      return new EasimartHttpResponse(this);
    }
  }

  private final int statusCode;
  private final InputStream content;
  private final long totalSize;
  private final String reasonPhrase;
  private final Map headers;
  private final String contentType;

  private EasimartHttpResponse(Builder builder) {
    this.statusCode = builder.statusCode;
    this.content = builder.content;
    this.totalSize = builder.totalSize;
    this.reasonPhrase = builder.reasonPhrase;
    this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers));
    this.contentType = builder.contentType;
  }

  /**
   * Gets the status code of this {@code EasimartHttpResponse}.
   *
   * @return The status code of this {@code EasimartHttpResponse}.
   */
  public int getStatusCode() {
    return statusCode;
  }

  /**
   * Returns the content of the {@code EasimartHttpResponse}'s body. The content can only
   * be read once and can't be reset.
   *
   * @return The content of the {@code EasimartHttpResponse}'s body.
   */
  public InputStream getContent() {
    return content;
  }

  /**
   * Returns the size of the {@code EasimartHttpResponse}'s body. {@code -1} if the size of the
   * {@code EasimartHttpResponse}'s body is unknown.
   *
   * @return The size of the {@code EasimartHttpResponse}'s body.
   */
  public long getTotalSize() {
    return totalSize;
  }

  /**
   * Gets the reason phrase of this {@code EasimartHttpResponse}.
   *
   * @return The reason phrase of this {@code EasimartHttpResponse}.
   */
  public String getReasonPhrase() {
    return reasonPhrase;
  }

  /**
   * Gets the {@code Content-Type} of this {@code EasimartHttpResponse}.
   *
   * @return The {@code Content-Type} of this {@code EasimartHttpResponse}.
   */
  public String getContentType() {
    return contentType;
  }

  /**
   * Retrieves the header value from this {@code EasimartHttpResponse} by the given header name.
   *
   * @param name
   *          The name of the header.
   * @return The value of the header.
   */
  public String getHeader(String name) {
    return headers.get(name);
  }

  /**
   * Gets all headers from this {@code EasimartHttpResponse}.
   *
   * @return The headers of this {@code EasimartHttpResponse}.
   */
  public Map getAllHeaders() {
    return headers;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy