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  /**
22   * Instances the appropriate handshake class to use for clients
23   */
24  public class WebSocketClientHandshakerFactory {
25  
26      /**
27       * Instances a new handshaker
28       *
29       * @param webSocketURL
30       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
31       *            Subsequent web socket frames will be sent to this URL.
32       * @param version
33       *            Version of web socket specification to use to connect to the server
34       * @param subprotocol
35       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
36       * @param allowExtensions
37       *            Allow extensions to be used in the reserved bits of the web socket frame
38       * @param customHeaders
39       *            Custom HTTP headers to send during the handshake
40       * @throws WebSocketHandshakeException
41       */
42      public WebSocketClientHandshaker newHandshaker(URI webSocketURL, WebSocketVersion version, String subprotocol,
43              boolean allowExtensions, Map<String, String> customHeaders) {
44          return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders, Long.MAX_VALUE);
45      }
46  
47      /**
48       * Instances a new handshaker
49       *
50       * @param webSocketURL
51       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
52       *            Subsequent web socket frames will be sent to this URL.
53       * @param version
54       *            Version of web socket specification to use to connect to the server
55       * @param subprotocol
56       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
57       * @param allowExtensions
58       *            Allow extensions to be used in the reserved bits of the web socket frame
59       * @param customHeaders
60       *            Custom HTTP headers to send during the handshake
61       * @param maxFramePayloadLength
62       *            Maximum allowable frame payload length. Setting this value to your application's
63       *            requirement may reduce denial of service attacks using long data frames.
64       * @throws WebSocketHandshakeException
65       */
66      public WebSocketClientHandshaker newHandshaker(
67              URI webSocketURL, WebSocketVersion version, String subprotocol,
68              boolean allowExtensions, Map<String, String> customHeaders, long maxFramePayloadLength) {
69          if (version == WebSocketVersion.V13) {
70              return new WebSocketClientHandshaker13(
71                      webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
72          }
73          if (version == WebSocketVersion.V08) {
74              return new WebSocketClientHandshaker08(
75                      webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
76          }
77          if (version == WebSocketVersion.V00) {
78              return new WebSocketClientHandshaker00(
79                      webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
80          }
81  
82          throw new WebSocketHandshakeException("Protocol version " + version.toString() + " not supported.");
83  
84      }
85  
86  }