org.webbitserver.netty.FlashPolicyFileHandler 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.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.util.CharsetUtil;
/**
* Responds with a Flash socket policy file.
*
*
* This implementation is based on the
* waywardmonkeys/netty-flash-crossdomain-policy-server project and the
* Setting up a socket policy file server article.
*
*/
public class FlashPolicyFileHandler extends SimpleChannelUpstreamHandler {
private final int publicPort;
public FlashPolicyFileHandler(int publicPort) {
super();
this.publicPort = publicPort;
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
Channel ch = e.getChannel();
ChannelBuffer response = getPolicyFileContents();
ChannelFuture future = ch.write(response);
future.addListener(ChannelFutureListener.CLOSE);
ctx.getPipeline().remove(this);
}
private ChannelBuffer getPolicyFileContents() throws Exception {
return ChannelBuffers.copiedBuffer(
"\r\n"
+ "\r\n"
+ "\r\n"
+ " \r\n"
+ " \r\n"
+ " \r\n",
CharsetUtil.US_ASCII);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
}