if (key == null) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
ServerEndpointConfig sec = mappingResult.getConfig();
// Origin check
String origin = req.getHeader("Origin");
if (!sec.getConfigurator().checkOrigin(origin)) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// Sub-protocols
List<String> subProtocols = getTokensFromHeader(req,
"Sec-WebSocket-Protocol");
if (!subProtocols.isEmpty()) {
subProtocol = sec.getConfigurator().
getNegotiatedSubprotocol(
sec.getSubprotocols(), subProtocols);
}
// Extensions
// Currently no extensions are supported by this implementation
// If we got this far, all is good. Accept the connection.
resp.setHeader(Constants.UPGRADE_HEADER_NAME,
Constants.UPGRADE_HEADER_VALUE);
resp.setHeader(Constants.CONNECTION_HEADER_NAME,
Constants.CONNECTION_HEADER_VALUE);
resp.setHeader(HandshakeResponse.SEC_WEBSOCKET_ACCEPT,
getWebSocketAccept(key));
if (subProtocol != null) {
resp.setHeader("Sec-WebSocket-Protocol", subProtocol);
}
if (!extensions.isEmpty()) {
StringBuilder sb = new StringBuilder();
Iterator<Extension> iter = extensions.iterator();
// There must be at least one
sb.append(iter.next());
while (iter.hasNext()) {
sb.append(',');
sb.append(iter.next().getName());
}
resp.setHeader("Sec-WebSocket-Extensions", sb.toString());
}
Endpoint ep;
try {
Class<?> clazz = sec.getEndpointClass();
if (Endpoint.class.isAssignableFrom(clazz)) {
ep = (Endpoint) sec.getConfigurator().getEndpointInstance(
clazz);
} else {
ep = new PojoEndpointServer();
}
} catch (InstantiationException e) {
throw new ServletException(e);
}
WsHandshakeRequest wsRequest = new WsHandshakeRequest(req);
WsHandshakeResponse wsResponse = new WsHandshakeResponse();
sec.getConfigurator().modifyHandshake(sec, wsRequest, wsResponse);
wsRequest.finished();
// Add any additional headers
for (Entry<String,List<String>> entry :
wsResponse.getHeaders().entrySet()) {