nettyで一種のロギングプロキシを作成したいと思います。目標は、WebブラウザがNettyサーバーにHTTPリクエストを送信し、それらをバックエンドWebサーバーに渡せるようにするだけでなく、HTTP固有のものに基づいて特定のアクションを実行できるようにすることです。
いくつかの便利なnettyの例、HexDumpProxy(プロトコルに依存しないプロキシ部分を実行します)があり、HttpSnoopServerHandlerからほんの少しのコードを取得しました。
私のコードは現在次のようになっています。HexDumpProxyInboundHandlerはhttp://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/proxy/HexDumpProxyInboundHandler.htmlにあります。
//in HexDumpProxyPipelineFactory
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline(); // Note the static import.
p.addLast("handler", new HexDumpProxyInboundHandler(cf, remoteHost, remotePort));
p.addLast("decoder", new HttpRequestDecoder());
p.addLast("handler2", new HttpSnoopServerHandler());
return p;
}
//HttpSnoopServerHandler
public class HttpSnoopServerHandler extends SimpleChannelUpstreamHandler {
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
HttpRequest request = (HttpRequest) e.getMessage();
System.out.println(request.getUri());
//going to do things based on the URI
}
}
残念ながら、HttpSnoopServerHandlerでmessageReceivedが呼び出されることはありません。これは、HexDumpProxyInboundHandlerがすべてのイベントを消費しているようです。
2つのハンドラーを作成するにはどうすればよいですか。一方はデコーダーを必要としますが、もう一方は必要ありません(HTTPを理解する必要がなく、すべての接続をプロキシするだけのHexDumpProxyをそのまま使用したいのですが、私のHttpSnoopHandlerその前にHttpRequestDecoderを置く必要があります)?