xref: /unit/src/java/javax/websocket/CloseReason.java (revision 1157:7ae152bda303)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package javax.websocket;
18 
19 public class CloseReason {
20 
21     private final CloseCode closeCode;
22     private final String reasonPhrase;
23 
CloseReason(CloseReason.CloseCode closeCode, String reasonPhrase)24     public CloseReason(CloseReason.CloseCode closeCode, String reasonPhrase) {
25         this.closeCode = closeCode;
26         this.reasonPhrase = reasonPhrase;
27     }
28 
getCloseCode()29     public CloseCode getCloseCode() {
30         return closeCode;
31     }
32 
getReasonPhrase()33     public String getReasonPhrase() {
34         return reasonPhrase;
35     }
36 
37     @Override
toString()38     public String toString() {
39         return "CloseReason: code [" + closeCode.getCode() +
40                 "], reason [" + reasonPhrase + "]";
41     }
42 
43     public interface CloseCode {
getCode()44         int getCode();
45     }
46 
47     public enum CloseCodes implements CloseReason.CloseCode {
48 
49         NORMAL_CLOSURE(1000),
50         GOING_AWAY(1001),
51         PROTOCOL_ERROR(1002),
52         CANNOT_ACCEPT(1003),
53         RESERVED(1004),
54         NO_STATUS_CODE(1005),
55         CLOSED_ABNORMALLY(1006),
56         NOT_CONSISTENT(1007),
57         VIOLATED_POLICY(1008),
58         TOO_BIG(1009),
59         NO_EXTENSION(1010),
60         UNEXPECTED_CONDITION(1011),
61         SERVICE_RESTART(1012),
62         TRY_AGAIN_LATER(1013),
63         TLS_HANDSHAKE_FAILURE(1015);
64 
65         private int code;
66 
CloseCodes(int code)67         CloseCodes(int code) {
68             this.code = code;
69         }
70 
getCloseCode(final int code)71         public static CloseCode getCloseCode(final int code) {
72             if (code > 2999 && code < 5000) {
73                 return new CloseCode() {
74                     @Override
75                     public int getCode() {
76                         return code;
77                     }
78                 };
79             }
80             switch (code) {
81                 case 1000:
82                     return CloseCodes.NORMAL_CLOSURE;
83                 case 1001:
84                     return CloseCodes.GOING_AWAY;
85                 case 1002:
86                     return CloseCodes.PROTOCOL_ERROR;
87                 case 1003:
88                     return CloseCodes.CANNOT_ACCEPT;
89                 case 1004:
90                     return CloseCodes.RESERVED;
91                 case 1005:
92                     return CloseCodes.NO_STATUS_CODE;
93                 case 1006:
94                     return CloseCodes.CLOSED_ABNORMALLY;
95                 case 1007:
96                     return CloseCodes.NOT_CONSISTENT;
97                 case 1008:
98                     return CloseCodes.VIOLATED_POLICY;
99                 case 1009:
100                     return CloseCodes.TOO_BIG;
101                 case 1010:
102                     return CloseCodes.NO_EXTENSION;
103                 case 1011:
104                     return CloseCodes.UNEXPECTED_CONDITION;
105                 case 1012:
106                     return CloseCodes.SERVICE_RESTART;
107                 case 1013:
108                     return CloseCodes.TRY_AGAIN_LATER;
109                 case 1015:
110                     return CloseCodes.TLS_HANDSHAKE_FAILURE;
111                 default:
112                     throw new IllegalArgumentException(
113                             "Invalid close code: [" + code + "]");
114             }
115         }
116 
117         @Override
getCode()118         public int getCode() {
119             return code;
120         }
121     }
122 }
123