View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  public class CloseStatus
22  {
23      private static final int MAX_CONTROL_PAYLOAD = 125;
24      private static final int MAX_REASON_PHRASE = MAX_CONTROL_PAYLOAD - 2;
25  
26      /**
27       * Convenience method for trimming a long reason phrase at the maximum reason phrase length.
28       * 
29       * @param reason
30       *            the proposed reason phrase
31       * @return the reason phrase (trimmed if needed)
32       */
33      public static String trimMaxReasonLength(String reason)
34      {
35          if (reason == null)
36          {
37              return null;
38          }
39          
40          if (reason.length() > MAX_REASON_PHRASE)
41          {
42              return reason.substring(0,MAX_REASON_PHRASE);
43          }
44          else
45          {
46              return reason;
47          }
48      }
49  
50      private int code;
51      private String phrase;
52  
53      /**
54       * Creates a reason for closing a web socket connection with the given code and reason phrase.
55       * 
56       * @param closeCode
57       *            the close code
58       * @param reasonPhrase
59       *            the reason phrase
60       * @see StatusCode
61       */
62      public CloseStatus(int closeCode, String reasonPhrase)
63      {
64          this.code = closeCode;
65          this.phrase = reasonPhrase;
66          if (reasonPhrase.length() > MAX_REASON_PHRASE)
67          {
68              throw new IllegalArgumentException("Phrase exceeds maximum length of " + MAX_REASON_PHRASE);
69          }
70      }
71  
72      public int getCode()
73      {
74          return code;
75      }
76  
77      public String getPhrase()
78      {
79          return phrase;
80      }
81  }