View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.length() > MAX_REASON_PHRASE)
36          {
37              return reason.substring(0,MAX_REASON_PHRASE);
38          }
39          else
40          {
41              return reason;
42          }
43      }
44  
45      private int code;
46      private String phrase;
47  
48      /**
49       * Creates a reason for closing a web socket connection with the given code and reason phrase.
50       * 
51       * @param closeCode
52       *            the close code
53       * @param reasonPhrase
54       *            the reason phrase
55       * @see StatusCode
56       */
57      public CloseStatus(int closeCode, String reasonPhrase)
58      {
59          this.code = closeCode;
60          this.phrase = reasonPhrase;
61          if (reasonPhrase.length() > MAX_REASON_PHRASE)
62          {
63              throw new IllegalArgumentException("Phrase exceeds maximum length of " + MAX_REASON_PHRASE);
64          }
65      }
66  
67      public int getCode()
68      {
69          return code;
70      }
71  
72      public String getPhrase()
73      {
74          return phrase;
75      }
76  }