Java tutorial
package io.urmia.proxy; /** * * Copyright 2014 by Amin Abbaspour * * This file is part of Urmia.io * * Urmia.io is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Urmia.io is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Urmia.io. If not, see <http://www.gnu.org/licenses/>. */ import io.netty.buffer.Unpooled; import io.netty.channel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DirectWriteBackHttpProxyBackendHandler extends ChannelInboundHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(DirectWriteBackHttpProxyBackendHandler.class); private final Channel inboundChannel; public DirectWriteBackHttpProxyBackendHandler(Channel inboundChannel) { this.inboundChannel = inboundChannel; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { log.info("backend active: {}", ctx); ctx.read(); ctx.write(Unpooled.EMPTY_BUFFER); } @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { //log.info("success on write back to inbound"); ctx.channel().read(); } else { log.warn("fail on write to back to inbound: {}", future.cause()); future.channel().close(); } } }); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { log.info("backend channelInactive: {}. closing inbound", ctx); inboundChannel.close(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { log.warn("exceptionCaught", cause); inboundChannel.close(); } }