Class AbstractGenericHandler<RESPONSE,ENCODED,REQUEST extends CouchbaseRequest>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.codec.MessageToMessageCodec<RESPONSE,REQUEST>
-
- com.couchbase.client.core.endpoint.AbstractGenericHandler<RESPONSE,ENCODED,REQUEST>
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
AnalyticsHandler
,AnalyticsHandlerV2
,ConfigHandler
,KeyValueHandler
,QueryHandler
,QueryHandlerV2
,SearchHandler
,ViewHandler
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.- Since:
- 1.0
- Author:
- Michael Nitschinger
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description protected static Charset
CHARSET
The default charset to use for all requests and responses.protected static byte[]
EMPTY_BYTES
Empty bytes to reuse.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, boolean isTransient, boolean pipeline)
Creates a newAbstractGenericHandler
with the default queue.protected
AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, Queue<REQUEST> queue, boolean isTransient, boolean pipeline)
Creates a newAbstractGenericHandler
with a custom queue.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static void
addHttpBasicAuth(ChannelHandlerContext ctx, HttpRequest request, String user, String password)
Add basic authentication headers to aHttpRequest
.void
channelActive(ChannelHandlerContext ctx)
void
channelInactive(ChannelHandlerContext ctx)
void
channelWritabilityChanged(ChannelHandlerContext ctx)
protected void
completeRequestSpan(CouchbaseRequest request)
Helper method to complete the request span, called from child instances.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 io.opentracing.Span
currentDispatchSpan()
protected REQUEST
currentRequest()
Returns the current request if set.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 aCouchbaseResponse
.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 RedactableArgument
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, rx.subjects.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 theServiceType
associated with this handler.boolean
shouldSendKeepAlive()
Helper method to check if conditions are met to send a keepalive right now.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)
-
Methods inherited from class io.netty.handler.codec.MessageToMessageCodec
acceptInboundMessage, acceptOutboundMessage, channelRead
-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelReadComplete, channelRegistered, channelUnregistered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
-
-
-
Field Detail
-
CHARSET
protected static final Charset CHARSET
The default charset to use for all requests and responses.
-
EMPTY_BYTES
protected static final byte[] EMPTY_BYTES
Empty bytes to reuse.
-
-
Constructor Detail
-
AbstractGenericHandler
protected AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, boolean isTransient, boolean pipeline)
Creates a newAbstractGenericHandler
with the default queue.- Parameters:
endpoint
- the endpoint reference.responseBuffer
- the response buffer.
-
AbstractGenericHandler
protected AbstractGenericHandler(AbstractEndpoint endpoint, com.lmax.disruptor.EventSink<ResponseEvent> responseBuffer, Queue<REQUEST> queue, boolean isTransient, boolean pipeline)
Creates a newAbstractGenericHandler
with a custom queue.- Parameters:
endpoint
- the endpoint reference.responseBuffer
- the response buffer.queue
- the queue.
-
-
Method Detail
-
encodeRequest
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.- Parameters:
ctx
- the context passed in.msg
- the outgoing message.- Returns:
- the encoded request.
- Throws:
Exception
- as a generic error.
-
decodeResponse
protected abstract CouchbaseResponse decodeResponse(ChannelHandlerContext ctx, RESPONSE msg) throws Exception
Decodes the incoming response and transforms it into aCouchbaseResponse
. Note that the actual notification is handled by this generic handler, the implementing class only is concerned about the conversion itself.- Parameters:
ctx
- the context passed in.msg
- the incoming message.- Returns:
- a response or null if nothing should be returned.
- Throws:
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.
-
serviceType
protected abstract ServiceType serviceType()
Returns theServiceType
associated with this handler.- Returns:
- the service type.
-
write
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classMessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
- Throws:
Exception
-
encode
protected void encode(ChannelHandlerContext ctx, REQUEST msg, List<Object> out) throws Exception
- Specified by:
encode
in classMessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
- Throws:
Exception
-
decode
protected void decode(ChannelHandlerContext ctx, RESPONSE msg, List<Object> out) throws Exception
- Specified by:
decode
in classMessageToMessageCodec<RESPONSE,REQUEST extends CouchbaseRequest>
- Throws:
Exception
-
currentDispatchSpan
protected io.opentracing.Span currentDispatchSpan()
-
publishResponse
protected void publishResponse(CouchbaseResponse response, rx.subjects.Subject<CouchbaseResponse,CouchbaseResponse> observable)
Publishes a response with the attached observable.- Parameters:
response
- the response to publish.observable
- pushing into the event sink.
-
finishedDecoding
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.
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceChannelInboundHandler
- Overrides:
channelActive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
- Specified by:
channelWritabilityChanged
in interfaceChannelInboundHandler
- Overrides:
channelWritabilityChanged
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
connect
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise future) throws Exception
- Specified by:
connect
in interfaceChannelOutboundHandler
- Overrides:
connect
in classChannelDuplexHandler
- Throws:
Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
sideEffectRequestToCancel
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.- Parameters:
request
- the request to side effect on.
-
userEventTriggered
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
- Specified by:
userEventTriggered
in interfaceChannelInboundHandler
- Overrides:
userEventTriggered
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
shouldSendKeepAlive
public boolean shouldSendKeepAlive()
Helper method to check if conditions are met to send a keepalive right now.- Returns:
- true if keepalive can be sent, false otherwise.
-
createKeepAliveRequest
protected CouchbaseRequest createKeepAliveRequest()
Override to return a non-null request to be fired in the pipeline in case a keep alive is triggered.- Returns:
- a CouchbaseRequest to be fired in case of keep alive (null by default).
-
onKeepAliveFired
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.- Parameters:
ctx
- the channel context.keepAliveRequest
- the keep alive request that was sent when keep alive was triggered
-
onKeepAliveResponse
protected 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.- Parameters:
ctx
- the channel context.keepAliveResponse
- the keep alive request that was sent when keep alive was triggered
-
currentRequest
protected REQUEST currentRequest()
Returns the current request if set.- Returns:
- the current request.
-
remoteHostname
protected String remoteHostname()
- Returns:
- stringified version of the remote node's hostname
-
env
protected CoreEnvironment env()
Returns environment.- Returns:
- the environment
-
endpoint
protected AbstractEndpoint endpoint()
The parent endpoint.
-
logIdent
protected static RedactableArgument logIdent(ChannelHandlerContext ctx, Endpoint endpoint)
Simple log helper to give logs a common prefix.- Parameters:
ctx
- the context.endpoint
- the endpoint.- Returns:
- a prefix string for logs.
-
addHttpBasicAuth
public static void addHttpBasicAuth(ChannelHandlerContext ctx, HttpRequest request, String user, String password)
Add basic authentication headers to aHttpRequest
. 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.- Parameters:
ctx
- the handler context.request
- the request where the header should be added.user
- the username for auth.password
- the password for auth.
-
completeRequestSpan
protected void completeRequestSpan(CouchbaseRequest request)
Helper method to complete the request span, called from child instances.- Parameters:
request
- the corresponding request.
-
remoteHttpHost
protected String remoteHttpHost(ChannelHandlerContext ctx)
Helper method to return the remote http host, cached.- Parameters:
ctx
- the handler context.- Returns:
- the remote http host.
-
getDecodingState
public DecodingState getDecodingState()
-
-