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