org.webbitserver.netty.FlashPolicyFileHandler Maven / Gradle / Ivy
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;
import java.util.concurrent.Executor;
/**
* 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;
private final ConnectionHelper connectionHelper;
public FlashPolicyFileHandler(Executor executor, Thread.UncaughtExceptionHandler exceptionHandler, Thread.UncaughtExceptionHandler ioExceptionHandler, int publicPort) {
this.publicPort = publicPort;
this.connectionHelper = new ConnectionHelper(executor, exceptionHandler, ioExceptionHandler) {
@Override
protected void fireOnClose() throws Exception {
throw new UnsupportedOperationException();
}
};
}
@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 {
connectionHelper.fireConnectionException(e);
}
}