1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ChannelHandlerContext;
22 import org.jboss.netty.channel.Channels;
23 import org.jboss.netty.handler.codec.http.HttpRequest;
24 import org.jboss.netty.util.internal.StringUtil;
25
26 import java.util.Collections;
27 import java.util.LinkedHashSet;
28 import java.util.Set;
29
30
31
32
33 public abstract class WebSocketServerHandshaker {
34
35 private final String webSocketUrl;
36
37 private final String[] subprotocols;
38
39 private final WebSocketVersion version;
40
41 private final long maxFramePayloadLength;
42
43 private String selectedSubprotocol;
44
45
46
47
48
49
50 public static final ChannelFutureListener HANDSHAKE_LISTENER = new ChannelFutureListener() {
51 public void operationComplete(ChannelFuture future) throws Exception {
52 if (!future.isSuccess()) {
53 Channels.fireExceptionCaught(future.getChannel(), future.getCause());
54 }
55 }
56 };
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 protected WebSocketServerHandshaker(WebSocketVersion version, String webSocketUrl, String subprotocols) {
72 this(version, webSocketUrl, subprotocols, Long.MAX_VALUE);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 protected WebSocketServerHandshaker(WebSocketVersion version, String webSocketUrl, String subprotocols,
91 long maxFramePayloadLength) {
92 this.version = version;
93 this.webSocketUrl = webSocketUrl;
94 if (subprotocols != null) {
95 String[] subprotocolArray = StringUtil.split(subprotocols, ',');
96 for (int i = 0; i < subprotocolArray.length; i++) {
97 subprotocolArray[i] = subprotocolArray[i].trim();
98 }
99 this.subprotocols = subprotocolArray;
100 } else {
101 this.subprotocols = new String[0];
102 }
103 this.maxFramePayloadLength = maxFramePayloadLength;
104 }
105
106
107
108
109 public String getWebSocketUrl() {
110 return webSocketUrl;
111 }
112
113
114
115
116 public Set<String> getSubprotocols() {
117 Set<String> ret = new LinkedHashSet<String>();
118 Collections.addAll(ret, subprotocols);
119 return ret;
120 }
121
122
123
124
125 public WebSocketVersion getVersion() {
126 return version;
127 }
128
129
130
131
132 public long getMaxFramePayloadLength() {
133 return maxFramePayloadLength;
134 }
135
136
137
138
139
140
141
142
143
144 public abstract ChannelFuture handshake(Channel channel, HttpRequest req);
145
146
147
148
149
150
151
152
153
154 public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame);
155
156
157
158
159
160
161
162
163 protected String selectSubprotocol(String requestedSubprotocols) {
164 if (requestedSubprotocols == null || subprotocols.length == 0) {
165 return null;
166 }
167
168 String[] requestedSubprotocolArray = StringUtil.split(requestedSubprotocols, ',');
169 for (String p : requestedSubprotocolArray) {
170 String requestedSubprotocol = p.trim();
171
172 for (String supportedSubprotocol : subprotocols) {
173 if (requestedSubprotocol.equals(supportedSubprotocol)) {
174 return requestedSubprotocol;
175 }
176 }
177 }
178
179
180 return null;
181 }
182
183
184
185
186
187
188
189 public String getSelectedSubprotocol() {
190 return selectedSubprotocol;
191 }
192
193 protected void setSelectedSubprotocol(String value) {
194 selectedSubprotocol = value;
195 }
196
197 }