Class ProtocolCodecFilter
java.lang.Object
org.apache.mina.core.filterchain.IoFilterAdapter
org.apache.mina.filter.codec.ProtocolCodecFilter
- All Implemented Interfaces:
IoFilter
- Direct Known Subclasses:
HttpClientCodec, HttpServerCodec
An
IoFilter which translates binary or protocol specific data into
message objects and vice versa using ProtocolCodecFactory,
ProtocolEncoder, or ProtocolDecoder.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static classprivate static classNested classes/interfaces inherited from interface IoFilter
IoFilter.NextFilter -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final AttributeKeyprivate static final AttributeKeyprivate static final IoBufferprivate static final Class<?>[]private static final AttributeKeyprivate static final AttributeKeyprivate final ProtocolCodecFactoryThe factory responsible for creating the encoder and decoderprivate static final org.slf4j.LoggerA logger for this class -
Constructor Summary
ConstructorsConstructorDescriptionProtocolCodecFilter(Class<? extends ProtocolEncoder> encoderClass, Class<? extends ProtocolDecoder> decoderClass) Creates a new instance of ProtocolCodecFilter, without any factory.ProtocolCodecFilter(ProtocolCodecFactory factory) Creates a new instance of ProtocolCodecFilter, associating a factory for the creation of the encoder and decoder.ProtocolCodecFilter(ProtocolEncoder encoder, ProtocolDecoder decoder) Creates a new instance of ProtocolCodecFilter, without any factory. -
Method Summary
Modifier and TypeMethodDescriptionprivate voiddisposeCodec(IoSession session) Dispose the encoder, decoder, and the callback for the decoded messages.private voiddisposeDecoder(IoSession session) Dispose the decoder, removing its instance from the session's attributes, and calling the associated dispose method.private voiddisposeDecoderOut(IoSession session) Remove the decoder callback from the session's attributes.private voiddisposeEncoder(IoSession session) Dispose the encoder, removing its instance from the session's attributes, and calling the associated dispose method.voidfilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) FiltersIoSession.write(Object)method invocation.private ProtocolDecoderOutputgetDecoderOut(IoSession session, IoFilter.NextFilter nextFilter) Return a reference to the decoder callback.getEncoder(IoSession session) Get the encoder instance from a given session.private ProtocolEncoderOutputgetEncoderOut(IoSession session, IoFilter.NextFilter nextFilter, WriteRequest writeRequest) voidmessageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) Process the incoming message, calling the session decoder.voidmessageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) FiltersIoHandler.messageSent(IoSession,Object)event.voidonPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) Invoked after this filter is removed from the specifiedparent.voidonPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) Invoked before this filter is added to the specifiedparent.voidsessionClosed(IoFilter.NextFilter nextFilter, IoSession session) FiltersIoHandler.sessionClosed(IoSession)event.Methods inherited from class IoFilterAdapter
destroy, event, exceptionCaught, filterClose, init, inputClosed, onPostAdd, onPreRemove, sessionCreated, sessionIdle, sessionOpened, toString
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGERA logger for this class -
EMPTY_PARAMS
-
EMPTY_BUFFER
-
ENCODER
-
DECODER
-
DECODER_OUT
-
ENCODER_OUT
-
factory
The factory responsible for creating the encoder and decoder
-
-
Constructor Details
-
ProtocolCodecFilter
Creates a new instance of ProtocolCodecFilter, associating a factory for the creation of the encoder and decoder.- Parameters:
factory- The associated factory
-
ProtocolCodecFilter
Creates a new instance of ProtocolCodecFilter, without any factory. The encoder/decoder factory will be created as an inner class, using the two parameters (encoder and decoder).- Parameters:
encoder- The class responsible for encoding the messagedecoder- The class responsible for decoding the message
-
ProtocolCodecFilter
public ProtocolCodecFilter(Class<? extends ProtocolEncoder> encoderClass, Class<? extends ProtocolDecoder> decoderClass) Creates a new instance of ProtocolCodecFilter, without any factory. The encoder/decoder factory will be created as an inner class, using the two parameters (encoder and decoder), which are class names. Instances for those classes will be created in this constructor.- Parameters:
encoderClass- The class responsible for encoding the messagedecoderClass- The class responsible for decoding the message
-
-
Method Details
-
getEncoder
Get the encoder instance from a given session.- Parameters:
session- The associated session we will get the encoder from- Returns:
- The encoder instance, if any
-
onPreAdd
public void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception Invoked before this filter is added to the specifiedparent. Please note that this method can be invoked more than once if this filter is added to more than one parents. This method is not invoked beforeIoFilter.init()is invoked.- Specified by:
onPreAddin interfaceIoFilter- Overrides:
onPreAddin classIoFilterAdapter- Parameters:
parent- the parent who called this methodname- the name assigned to this filternextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.- Throws:
Exception- If an error occurred while processing the event
-
onPostRemove
public void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception Invoked after this filter is removed from the specifiedparent. Please note that this method can be invoked more than once if this filter is removed from more than one parents. This method is always invoked beforeIoFilter.destroy()is invoked.- Specified by:
onPostRemovein interfaceIoFilter- Overrides:
onPostRemovein classIoFilterAdapter- Parameters:
parent- the parent who called this methodname- the name assigned to this filternextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.- Throws:
Exception- If an error occurred while processing the event
-
messageReceived
public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) throws Exception Process the incoming message, calling the session decoder. As the incoming buffer might contains more than one messages, we have to loop until the decoder throws an exception. while ( buffer not empty ) try decode ( buffer ) catch break;- Specified by:
messageReceivedin interfaceIoFilter- Overrides:
messageReceivedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventmessage- The received message- Throws:
Exception- If an error occurred while processing the event
-
messageSent
public void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception FiltersIoHandler.messageSent(IoSession,Object)event.- Specified by:
messageSentin interfaceIoFilter- Overrides:
messageSentin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventwriteRequest- TheWriteRequestthat contains the sent message- Throws:
Exception- If an error occurred while processing the event
-
filterWrite
public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception FiltersIoSession.write(Object)method invocation.- Specified by:
filterWritein interfaceIoFilter- Overrides:
filterWritein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has to process this invocationwriteRequest- TheWriteRequestto process- Throws:
Exception- If an error occurred while processing the event
-
sessionClosed
FiltersIoHandler.sessionClosed(IoSession)event.- Specified by:
sessionClosedin interfaceIoFilter- Overrides:
sessionClosedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this event- Throws:
Exception- If an error occurred while processing the event
-
disposeCodec
Dispose the encoder, decoder, and the callback for the decoded messages. -
disposeEncoder
Dispose the encoder, removing its instance from the session's attributes, and calling the associated dispose method. -
disposeDecoder
Dispose the decoder, removing its instance from the session's attributes, and calling the associated dispose method. -
getDecoderOut
Return a reference to the decoder callback. If it's not already created and stored into the session, we create a new instance. -
getEncoderOut
private ProtocolEncoderOutput getEncoderOut(IoSession session, IoFilter.NextFilter nextFilter, WriteRequest writeRequest) -
disposeDecoderOut
Remove the decoder callback from the session's attributes.
-