Package org.apache.mina.filter.codec
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
public class ProtocolCodecFilter extends IoFilterAdapter
AnIoFilterwhich translates binary or protocol specific data into message objects and vice versa usingProtocolCodecFactory,ProtocolEncoder, orProtocolDecoder.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classProtocolCodecFilter.EncodedWriteRequestprivate static classProtocolCodecFilter.ProtocolDecoderOutputImplprivate static classProtocolCodecFilter.ProtocolEncoderOutputImpl-
Nested classes/interfaces inherited from interface org.apache.mina.core.filterchain.IoFilter
IoFilter.NextFilter
-
-
Field Summary
Fields Modifier and Type Field Description private static AttributeKeyDECODERprivate static AttributeKeyDECODER_OUTprivate static IoBufferEMPTY_BUFFERprivate static java.lang.Class<?>[]EMPTY_PARAMSprivate static AttributeKeyENCODERprivate static AttributeKeyENCODER_OUTprivate ProtocolCodecFactoryfactoryThe factory responsible for creating the encoder and decoderprivate static org.slf4j.LoggerLOGGERA logger for this class
-
Constructor Summary
Constructors Constructor Description ProtocolCodecFilter(java.lang.Class<? extends ProtocolEncoder> encoderClass, java.lang.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private 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.ProtocolEncodergetEncoder(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, java.lang.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, java.lang.String name, IoFilter.NextFilter nextFilter)Invoked after this filter is removed from the specifiedparent.voidonPreAdd(IoFilterChain parent, java.lang.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 org.apache.mina.core.filterchain.IoFilterAdapter
destroy, event, exceptionCaught, filterClose, init, inputClosed, onPostAdd, onPreRemove, sessionCreated, sessionIdle, sessionOpened, toString
-
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
A logger for this class
-
EMPTY_PARAMS
private static final java.lang.Class<?>[] EMPTY_PARAMS
-
EMPTY_BUFFER
private static final IoBuffer EMPTY_BUFFER
-
ENCODER
private static final AttributeKey ENCODER
-
DECODER
private static final AttributeKey DECODER
-
DECODER_OUT
private static final AttributeKey DECODER_OUT
-
ENCODER_OUT
private static final AttributeKey ENCODER_OUT
-
factory
private final ProtocolCodecFactory factory
The factory responsible for creating the encoder and decoder
-
-
Constructor Detail
-
ProtocolCodecFilter
public ProtocolCodecFilter(ProtocolCodecFactory factory)
Creates a new instance of ProtocolCodecFilter, associating a factory for the creation of the encoder and decoder.- Parameters:
factory- The associated factory
-
ProtocolCodecFilter
public ProtocolCodecFilter(ProtocolEncoder encoder, ProtocolDecoder decoder)
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(java.lang.Class<? extends ProtocolEncoder> encoderClass, java.lang.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 Detail
-
getEncoder
public ProtocolEncoder getEncoder(IoSession session)
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, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
onPostRemove
public void onPostRemove(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
messageReceived
public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Object message) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
messageSent
public void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
filterWrite
public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
sessionClosed
public void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session) throws java.lang.Exception
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:
java.lang.Exception- If an error occurred while processing the event
-
disposeCodec
private void disposeCodec(IoSession session)
Dispose the encoder, decoder, and the callback for the decoded messages.
-
disposeEncoder
private void disposeEncoder(IoSession session)
Dispose the encoder, removing its instance from the session's attributes, and calling the associated dispose method.
-
disposeDecoder
private void disposeDecoder(IoSession session)
Dispose the decoder, removing its instance from the session's attributes, and calling the associated dispose method.
-
getDecoderOut
private ProtocolDecoderOutput getDecoderOut(IoSession session, IoFilter.NextFilter nextFilter)
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
private void disposeDecoderOut(IoSession session)
Remove the decoder callback from the session's attributes.
-
-