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

org.zodiac.sdk.nio.http.common.HttpProtocolVersion Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.http.common;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpProtocolVersion implements Comparable {

    private static final Pattern VERSION_PATTERN = Pattern.compile("(\\S+)/(\\d+)\\.(\\d+)");

    /**
     * HTTP/1.0
     */
    public static final String HTTP_1_0_STRING = "HTTP/1.0";
    /**
     * HTTP/1.1
     */
    public static final String HTTP_1_1_STRING = "HTTP/1.1";

    /**
     * HTTP/1.0
     */
    public static final HttpProtocolVersion HTTP_1_0 = new HttpProtocolVersion("HTTP", 1, 0, false, true);

    /**
     * HTTP/1.1
     */
    public static final HttpProtocolVersion HTTP_1_1 = new HttpProtocolVersion("HTTP", 1, 1, true, true);

    private final String protocolName;
    private final int majorVersion;
    private final int minorVersion;
    private final String text;
    private final boolean keepAliveDefault;

    protected HttpProtocolVersion(String text, boolean keepAliveDefault) {
        Objects.requireNonNull(text, "text");

        text = text.trim().toUpperCase();
        if (text.isEmpty()) {
            throw new IllegalArgumentException("empty text");
        }

        Matcher m = VERSION_PATTERN.matcher(text);
        if (!m.matches()) {
            throw new IllegalArgumentException("invalid version format: " + text);
        }

        protocolName = m.group(1);
        majorVersion = Integer.parseInt(m.group(2));
        minorVersion = Integer.parseInt(m.group(3));
        this.text = protocolName + '/' + majorVersion + '.' + minorVersion;
        this.keepAliveDefault = keepAliveDefault;
    }

    protected HttpProtocolVersion(
            String protocolName, int majorVersion, int minorVersion,
            boolean keepAliveDefault) {
        this(protocolName, majorVersion, minorVersion, keepAliveDefault, false);
    }

    private HttpProtocolVersion(
            String protocolName, int majorVersion, int minorVersion,
            boolean keepAliveDefault, boolean bytes) {
        Objects.requireNonNull(protocolName, "protocolName");

        protocolName = protocolName.trim().toUpperCase();
        if (protocolName.isEmpty()) {
            throw new IllegalArgumentException("empty protocolName");
        }

        for (int i = 0; i < protocolName.length(); i ++) {
            if (Character.isISOControl(protocolName.charAt(i)) ||
                    Character.isWhitespace(protocolName.charAt(i))) {
                throw new IllegalArgumentException("invalid character in protocolName");
            }
        }

        if (majorVersion < 0) {
            throw new IllegalArgumentException("majorVersion: " + majorVersion + " (expected: >= 0)");
        }
        if (minorVersion < 0) {
            throw new IllegalArgumentException("minorVersion: " + minorVersion + " (expected: >= 0)");
        }

        this.protocolName = protocolName;
        this.majorVersion = majorVersion;
        this.minorVersion = minorVersion;
        text = protocolName + '/' + majorVersion + '.' + minorVersion;
        this.keepAliveDefault = keepAliveDefault;
    }

    @Override
    public int compareTo(HttpProtocolVersion o) {
        int v = protocolName.compareTo(o.protocolName);
        if (v != 0) {
            return v;
        }

        v = majorVersion - o.majorVersion;
        if (v != 0) {
            return v;
        }

        return minorVersion - o.minorVersion;
    }

    public String getProtocolName() {
        return protocolName;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public int getMinorVersion() {
        return minorVersion;
    }

    public String getText() {
        return text;
    }

    public boolean isKeepAliveDefault() {
        return keepAliveDefault;
    }

//    public static HttpProtocolVersion getHttp10() {
//        return HttpProtovolVersionHolder.HTTP_1_0;
//    }
//
//    public static HttpProtocolVersion getHttp11() {
//        return HttpProtovolVersionHolder.HTTP_1_1;
//    }

    public static HttpProtocolVersion valueOf(String text) {
        Objects.requireNonNull(text, "text");

        text = text.trim();

        if (text.isEmpty()) {
            throw new IllegalArgumentException("text is empty (possibly HTTP/0.9)");
        }
        HttpProtocolVersion version = version0(text);
        if (version == null) {
            version = new HttpProtocolVersion(text, true);
        }
        return version;
    }

    private static HttpProtocolVersion version0(String text) {
        if (HTTP_1_1_STRING.equalsIgnoreCase(text)) {
            return HTTP_1_1;
        }
        if (HTTP_1_0_STRING.equalsIgnoreCase(text)) {
            return HTTP_1_0;
        }
        return null;
    }

    private static class HttpProtovolVersionHolder {
        /**
         * HTTP/1.0
         */
        public static final HttpProtocolVersion HTTP_1_0 = new HttpProtocolVersion("HTTP", 1, 0, false, true);

        /**
         * HTTP/1.1
         */
        public static final HttpProtocolVersion HTTP_1_1 = new HttpProtocolVersion("HTTP", 1, 1, true, true);
    }

    public static void main(String[] args) {
        System.out.println(HttpProtocolVersion.HTTP_1_1.getText());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy