View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.api;
20  
21  import java.nio.charset.StandardCharsets;
22  
23  public class CloseStatus
24  {
25      private static final int MAX_CONTROL_PAYLOAD = 125;
26      public static final int MAX_REASON_PHRASE = MAX_CONTROL_PAYLOAD - 2;
27  
28      /**
29       * Convenience method for trimming a long reason phrase at the maximum reason phrase length of 123 UTF-8 bytes (per WebSocket spec).
30       * 
31       * @param reason
32       *            the proposed reason phrase
33       * @return the reason phrase (trimmed if needed)
34       * @deprecated use of this method is strongly discouraged, as it creates too many new objects that are just thrown away to accomplish its goals.
35       */
36      @Deprecated
37      public static String trimMaxReasonLength(String reason)
38      {
39          if (reason == null)
40          {
41              return null;
42          }
43  
44          byte[] reasonBytes = reason.getBytes(StandardCharsets.UTF_8);
45          if (reasonBytes.length > MAX_REASON_PHRASE)
46          {
47              byte[] trimmed = new byte[MAX_REASON_PHRASE];
48              System.arraycopy(reasonBytes,0,trimmed,0,MAX_REASON_PHRASE);
49              return new String(trimmed,StandardCharsets.UTF_8);
50          }
51          
52          return reason;
53      }
54  
55      private int code;
56      private String phrase;
57  
58      /**
59       * Creates a reason for closing a web socket connection with the given code and reason phrase.
60       * 
61       * @param closeCode
62       *            the close code
63       * @param reasonPhrase
64       *            the reason phrase
65       * @see StatusCode
66       */
67      public CloseStatus(int closeCode, String reasonPhrase)
68      {
69          this.code = closeCode;
70          this.phrase = reasonPhrase;
71          if (reasonPhrase.length() > MAX_REASON_PHRASE)
72          {
73              throw new IllegalArgumentException("Phrase exceeds maximum length of " + MAX_REASON_PHRASE);
74          }
75      }
76  
77      public int getCode()
78      {
79          return code;
80      }
81  
82      public String getPhrase()
83      {
84          return phrase;
85      }
86  }