java 中 code 与 reason 的包装

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package javax.websocket;

public class CloseReason {
    private final CloseReason.CloseCode closeCode;
    private final String reasonPhrase;

    public CloseReason(CloseReason.CloseCode closeCode, String reasonPhrase) {
        this.closeCode = closeCode;
        this.reasonPhrase = reasonPhrase;
    }

    public CloseReason.CloseCode getCloseCode() {
        return this.closeCode;
    }

    public String getReasonPhrase() {
        return this.reasonPhrase;
    }

    public String toString() {
        return "CloseReason: code [" + this.closeCode.getCode() + "], reason [" + this.reasonPhrase + "]";
    }

    public static enum CloseCodes implements CloseReason.CloseCode {
        NORMAL_CLOSURE(1000),
        GOING_AWAY(1001),
        PROTOCOL_ERROR(1002),
        CANNOT_ACCEPT(1003),
        RESERVED(1004),
        NO_STATUS_CODE(1005),
        CLOSED_ABNORMALLY(1006),
        NOT_CONSISTENT(1007),
        VIOLATED_POLICY(1008),
        TOO_BIG(1009),
        NO_EXTENSION(1010),
        UNEXPECTED_CONDITION(1011),
        SERVICE_RESTART(1012),
        TRY_AGAIN_LATER(1013),
        TLS_HANDSHAKE_FAILURE(1015);

        private int code;

        private CloseCodes(int code) {
            this.code = code;
        }

        public static CloseReason.CloseCode getCloseCode(final int code) {
            if (code > 2999 && code < 5000) {
                return new CloseReason.CloseCode() {
                    public int getCode() {
                        return code;
                    }
                };
            } else {
                switch(code) {
                case 1000:
                    return NORMAL_CLOSURE;
                case 1001:
                    return GOING_AWAY;
                case 1002:
                    return PROTOCOL_ERROR;
                case 1003:
                    return CANNOT_ACCEPT;
                case 1004:
                    return RESERVED;
                case 1005:
                    return NO_STATUS_CODE;
                case 1006:
                    return CLOSED_ABNORMALLY;
                case 1007:
                    return NOT_CONSISTENT;
                case 1008:
                    return VIOLATED_POLICY;
                case 1009:
                    return TOO_BIG;
                case 1010:
                    return NO_EXTENSION;
                case 1011:
                    return UNEXPECTED_CONDITION;
                case 1012:
                    return SERVICE_RESTART;
                case 1013:
                    return TRY_AGAIN_LATER;
                case 1014:
                default:
                    throw new IllegalArgumentException("Invalid close code: [" + code + "]");
                case 1015:
                    return TLS_HANDSHAKE_FAILURE;
                }
            }
        }

        public int getCode() {
            return this.code;
        }
    }

    public interface CloseCode {
        int getCode();
    }
}

参考自 apache tomcat websocket 源码~


转载请注明来源。 欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。 可以在下面评论区评论,也可以邮件至 sharlot2050@foxmail.com。

文章标题:java 中 code 与 reason 的包装

字数:272

本文作者:夏来风

发布时间:2020-10-18, 22:08:36

原始链接:http://www.demo1024.com/blog/java-msgdto/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。