View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.http.websocketx;
17  
18  import java.net.URI;
19  import java.util.Map;
20  
21  import static org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion.*;
22  
23  /**
24   * Instances the appropriate handshake class to use for clients
25   */
26  public class WebSocketClientHandshakerFactory {
27  
28      /**
29       * Instances a new handshaker
30       *
31       * @param webSocketURL
32       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
33       *            Subsequent web socket frames will be sent to this URL.
34       * @param version
35       *            Version of web socket specification to use to connect to the server
36       * @param subprotocol
37       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
38       * @param allowExtensions
39       *            Allow extensions to be used in the reserved bits of the web socket frame
40       * @param customHeaders
41       *            Custom HTTP headers to send during the handshake
42       * @throws WebSocketHandshakeException
43       */
44      public WebSocketClientHandshaker newHandshaker(URI webSocketURL, WebSocketVersion version, String subprotocol,
45              boolean allowExtensions, Map<String, String> customHeaders) {
46          return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders, Long.MAX_VALUE);
47      }
48  
49      /**
50       * Instances a new handshaker
51       *
52       * @param webSocketURL
53       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
54       *            Subsequent web socket frames will be sent to this URL.
55       * @param version
56       *            Version of web socket specification to use to connect to the server
57       * @param subprotocol
58       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
59       * @param allowExtensions
60       *            Allow extensions to be used in the reserved bits of the web socket frame
61       * @param customHeaders
62       *            Custom HTTP headers to send during the handshake
63       * @param maxFramePayloadLength
64       *            Maximum allowable frame payload length. Setting this value to your application's
65       *            requirement may reduce denial of service attacks using long data frames.
66       * @throws WebSocketHandshakeException
67       */
68      public WebSocketClientHandshaker newHandshaker(
69              URI webSocketURL, WebSocketVersion version, String subprotocol,
70              boolean allowExtensions, Map<String, String> customHeaders, long maxFramePayloadLength) {
71          if (version == V13) {
72              return new WebSocketClientHandshaker13(
73                      webSocketURL, V13, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
74          }
75          if (version == V08) {
76              return new WebSocketClientHandshaker08(
77                      webSocketURL, V08, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
78          }
79          if (version == V07) {
80              return new WebSocketClientHandshaker07(
81                      webSocketURL, V07, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
82          }
83          if (version == V00) {
84              return new WebSocketClientHandshaker00(
85                      webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength);
86          }
87  
88          throw new WebSocketHandshakeException("Protocol version " + version.toString() + " not supported.");
89      }
90  }