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 org.jboss.netty.channel.Channel;
19  import org.jboss.netty.channel.ChannelFuture;
20  import org.jboss.netty.channel.ChannelFutureListener;
21  import org.jboss.netty.channel.ChannelHandler;
22  import org.jboss.netty.channel.ChannelHandlerContext;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.Channels;
25  import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
26  import org.jboss.netty.handler.codec.http.HttpRequest;
27  import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
28  import org.jboss.netty.handler.codec.http.HttpResponse;
29  import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
30  import org.jboss.netty.util.internal.StringUtil;
31  
32  import java.util.Collections;
33  import java.util.LinkedHashSet;
34  import java.util.Set;
35  
36  /**
37   * Base class for server side web socket opening and closing handshakes
38   */
39  public abstract class WebSocketServerHandshaker {
40  
41      /**
42       * Use this as wildcard to support all requested sub-protocols
43       */
44      public static final String SUB_PROTOCOL_WILDCARD = "*";
45  
46      private final String webSocketUrl;
47  
48      private final String[] subprotocols;
49  
50      private final WebSocketVersion version;
51  
52      private final long maxFramePayloadLength;
53  
54      private String selectedSubprotocol;
55  
56      /**
57       * {@link ChannelFutureListener} which will call
58       * {@link Channels#fireExceptionCaught(ChannelHandlerContext, Throwable)}
59       * if the {@link ChannelFuture} was not successful.
60       */
61      public static final ChannelFutureListener HANDSHAKE_LISTENER = new ChannelFutureListener() {
62          public void operationComplete(ChannelFuture future) throws Exception {
63              if (!future.isSuccess()) {
64                  Channels.fireExceptionCaught(future.getChannel(), future.getCause());
65              }
66          }
67      };
68  
69      /**
70       * Constructor using default values
71       *
72       * @param version
73       *            the protocol version
74       * @param webSocketUrl
75       *            URL for web socket communications. e.g
76       *            "ws://myhost.com/mypath". Subsequent web socket frames will be
77       *            sent to this URL.
78       * @param subprotocols
79       *            CSV of supported protocols. Null if sub protocols not
80       *            supported.
81       */
82      protected WebSocketServerHandshaker(WebSocketVersion version, String webSocketUrl, String subprotocols) {
83          this(version, webSocketUrl, subprotocols, Long.MAX_VALUE);
84      }
85  
86      /**
87       * Constructor specifying the destination web socket location
88       *
89       * @param version
90       *            the protocol version
91       * @param webSocketUrl
92       *            URL for web socket communications. e.g
93       *            "ws://myhost.com/mypath". Subsequent web socket frames will be
94       *            sent to this URL.
95       * @param subprotocols
96       *            CSV of supported protocols. Null if sub protocols not
97       *            supported.
98       * @param maxFramePayloadLength
99       *            Maximum length of a frame's payload
100      */
101     protected WebSocketServerHandshaker(WebSocketVersion version, String webSocketUrl, String subprotocols,
102             long maxFramePayloadLength) {
103         this.version = version;
104         this.webSocketUrl = webSocketUrl;
105         if (subprotocols != null) {
106             String[] subprotocolArray = StringUtil.split(subprotocols, ',');
107             for (int i = 0; i < subprotocolArray.length; i++) {
108                 subprotocolArray[i] = subprotocolArray[i].trim();
109             }
110             this.subprotocols = subprotocolArray;
111         } else {
112             this.subprotocols = new String[0];
113         }
114         this.maxFramePayloadLength = maxFramePayloadLength;
115     }
116 
117     /**
118      * Returns the URL of the web socket
119      */
120     public String getWebSocketUrl() {
121         return webSocketUrl;
122     }
123 
124     /**
125      * Returns the CSV of supported sub protocols
126      */
127     public Set<String> getSubprotocols() {
128         Set<String> ret = new LinkedHashSet<String>();
129         Collections.addAll(ret, subprotocols);
130         return ret;
131     }
132 
133     /**
134      * Returns the version of the specification being supported
135      */
136     public WebSocketVersion getVersion() {
137         return version;
138     }
139 
140     /**
141      * Returns the max length for any frame's payload
142      */
143     public long getMaxFramePayloadLength() {
144         return maxFramePayloadLength;
145     }
146 
147     /**
148      * Performs the opening handshake
149      *
150      * @param channel
151      *            Channel
152      * @param req
153      *            HTTP Request
154      */
155     public abstract ChannelFuture handshake(Channel channel, HttpRequest req);
156 
157     /**
158      * Upgrades the connection and send the handshake response.
159      */
160     protected ChannelFuture writeHandshakeResponse(
161             Channel channel, HttpResponse res, ChannelHandler encoder, ChannelHandler decoder) {
162         final ChannelPipeline p = channel.getPipeline();
163         if (p.get(HttpChunkAggregator.class) != null) {
164             p.remove(HttpChunkAggregator.class);
165         }
166 
167         final String httpEncoderName = p.getContext(HttpResponseEncoder.class).getName();
168         p.addAfter(httpEncoderName, "wsencoder", encoder);
169         p.get(HttpRequestDecoder.class).replace("wsdecoder", decoder);
170 
171         final ChannelFuture future = channel.write(res);
172         future.addListener(new ChannelFutureListener() {
173             public void operationComplete(ChannelFuture future) {
174                 p.remove(httpEncoderName);
175             }
176         });
177 
178         return future;
179     }
180 
181     /**
182      * Performs the closing handshake
183      *
184      * @param channel
185      *            Channel
186      * @param frame
187      *            Closing Frame that was received
188      */
189     public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame);
190 
191     /**
192      * Selects the first matching supported sub protocol
193      *
194      * @param requestedSubprotocols
195      *            CSV of protocols to be supported. e.g. "chat, superchat"
196      * @return First matching supported sub protocol. Null if not found.
197      */
198     protected String selectSubprotocol(String requestedSubprotocols) {
199         if (requestedSubprotocols == null || subprotocols.length == 0) {
200             return null;
201         }
202 
203         String[] requestedSubprotocolArray = StringUtil.split(requestedSubprotocols, ',');
204         for (String p : requestedSubprotocolArray) {
205             String requestedSubprotocol = p.trim();
206 
207             for (String supportedSubprotocol : subprotocols) {
208                 if (SUB_PROTOCOL_WILDCARD.equals(supportedSubprotocol) ||
209                          requestedSubprotocol.equals(supportedSubprotocol)) {
210                     return requestedSubprotocol;
211                 }
212             }
213         }
214 
215         // No match found
216         return null;
217     }
218 
219     /**
220      * Returns the selected subprotocol. Null if no subprotocol has been selected.
221      * <p>
222      * This is only available AFTER <tt>handshake()</tt> has been called.
223      * </p>
224      */
225     public String getSelectedSubprotocol() {
226         return selectedSubprotocol;
227     }
228 
229     protected void setSelectedSubprotocol(String value) {
230         selectedSubprotocol = value;
231     }
232 
233 }