
org.webbitserver.netty.NettyWebSocketChannelHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webbit Show documentation
Show all versions of webbit Show documentation
A Java event based WebSocket and HTTP server
package org.webbitserver.netty;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.websocket.WebSocketFrame;
import org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder;
import org.jboss.netty.handler.codec.http.websocket.WebSocketFrameEncoder;
import org.webbitserver.WebSocketHandler;
import java.lang.Thread.UncaughtExceptionHandler;
import java.nio.channels.ClosedChannelException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.ORIGIN;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_KEY1;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_KEY2;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_LOCATION;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.UPGRADE;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.WEBSOCKET_LOCATION;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.WEBSOCKET_ORIGIN;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.WEBSOCKET_PROTOCOL;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.WEBSOCKET;
public class NettyWebSocketChannelHandler extends SimpleChannelUpstreamHandler {
protected final Executor executor;
protected final NettyHttpRequest nettyHttpRequest;
protected final NettyWebSocketConnection webSocketConnection;
protected final Thread.UncaughtExceptionHandler exceptionHandler;
protected final Thread.UncaughtExceptionHandler ioExceptionHandler;
protected final WebSocketHandler handler;
public NettyWebSocketChannelHandler(
Executor executor,
WebSocketHandler handler,
ChannelHandlerContext ctx,
UncaughtExceptionHandler exceptionHandler,
NettyHttpRequest nettyHttpRequest,
UncaughtExceptionHandler ioExceptionHandler,
NettyWebSocketConnection webSocketConnection,
HttpRequest req,
HttpResponse res
) {
this.handler = handler;
this.exceptionHandler = exceptionHandler;
this.nettyHttpRequest = nettyHttpRequest;
this.executor = executor;
this.ioExceptionHandler = ioExceptionHandler;
this.webSocketConnection = webSocketConnection;
prepareConnection(req, res);
ctx.getChannel().write(res);
adjustPipeline(ctx);
try {
handler.onOpen(this.webSocketConnection);
} catch (Exception e) {
// TODO
e.printStackTrace();
}
}
protected void prepareConnection(HttpRequest request, HttpResponse response) {
// Support both commonly used versions of the WebSocket spec.
if (isNewSkoolWebSocketRequest(request)) {
upgradeResponseNewSkool(request, response);
} else {
upgradeResponseOldSkool(request, response);
}
}
protected void adjustPipeline(ChannelHandlerContext ctx) {
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
p.replace("handler", "wshandler", this);
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
}
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
executor.execute(new Runnable() {
@Override
public void run() {
try {
handler.onClose(webSocketConnection);
} catch (Exception e1) {
exceptionHandler.uncaughtException(Thread.currentThread(), e1);
}
}
});
}
@Override
public String toString() {
return nettyHttpRequest.toString();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, final ExceptionEvent e) throws Exception {
if (e.getCause() instanceof ClosedChannelException) {
e.getChannel().close();
} else {
final Thread thread = Thread.currentThread();
executor.execute(new Runnable() {
@Override
public void run() {
ioExceptionHandler.uncaughtException(thread, e.getCause());
}
});
}
}
private boolean isNewSkoolWebSocketRequest(HttpRequest req) {
return req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2);
}
private void upgradeResponseNewSkool(HttpRequest req, HttpResponse res) {
res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE);
res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
}
// Calculate the answer of the challenge.
String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
long c = req.getContent().readLong();
ChannelBuffer input = ChannelBuffers.buffer(16);
input.writeInt(a);
input.writeInt(b);
input.writeLong(c);
try {
ChannelBuffer output = ChannelBuffers.wrappedBuffer(
MessageDigest.getInstance("MD5").digest(input.array()));
res.setContent(output);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private void upgradeResponseOldSkool(HttpRequest req, HttpResponse res) {
res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE);
res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(WEBSOCKET_PROTOCOL, protocol);
}
}
private String getWebSocketLocation(HttpRequest req) {
return "ws://" + req.getHeader(HttpHeaders.Names.HOST) + req.getUri();
}
@Override
public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
executor.execute(new Runnable() {
@Override
public void run() {
try {
handler.onMessage(webSocketConnection, ((WebSocketFrame) e.getMessage()).getTextData());
} catch (Throwable t) {
// TODO
t.printStackTrace();
}
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy