Skip to content

Package: CloseReason$CloseCodes

CloseReason$CloseCodes

nameinstructionbranchcomplexitylinemethod
CloseReason.CloseCodes(String, int, int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCloseCode(int)
M: 55 C: 0
0%
M: 20 C: 0
0%
M: 18 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
getCode()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 169 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2018, 2019 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package jakarta.websocket;
19:
20: import java.io.UnsupportedEncodingException;
21:
22: /**
23: * A class encapsulating the reason why a web socket has been closed, or why it is being asked to close. Note the
24: * acceptable uses of codes and reason phrase are defined in more detail by
25: * <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>.
26: *
27: * @author dannycoward
28: */
29: public class CloseReason {
30:
31: private final CloseReason.CloseCode closeCode;
32: private final String reasonPhrase;
33:
34: /**
35: * Creates a reason for closing a web socket connection with the given code and reason phrase.
36: *
37: * @param closeCode the close code, may not be {@code null}
38: * @param reasonPhrase the reason phrase, may be {@code null}.
39: */
40: public CloseReason(CloseReason.CloseCode closeCode, String reasonPhrase) {
41: if (closeCode == null) {
42: throw new IllegalArgumentException("closeCode cannot be null");
43: }
44:
45: try {
46: if (reasonPhrase != null && reasonPhrase.getBytes("UTF-8").length > 123) {
47: throw new IllegalArgumentException(
48: "Reason Phrase cannot exceed 123 UTF-8 encoded bytes: " + reasonPhrase);
49: }
50: } catch (UnsupportedEncodingException uee) {
51: throw new IllegalStateException(uee);
52: }
53: this.closeCode = closeCode;
54: this.reasonPhrase = "".equals(reasonPhrase) ? null : reasonPhrase;
55: }
56:
57: /**
58: * The Close code associated with this CloseReason.
59: *
60: * @return the close code.
61: */
62: public CloseReason.CloseCode getCloseCode() {
63: return this.closeCode;
64: }
65:
66: /**
67: * The reason phrase associated with this CloseReason.
68: *
69: * @return the reason phrase. If there is no reason phrase, this returns the empty string
70: */
71: public String getReasonPhrase() {
72: return (this.reasonPhrase == null) ? "" : this.reasonPhrase;
73: }
74:
75: /**
76: * Converts the CloseReason to a debug-friendly string. The exact format is not defined by the specification and may
77: * change in future releases.
78: *
79: * @return A String representation of this CloseReason
80: */
81: @Override
82: public String toString() {
83: return (this.reasonPhrase == null) ? "CloseReason[" + this.closeCode.getCode() + "]"
84: : "CloseReason[" + this.closeCode.getCode() + "," + reasonPhrase + "]";
85: }
86:
87: /**
88: * A marker interface for the close codes. This interface may be implemented by enumerations that contain web socket
89: * close codes, for example enumerations that contain all the in use close codes as of web socket 1.0, or an
90: * enumeration that contains close codes that are currently reserved for special use by the web socket
91: * specification.
92: */
93: public interface CloseCode {
94: /**
95: * Returns the code number, for example the integer '1000' for normal closure.
96: *
97: * @return the code number
98: */
99: int getCode();
100: }
101:
102: /**
103: * An Enumeration of status codes for a web socket close that are defined in the specification.
104: */
105: public enum CloseCodes implements CloseReason.CloseCode {
106:
107: /**
108: * 1000 indicates a normal closure, meaning that the purpose for which the connection was established has been
109: * fulfilled.
110: */
111: NORMAL_CLOSURE(1000),
112: /**
113: * 1001 indicates that an endpoint is "going away", such as a server going down or a browser having navigated
114: * away from a page.
115: */
116: GOING_AWAY(1001),
117: /**
118: * 1002 indicates that an endpoint is terminating the connection due to a protocol error.
119: */
120: PROTOCOL_ERROR(1002),
121: /**
122: * 1003 indicates that an endpoint is terminating the connection because it has received a type of data it
123: * cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary
124: * message).
125: */
126: CANNOT_ACCEPT(1003),
127: /**
128: * Reserved. The specific meaning might be defined in the future.
129: */
130: RESERVED(1004),
131: /**
132: * 1005 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is
133: * designated for use in applications expecting a status code to indicate that no status code was actually
134: * present.
135: */
136: NO_STATUS_CODE(1005),
137: /**
138: * 1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is
139: * designated for use in applications expecting a status code to indicate that the connection was closed
140: * abnormally, e.g., without sending or receiving a Close control frame.
141: */
142: CLOSED_ABNORMALLY(1006),
143: /**
144: * 1007 indicates that an endpoint is terminating the connection because it has received data within a message
145: * that was not consistent with the type of the message (e.g., non-UTF-8 data within a text message).
146: */
147: NOT_CONSISTENT(1007),
148: /**
149: * 1008 indicates that an endpoint is terminating the connection because it has received a message that violates
150: * its policy. This is a generic status code that can be returned when there is no other more suitable status
151: * code (e.g., 1003 or 1009) or if there is a need to hide specific details about the policy.
152: */
153: VIOLATED_POLICY(1008),
154: /**
155: * 1009 indicates that an endpoint is terminating the connection because it has received a message that is too
156: * big for it to process.
157: */
158: TOO_BIG(1009),
159: /**
160: * 1010 indicates that an endpoint (client) is terminating the connection because it has expected the server to
161: * negotiate one or more extension, but the server didn't return them in the response message of the WebSocket
162: * handshake. The list of extensions that are needed SHOULD appear in the /reason/ part of the Close frame. Note
163: * that this status code is not used by the server, because it can fail the WebSocket handshake instead.
164: */
165: NO_EXTENSION(1010),
166: /**
167: * 1011 indicates that a server is terminating the connection because it encountered an unexpected condition
168: * that prevented it from fulfilling the request.
169: */
170: UNEXPECTED_CONDITION(1011),
171: /**
172: * 1012 indicates that the service will be restarted.
173: */
174: SERVICE_RESTART(1012),
175: /**
176: * 1013 indicates that the service is experiencing overload
177: */
178: TRY_AGAIN_LATER(1013),
179: /**
180: * 1015 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is
181: * designated for use in applications expecting a status code to indicate that the connection was closed due to
182: * a failure to perform a TLS handshake (e.g., the server certificate can't be verified).
183: */
184: TLS_HANDSHAKE_FAILURE(1015);
185:
186: /**
187: * Creates a CloseCode from the given int code number. This method throws an IllegalArgumentException if the int
188: * is not one of the
189: *
190: * @param code the integer code number
191: * @return a new CloseCode with the given code number
192: * @throws IllegalArgumentException if the code is not a valid close code
193: */
194: public static CloseReason.CloseCode getCloseCode(final int code) {
195:• if (code < 1000 || code > 4999) {
196: throw new IllegalArgumentException("Invalid code: " + code);
197: }
198:• switch (code) {
199: case 1000:
200: return CloseReason.CloseCodes.NORMAL_CLOSURE;
201: case 1001:
202: return CloseReason.CloseCodes.GOING_AWAY;
203: case 1002:
204: return CloseReason.CloseCodes.PROTOCOL_ERROR;
205: case 1003:
206: return CloseReason.CloseCodes.CANNOT_ACCEPT;
207: case 1004:
208: return CloseReason.CloseCodes.RESERVED;
209: case 1005:
210: return CloseReason.CloseCodes.NO_STATUS_CODE;
211: case 1006:
212: return CloseReason.CloseCodes.CLOSED_ABNORMALLY;
213: case 1007:
214: return CloseReason.CloseCodes.NOT_CONSISTENT;
215: case 1008:
216: return CloseReason.CloseCodes.VIOLATED_POLICY;
217: case 1009:
218: return CloseReason.CloseCodes.TOO_BIG;
219: case 1010:
220: return CloseReason.CloseCodes.NO_EXTENSION;
221: case 1011:
222: return CloseReason.CloseCodes.UNEXPECTED_CONDITION;
223: case 1012:
224: return CloseReason.CloseCodes.SERVICE_RESTART;
225: case 1013:
226: return CloseReason.CloseCodes.TRY_AGAIN_LATER;
227: case 1015:
228: return CloseReason.CloseCodes.TLS_HANDSHAKE_FAILURE;
229: }
230: return new CloseReason.CloseCode() {
231: @Override
232: public int getCode() {
233: return code;
234: }
235: };
236:
237: }
238:
239: CloseCodes(int code) {
240: this.code = code;
241: }
242:
243: /**
244: * Return the code number of this status code.
245: *
246: * @return the code.
247: */
248: @Override
249: public int getCode() {
250: return code;
251: }
252:
253: private int code;
254: }
255: }