View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  /**
22   * Settings for WebSocket operations.
23   */
24  public class WebSocketPolicy
25  {
26      private static final int KB = 1024;
27  
28      public static WebSocketPolicy newClientPolicy()
29      {
30          return new WebSocketPolicy(WebSocketBehavior.CLIENT);
31      }
32  
33      public static WebSocketPolicy newServerPolicy()
34      {
35          return new WebSocketPolicy(WebSocketBehavior.SERVER);
36      }
37  
38      /**
39       * The maximum size of a text message during parsing/generating.
40       * <p>
41       * Default: 65536 (64 K)
42       */
43      private long maxMessageSize = 64 * KB;
44  
45      /**
46       * The time in ms (milliseconds) that a websocket may be idle before closing.
47       * <p>
48       * Default: 300000 (ms)
49       */
50      private long idleTimeout = 300000;
51  
52      /**
53       * The size of the input (read from network layer) buffer size.
54       * <p>
55       * Default: 4096 (4 K)
56       */
57      private int inputBufferSize = 4 * KB;
58  
59      /**
60       * Behavior of the websockets
61       */
62      private final WebSocketBehavior behavior;
63  
64      public WebSocketPolicy(WebSocketBehavior behavior)
65      {
66          this.behavior = behavior;
67      }
68  
69      public void assertValidMessageSize(int requestedSize)
70      {
71          if (maxMessageSize > 0)
72          {
73              // validate it
74              if (requestedSize > maxMessageSize)
75              {
76                  throw new MessageTooLargeException("Requested message size [" + requestedSize + "] exceeds maximum size [" + maxMessageSize + "]");
77              }
78          }
79      }
80  
81      public WebSocketPolicy clonePolicy()
82      {
83          WebSocketPolicy clone = new WebSocketPolicy(this.behavior);
84          clone.idleTimeout = this.idleTimeout;
85          clone.maxMessageSize = this.maxMessageSize;
86          clone.inputBufferSize = this.inputBufferSize;
87          return clone;
88      }
89  
90      public WebSocketBehavior getBehavior()
91      {
92          return behavior;
93      }
94  
95      public long getIdleTimeout()
96      {
97          return idleTimeout;
98      }
99  
100     public int getInputBufferSize()
101     {
102         return inputBufferSize;
103     }
104 
105     public long getMaxMessageSize()
106     {
107         return maxMessageSize;
108     }
109 
110     public void setIdleTimeout(long idleTimeout)
111     {
112         this.idleTimeout = idleTimeout;
113     }
114 
115     public void setInputBufferSize(int inputBufferSize)
116     {
117         this.inputBufferSize = inputBufferSize;
118     }
119 
120     public void setMaxMessageSize(long maxMessageSize)
121     {
122         this.maxMessageSize = maxMessageSize;
123     }
124 }