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