public abstract class ProxyHandler extends ChannelDuplexHandler
Modifier and Type | Class and Description |
---|---|
private class |
ProxyHandler.LazyChannelPromise |
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.String |
AUTH_NONE
A string that signifies 'no authentication' or 'anonymous'.
|
private ProxyHandler.LazyChannelPromise |
connectPromise |
private ScheduledFuture<?> |
connectTimeoutFuture |
private long |
connectTimeoutMillis |
private ChannelHandlerContext |
ctx |
private static long |
DEFAULT_CONNECT_TIMEOUT_MILLIS
The default connect timeout: 10 seconds.
|
private java.net.SocketAddress |
destinationAddress |
private boolean |
finished |
private boolean |
flushedPrematurely |
private static InternalLogger |
logger |
private PendingWriteQueue |
pendingWrites |
private java.net.SocketAddress |
proxyAddress |
private boolean |
suppressChannelReadComplete |
private ChannelFutureListener |
writeListener |
Modifier | Constructor and Description |
---|---|
protected |
ProxyHandler(java.net.SocketAddress proxyAddress) |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
addCodec(ChannelHandlerContext ctx)
Adds the codec handlers required to communicate with the proxy server.
|
private void |
addPendingWrite(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise) |
abstract java.lang.String |
authScheme()
Returns the name of the authentication scheme in use.
|
private void |
cancelConnectTimeoutFuture() |
void |
channelActive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelActive() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
connect(ChannelHandlerContext ctx,
java.net.SocketAddress remoteAddress,
java.net.SocketAddress localAddress,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
Future<Channel> |
connectFuture()
Returns a
Future that is notified when the connection to the destination has been established
or the connection attempt has failed. |
long |
connectTimeoutMillis()
Returns the connect timeout in millis.
|
<T extends java.net.SocketAddress> |
destinationAddress()
Returns the address of the destination to connect to via the proxy server.
|
void |
exceptionCaught(ChannelHandlerContext ctx,
java.lang.Throwable cause)
Calls
ChannelHandlerContext.fireExceptionCaught(Throwable) to forward
to the next ChannelHandler in the ChannelPipeline . |
protected java.lang.String |
exceptionMessage(java.lang.String msg)
Decorates the specified exception message with the common information such as the current protocol,
authentication scheme, proxy address, and destination address.
|
private void |
failPendingWrites(java.lang.Throwable cause) |
private void |
failPendingWritesAndClose(java.lang.Throwable cause) |
void |
flush(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.flush() to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected abstract boolean |
handleResponse(ChannelHandlerContext ctx,
java.lang.Object response)
Handles the message received from the proxy server.
|
boolean |
isConnected()
Returns
true if and only if the connection to the destination has been established successfully. |
protected abstract java.lang.Object |
newInitialMessage(ChannelHandlerContext ctx)
Returns a new message that is sent at first time when the connection to the proxy server has been established.
|
abstract java.lang.String |
protocol()
Returns the name of the proxy protocol in use.
|
<T extends java.net.SocketAddress> |
proxyAddress()
Returns the address of the proxy server.
|
private static void |
readIfNeeded(ChannelHandlerContext ctx) |
protected abstract void |
removeDecoder(ChannelHandlerContext ctx)
Removes the decoders added in
addCodec(ChannelHandlerContext) . |
protected abstract void |
removeEncoder(ChannelHandlerContext ctx)
Removes the encoders added in
addCodec(ChannelHandlerContext) . |
private boolean |
safeRemoveDecoder() |
private boolean |
safeRemoveEncoder() |
private void |
sendInitialMessage(ChannelHandlerContext ctx)
Sends the initial message to be sent to the proxy server.
|
protected void |
sendToProxyServer(java.lang.Object msg)
Sends the specified message to the proxy server.
|
private void |
setConnectFailure(java.lang.Throwable cause) |
private void |
setConnectSuccess() |
void |
setConnectTimeoutMillis(long connectTimeoutMillis)
Sets the connect timeout in millis.
|
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
private void |
writePendingWrites() |
bind, close, deregister, disconnect, read
channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
ensureNotSharable, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerRemoved
private static final InternalLogger logger
private static final long DEFAULT_CONNECT_TIMEOUT_MILLIS
static final java.lang.String AUTH_NONE
private final java.net.SocketAddress proxyAddress
private volatile java.net.SocketAddress destinationAddress
private volatile long connectTimeoutMillis
private volatile ChannelHandlerContext ctx
private PendingWriteQueue pendingWrites
private boolean finished
private boolean suppressChannelReadComplete
private boolean flushedPrematurely
private final ProxyHandler.LazyChannelPromise connectPromise
private ScheduledFuture<?> connectTimeoutFuture
private final ChannelFutureListener writeListener
public abstract java.lang.String protocol()
public abstract java.lang.String authScheme()
public final <T extends java.net.SocketAddress> T proxyAddress()
public final <T extends java.net.SocketAddress> T destinationAddress()
public final boolean isConnected()
true
if and only if the connection to the destination has been established successfully.public final Future<Channel> connectFuture()
Future
that is notified when the connection to the destination has been established
or the connection attempt has failed.public final long connectTimeoutMillis()
public final void setConnectTimeoutMillis(long connectTimeoutMillis)
public final void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
java.lang.Exception
protected abstract void addCodec(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception
protected abstract void removeEncoder(ChannelHandlerContext ctx) throws java.lang.Exception
addCodec(ChannelHandlerContext)
.java.lang.Exception
protected abstract void removeDecoder(ChannelHandlerContext ctx) throws java.lang.Exception
addCodec(ChannelHandlerContext)
.java.lang.Exception
public final void connect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.connect
in interface ChannelOutboundHandler
connect
in class ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the connect operation is maderemoteAddress
- the SocketAddress
to which it should connectlocalAddress
- the SocketAddress
which is used as source on connectpromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occurspublic final void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelActive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelActive
in interface ChannelInboundHandler
channelActive
in class ChannelInboundHandlerAdapter
java.lang.Exception
private void sendInitialMessage(ChannelHandlerContext ctx) throws java.lang.Exception
connectPromise
as failure if the connection attempt does not success within the timeout.java.lang.Exception
protected abstract java.lang.Object newInitialMessage(ChannelHandlerContext ctx) throws java.lang.Exception
null
if the proxy server is expected to send the first message insteadjava.lang.Exception
protected final void sendToProxyServer(java.lang.Object msg)
handleResponse(ChannelHandlerContext, Object)
.public final void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
java.lang.Exception
public final void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireExceptionCaught(Throwable)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.exceptionCaught
in interface ChannelHandler
exceptionCaught
in interface ChannelInboundHandler
exceptionCaught
in class ChannelInboundHandlerAdapter
java.lang.Exception
public final void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
in interface ChannelInboundHandler
channelRead
in class ChannelInboundHandlerAdapter
java.lang.Exception
protected abstract boolean handleResponse(ChannelHandlerContext ctx, java.lang.Object response) throws java.lang.Exception
true
if the connection to the destination has been established,
false
if the connection to the destination has not been established and more messages are
expected from the proxy serverjava.lang.Exception
private void setConnectSuccess()
private boolean safeRemoveDecoder()
private boolean safeRemoveEncoder()
private void setConnectFailure(java.lang.Throwable cause)
private void failPendingWritesAndClose(java.lang.Throwable cause)
private void cancelConnectTimeoutFuture()
protected final java.lang.String exceptionMessage(java.lang.String msg)
public final void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelReadComplete()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelReadComplete
in interface ChannelInboundHandler
channelReadComplete
in class ChannelInboundHandlerAdapter
java.lang.Exception
public final void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelOutboundInvoker.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelOutboundHandler
write
in class ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occurspublic final void flush(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelDuplexHandler
ChannelHandlerContext.flush()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.flush
in interface ChannelOutboundHandler
flush
in class ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the flush operation is madejava.lang.Exception
- thrown if an error occursprivate static void readIfNeeded(ChannelHandlerContext ctx)
private void writePendingWrites()
private void failPendingWrites(java.lang.Throwable cause)
private void addPendingWrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)