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

io.elastic.api.HttpReply Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
package io.elastic.api;

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

public final class HttpReply {

    private int status;
    private Map headers = new HashMap();
    private InputStream content;

    private HttpReply(final int status,
                      final InputStream content,
                      final Map headers) {
        if (content == null) {
            throw new IllegalArgumentException("HttpReply content must not be null");
        }
        if (headers == null) {
            throw new IllegalArgumentException("HttpReply headers must not be null");
        }
        this.status = status;
        this.content = content;
        this.headers.putAll(headers);
    }

    public int getStatus() {
        return status;
    }

    public Map getHeaders() {
        return headers;
    }

    public InputStream getContent() {
        return content;
    }

    @Override
    public String toString() {
        return "HttpReply{" +
                "status=" + status +
                ", headers=" + headers +
                ", content=" + content +
                '}';
    }

    public static final class Builder {
        private int status;
        private InputStream content;
        private Map headers = new HashMap();

        public Builder() {
        }

        public Builder status(int statusCode) {
            this.status = statusCode;
            return this;
        }

        public Builder header(final String name, final String value) {
            this.headers.put(name, value);

            return this;
        }

        public Builder content(final InputStream content) {
            if (content == null) {
                throw new IllegalArgumentException("Content must not be null");
            }
            this.content = content;

            return this;
        }

        public Builder status(final Status status) {
            if (status == null) {
                throw new IllegalArgumentException("Status must not be null");
            }
            return status(status.getStatusCode());
        }

        public HttpReply build() {
            return new HttpReply(this.status, this.content, this.headers);
        }
    }

    public enum Status {

        /**
         * 200 OK, see HTTP/1.1 documentation.
         */
        OK(200, "OK"),
        /**
         * 201 Created, see HTTP/1.1 documentation.
         */
        CREATED(201, "Created"),
        /**
         * 202 Accepted, see HTTP/1.1 documentation.
         */
        ACCEPTED(202, "Accepted"),
        /**
         * 204 No Content, see HTTP/1.1 documentation.
         */
        NO_CONTENT(204, "No Content"),
        /**
         * 205 Reset Content, see HTTP/1.1 documentation.
         */
        RESET_CONTENT(205, "Reset Content"),
        /**
         * 206 Reset Content, see HTTP/1.1 documentation.
         */
        PARTIAL_CONTENT(206, "Partial Content"),
        /**
         * 301 Moved Permanently, see HTTP/1.1 documentation.
         */
        MOVED_PERMANENTLY(301, "Moved Permanently"),
        /**
         * 302 Found, see HTTP/1.1 documentation.
         */
        FOUND(302, "Found"),
        /**
         * 303 See Other, see HTTP/1.1 documentation.
         */
        SEE_OTHER(303, "See Other"),
        /**
         * 304 Not Modified, see HTTP/1.1 documentation.
         */
        NOT_MODIFIED(304, "Not Modified"),
        /**
         * 305 Use Proxy, see HTTP/1.1 documentation.
         */
        USE_PROXY(305, "Use Proxy"),
        /**
         * 307 Temporary Redirect, see HTTP/1.1 documentation.
         */
        TEMPORARY_REDIRECT(307, "Temporary Redirect"),
        /**
         * 400 Bad Request, see HTTP/1.1 documentation.
         */
        BAD_REQUEST(400, "Bad Request"),
        /**
         * 401 Unauthorized, see HTTP/1.1 documentation.
         */
        UNAUTHORIZED(401, "Unauthorized"),
        /**
         * 402 Payment Required, see HTTP/1.1 documentation.
         */
        PAYMENT_REQUIRED(402, "Payment Required"),
        /**
         * 403 Forbidden, see HTTP/1.1 documentation.
         */
        FORBIDDEN(403, "Forbidden"),
        /**
         * 404 Not Found, see HTTP/1.1 documentation.
         */
        NOT_FOUND(404, "Not Found"),
        /**
         * 405 Method Not Allowed, see HTTP/1.1 documentation.
         */
        METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
        /**
         * 406 Not Acceptable, see HTTP/1.1 documentation.
         */
        NOT_ACCEPTABLE(406, "Not Acceptable"),
        /**
         * 407 Proxy Authentication Required, see HTTP/1.1 documentation.
         */
        PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
        /**
         * 408 Request Timeout, see HTTP/1.1 documentation.
         */
        REQUEST_TIMEOUT(408, "Request Timeout"),
        /**
         * 409 Conflict, see HTTP/1.1 documentation.
         */
        CONFLICT(409, "Conflict"),
        /**
         * 410 Gone, see HTTP/1.1 documentation.
         */
        GONE(410, "Gone"),
        /**
         * 411 Length Required, see HTTP/1.1 documentation.
         */
        LENGTH_REQUIRED(411, "Length Required"),
        /**
         * 412 Precondition Failed, see HTTP/1.1 documentation.
         */
        PRECONDITION_FAILED(412, "Precondition Failed"),
        /**
         * 413 Request Entity Too Large, see HTTP/1.1 documentation.
         */
        REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),
        /**
         * 414 Request-URI Too Long, see HTTP/1.1 documentation.
         */
        REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"),
        /**
         * 415 Unsupported Media Type, see HTTP/1.1 documentation.
         */
        UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
        /**
         * 416 Requested Range Not Satisfiable, see HTTP/1.1 documentation.
         */
        REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"),
        /**
         * 417 Expectation Failed, see HTTP/1.1 documentation.0
         */
        EXPECTATION_FAILED(417, "Expectation Failed"),
        /**
         * 500 Internal Server Error, see HTTP/1.1 documentation.
         */
        INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
        /**
         * 501 Not Implemented, see HTTP/1.1 documentation.
         */
        NOT_IMPLEMENTED(501, "Not Implemented"),
        /**
         * 502 Bad Gateway, see HTTP/1.1 documentation.
         */
        BAD_GATEWAY(502, "Bad Gateway"),
        /**
         * 503 Service Unavailable, see HTTP/1.1 documentation.
         */
        SERVICE_UNAVAILABLE(503, "Service Unavailable"),
        /**
         * 504 Gateway Timeout, see HTTP/1.1 documentation.
         */
        GATEWAY_TIMEOUT(504, "Gateway Timeout"),
        /**
         * 505 HTTP Version Not Supported, see HTTP/1.1 documentation.
         */
        HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported");
        private final int code;
        private final String reason;

        private Status(final int statusCode, final String reason) {
            this.code = statusCode;
            this.reason = reason;
        }

        /**
         * Get the associated status code.
         *
         * @return the status code.
         */
        public int getStatusCode() {
            return code;
        }

        /**
         * Get the reason phrase.
         *
         * @return the reason phrase.
         */
        public String getReason() {
            return toString();
        }

        @Override
        public String toString() {
            return "Status{" +
                    "code=" + code +
                    ", reason='" + reason + '\'' +
                    '}';
        }

        /**
         * Convert a numerical status code into the corresponding Status.
         *
         * @param statusCode the numerical status code.
         * @return the matching Status or null is no matching Status is defined.
         */
        public static Status fromStatusCode(final int statusCode) {
            for (Status s : Status.values()) {
                if (s.code == statusCode) {
                    return s;
                }
            }
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy