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

io.higgs.http.client.Response Maven / Gradle / Ivy

package io.higgs.http.client;

import io.higgs.http.client.readers.Reader;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**
 * @author Courtney Robinson 
 */
public class Response {
    protected final Request request;
    protected boolean failed;
    protected Throwable cause;
    protected Reader reader;
    private boolean chunked;
    private HttpVersion protocolVersion;
    private HttpResponseStatus status;
    private HttpHeaders headers;
    private boolean completed;

    public Response(Request request, Reader reader) {
        this.reader = reader;
        this.request = request;
        reader.response(this);
    }

    public boolean isChunked() {
        return chunked;
    }

    public void setChunked(boolean chunked) {
        this.chunked = chunked;
    }

    public HttpVersion getProtocolVersion() {
        return protocolVersion;
    }

    public void setProtocolVersion(HttpVersion protocolVersion) {
        this.protocolVersion = protocolVersion;
        reader.onProtocolVersion(protocolVersion);
    }

    public HttpResponseStatus getStatus() {
        return status;
    }

    public void setStatus(HttpResponseStatus status) {
        this.status = status;
        reader.onStatus(status);
    }

    public HttpHeaders getHeaders() {
        return headers;
    }

    public void setHeaders(HttpHeaders headers) {
        this.headers = headers;
        reader.onHeaders(headers);
    }

    public void write(ByteBuf content) {
        if (content != null) {
            reader.data(content);
        }
    }

    /**
     * Mark the request as failed, this will set this response as completed
     * and notify all listeners that the request failed
     *
     * @param cause an optional exception that may have been the cause of the failure
     */
    public void markFailed(Throwable cause) {
        this.failed = true;
        this.cause = cause;
        setCompleted(true);
    }

    /**
     * @return true if the request for this response has failed for some reason
     */
    public boolean hasFailed() {
        return failed;
    }

    /**
     * @return If the request has failed, this returns the reason for the failure
     * May* be null
     */
    public Throwable failureCause() {
        return cause;
    }

    public boolean isCompleted() {
        return completed;
    }

    public void setCompleted(boolean completed) {
        this.completed = completed;
        reader.setCompleted(completed);
    }

    @Override
    public String toString() {
        return "Response{" +
                "chunked=" + chunked +
                ", protocolVersion=" + protocolVersion +
                ", status=" + status +
                ", headers=" + headers +
                ", completed=" + completed +
                ", reader=" + reader +
                ", \nrequest=" + request +
                '}';
    }

    /**
     * @return The request which generated this response
     */
    public Request request() {
        return request;
    }

    /**
     * @return true if this response is the result of following a redirect
     */
    public boolean isRedirected() {
        return request.originalUri() != null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy