public abstract class AbstractGenericHandler<RESPONSE,ENCODED,REQUEST extends CouchbaseRequest> extends MessageToMessageCodec<RESPONSE,REQUEST>
Generic handler which acts as the common base type for all implementing handlers.
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
protected static Charset |
CHARSET
The default charset to use for all requests and responses.
|
protected static byte[] |
EMPTY_BYTES
Empty bytes to reuse.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractGenericHandler(AbstractEndpoint endpoint,
com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer,
boolean isTransient,
boolean pipeline)
Creates a new
AbstractGenericHandler with the default queue. |
protected |
AbstractGenericHandler(AbstractEndpoint endpoint,
com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer,
Queue<REQUEST> queue,
boolean isTransient,
boolean pipeline)
Creates a new
AbstractGenericHandler with a custom queue. |
Modifier and Type | Method and Description |
---|---|
static void |
addHttpBasicAuth(ChannelHandlerContext ctx,
HttpRequest request,
String user,
String password)
Add basic authentication headers to a
HttpRequest . |
void |
channelActive(ChannelHandlerContext ctx) |
void |
channelInactive(ChannelHandlerContext ctx) |
void |
channelWritabilityChanged(ChannelHandlerContext ctx) |
void |
connect(ChannelHandlerContext ctx,
SocketAddress remoteAddress,
SocketAddress localAddress,
ChannelPromise future) |
protected CouchbaseRequest |
createKeepAliveRequest()
Override to return a non-null request to be fired in the pipeline in case a keep alive is triggered.
|
protected REQUEST |
currentRequest()
Returns the current request if set.
|
protected void |
currentRequest(REQUEST request)
Sets current request.
|
protected void |
decode(ChannelHandlerContext ctx,
RESPONSE msg,
List<Object> out) |
protected abstract CouchbaseResponse |
decodeResponse(ChannelHandlerContext ctx,
RESPONSE msg)
Decodes the incoming response and transforms it into a
CouchbaseResponse . |
protected void |
encode(ChannelHandlerContext ctx,
REQUEST msg,
List<Object> out) |
protected abstract ENCODED |
encodeRequest(ChannelHandlerContext ctx,
REQUEST msg)
Encode the outgoing request and return it in encoded format.
|
protected AbstractEndpoint |
endpoint()
The parent endpoint.
|
protected CoreEnvironment |
env()
Returns environment.
|
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) |
protected void |
finishedDecoding()
Notify that decoding is finished.
|
DecodingState |
getDecodingState() |
void |
handlerRemoved(ChannelHandlerContext ctx) |
protected static String |
logIdent(ChannelHandlerContext ctx,
Endpoint endpoint)
Simple log helper to give logs a common prefix.
|
protected void |
onKeepAliveFired(ChannelHandlerContext ctx,
CouchbaseRequest keepAliveRequest)
Override to customize the behavior when a keep alive has been triggered and a keep alive request sent.
|
protected void |
onKeepAliveResponse(ChannelHandlerContext ctx,
CouchbaseResponse keepAliveResponse)
Override to customize the behavior when a keep alive has been responded to.
|
protected void |
publishResponse(CouchbaseResponse response,
Subject<CouchbaseResponse,CouchbaseResponse> observable)
Publishes a response with the attached observable.
|
protected String |
remoteHostname() |
protected String |
remoteHttpHost(ChannelHandlerContext ctx)
Helper method to return the remote http host, cached.
|
protected abstract ServiceType |
serviceType()
Returns the
ServiceType associated with this handler. |
protected void |
sideEffectRequestToCancel(REQUEST request)
This method can be overridden as it is called every time an operation is cancelled.
|
void |
userEventTriggered(ChannelHandlerContext ctx,
Object evt) |
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise) |
acceptInboundMessage, acceptOutboundMessage, channelRead
bind, close, deregister, disconnect, flush, read
channelReadComplete, channelRegistered, channelUnregistered
handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded
protected static final Charset CHARSET
The default charset to use for all requests and responses.
protected static final byte[] EMPTY_BYTES
Empty bytes to reuse.
protected AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, boolean isTransient, boolean pipeline)
Creates a new AbstractGenericHandler
with the default queue.
endpoint
- the endpoint reference.responseBuffer
- the response buffer.protected AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, Queue<REQUEST> queue, boolean isTransient, boolean pipeline)
Creates a new AbstractGenericHandler
with a custom queue.
endpoint
- the endpoint reference.responseBuffer
- the response buffer.queue
- the queue.protected abstract ENCODED encodeRequest(ChannelHandlerContext ctx, REQUEST msg) throws Exception
Encode the outgoing request and return it in encoded format.
This method needs to be implemented by the child handler and is responsible for the actual conversion.
ctx
- the context passed in.msg
- the outgoing message.Exception
- as a generic error.protected abstract CouchbaseResponse decodeResponse(ChannelHandlerContext ctx, RESPONSE msg) throws Exception
Decodes the incoming response and transforms it into a CouchbaseResponse
.
Note that the actual notification is handled by this generic handler, the implementing class only is concerned about the conversion itself.
ctx
- the context passed in.msg
- the incoming message.Exception
- as a generic error. It will be bubbled up to the user (wrapped in a CouchbaseException) in the onError of the request’s Observable.protected abstract ServiceType serviceType()
Returns the ServiceType
associated with this handler.
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
write
in interface ChannelOutboundHandler
write
in class MessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
Exception
protected void encode(ChannelHandlerContext ctx, REQUEST msg, List<Object> out) throws Exception
encode
in class MessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
Exception
protected void decode(ChannelHandlerContext ctx, RESPONSE msg, List<Object> out) throws Exception
decode
in class MessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
Exception
protected void publishResponse(CouchbaseResponse response, Subject<CouchbaseResponse,CouchbaseResponse> observable)
Publishes a response with the attached observable.
response
- the response to publish.observable
- pushing into the event sink.protected void finishedDecoding()
Notify that decoding is finished. This needs to be called by the child handlers in order to signal that operations are done.
public void channelInactive(ChannelHandlerContext ctx) throws Exception
channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
Exception
public void channelActive(ChannelHandlerContext ctx) throws Exception
channelActive
in interface ChannelInboundHandler
channelActive
in class ChannelInboundHandlerAdapter
Exception
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
channelWritabilityChanged
in interface ChannelInboundHandler
channelWritabilityChanged
in class ChannelInboundHandlerAdapter
Exception
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise future) throws Exception
connect
in interface ChannelOutboundHandler
connect
in class ChannelDuplexHandler
Exception
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
exceptionCaught
in interface ChannelHandler
exceptionCaught
in interface ChannelInboundHandler
exceptionCaught
in class ChannelInboundHandlerAdapter
Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
protected void sideEffectRequestToCancel(REQUEST request)
This method can be overridden as it is called every time an operation is cancelled.
Overriding implementations may do some custom logic with them, for example freeing resources they know of to avoid leaking.
request
- the request to side effect on.public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
userEventTriggered
in interface ChannelInboundHandler
userEventTriggered
in class ChannelInboundHandlerAdapter
Exception
protected CouchbaseRequest createKeepAliveRequest()
Override to return a non-null request to be fired in the pipeline in case a keep alive is triggered.
protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest)
Override to customize the behavior when a keep alive has been triggered and a keep alive request sent.
The default behavior is to log the event at debug level.
ctx
- the channel context.keepAliveRequest
- the keep alive request that was sent when keep alive was triggeredprotected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse)
Override to customize the behavior when a keep alive has been responded to.
The default behavior is to log the event and the response status at trace level.
ctx
- the channel context.keepAliveResponse
- the keep alive request that was sent when keep alive was triggeredprotected REQUEST currentRequest()
Returns the current request if set.
protected void currentRequest(REQUEST request)
Sets current request.
FIXME this is temporary solution for DCPHandler
request
- request to become the current oneprotected String remoteHostname()
protected CoreEnvironment env()
Returns environment.
protected AbstractEndpoint endpoint()
The parent endpoint.
protected static String logIdent(ChannelHandlerContext ctx, Endpoint endpoint)
Simple log helper to give logs a common prefix.
ctx
- the context.endpoint
- the endpoint.public static void addHttpBasicAuth(ChannelHandlerContext ctx, HttpRequest request, String user, String password)
Add basic authentication headers to a HttpRequest
.
The given information is Base64 encoded and the authorization header is set appropriately. Since this needs to be done for every request, it is refactored out.
ctx
- the handler context.request
- the request where the header should be added.user
- the username for auth.password
- the password for auth.protected String remoteHttpHost(ChannelHandlerContext ctx)
Helper method to return the remote http host, cached.
ctx
- the handler context.public DecodingState getDecodingState()
Copyright © 2017 Couchbase, Inc.. All rights reserved.