org.mockserver.proxy.relay.ProxyRelayHandler Maven / Gradle / Ivy
package org.mockserver.proxy.relay;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.HttpMessage;
import org.slf4j.Logger;
import static org.mockserver.proxy.error.Logging.shouldIgnoreException;
public class ProxyRelayHandler extends SimpleChannelInboundHandler {
private final Logger logger;
private volatile Channel channel;
public ProxyRelayHandler(Channel channel, Logger logger) {
super(false);
this.channel = channel;
this.logger = logger;
}
/**
* Closes the specified channel after all queued write requests are flushed.
*/
public static void closeOnFlush(Channel ch) {
if (ch != null && ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.read();
ctx.write(Unpooled.EMPTY_BUFFER);
}
@Override
public void channelRead0(final ChannelHandlerContext ctx, final T msg) {
channel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ctx.channel().read();
} else {
logger.error("Exception while returning writing", future.cause());
future.channel().close();
}
}
});
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
closeOnFlush(channel);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (!shouldIgnoreException(cause)) {
logger.warn("Exception caught by proxy relay handler -> closing pipeline " + ctx.channel(), cause);
}
closeOnFlush(ctx.channel());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy