Author: elecharny Date: Wed Nov 12 05:31:57 2008 New Revision: 713363 URL: http://svn.apache.org/viewvc?rev=713363&view=rev Log: More cleaning : - removed a useless check - moved all the helper methods to the helper section - added a method to get the ProtocolDecoderOutput from the session, if it exists, or create it and stores it into the session - added some javadoc
Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java?rev=713363&r1=713362&r2=713363&view=diff ============================================================================== --- mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java (original) +++ mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java Wed Nov 12 05:31:57 2008 @@ -55,6 +55,7 @@ private final AttributeKey ENCODER = new AttributeKey(getClass(), "encoder"); private final AttributeKey DECODER = new AttributeKey(getClass(), "decoder"); private final AttributeKey DECODER_OUT = new AttributeKey(getClass(), "decoderOut"); + private final AttributeKey ENCODER_OUT = new AttributeKey(getClass(), "encoderOut"); /** The factory responsible for creating the encoder and decoder */ private final ProtocolCodecFactory factory; @@ -168,16 +169,6 @@ return (ProtocolEncoder) session.getAttribute(ENCODER); } - /** - * Get the decoder instance from a given session. - * - * @param session The associated session we will get the decoder from - * @return The decoder instance - */ - public ProtocolDecoder getDecoder(IoSession session) { - return (ProtocolDecoder) session.getAttribute(DECODER); - } - @Override public void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { @@ -298,39 +289,15 @@ // Get the encoder in the session ProtocolEncoder encoder = getEncoder(session); - if ( encoder == null) { - // The encoder must not be null. It's null if - // the sessionCreated message has not be called, for - // instance if the filter has been added after the - // first session is created. - ProtocolDecoderException pde = new ProtocolDecoderException( - "Cannot encode if the encoder is null. Add the filter in the chain" + - "before the first session is created" ); - nextFilter.exceptionCaught(session, pde); - return; - } - - ProtocolEncoderOutputImpl encoderOut = getEncoderOut(session, + ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest); - if ( encoderOut == null) { - // The encoder must not be null. It's null if - // the sessionCreated message has not be called, for - // instance if the filter has been added after the - // first session is created. - ProtocolDecoderException pde = new ProtocolDecoderException( - "Cannot encode if the encoder is null. Add the filter in the chain" + - "before the first session is created" ); - nextFilter.exceptionCaught(session, pde); - return; - } - try { // Now we can try to encode the response encoder.encode(session, message, encoderOut); // Send it directly - encoderOut.flushWithoutFuture(); + ((ProtocolEncoderOutputImpl)encoderOut).flushWithoutFuture(); // Call the next filter nextFilter.filterWrite(session, new MessageWriteRequest( @@ -396,11 +363,6 @@ nextFilter.sessionClosed(session); } - private ProtocolEncoderOutputImpl getEncoderOut(IoSession session, - NextFilter nextFilter, WriteRequest writeRequest) { - return new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest); - } - private static class EncodedWriteRequest extends DefaultWriteRequest { private EncodedWriteRequest(Object encodedMessage, WriteFuture future, SocketAddress destination) { @@ -495,23 +457,6 @@ //----------- Helper methods --------------------------------------------- /** - * Return a reference to the decoder callback. If it's not already created - * and stored into the session, we create a new instance. - */ - private ProtocolDecoderOutput getDecoderOut(IoSession session, - NextFilter nextFilter) { - ProtocolDecoderOutput out = (ProtocolDecoderOutput) session.getAttribute(DECODER_OUT); - - if (out == null) { - // Create a new instance, and stores it into the session - out = new ProtocolDecoderOutputImpl(); - session.setAttribute(DECODER_OUT, out); - } - - return out; - } - - /** * Initialize the encoder and the decoder, storing them in the * session attributes. */ @@ -560,6 +505,16 @@ } /** + * Get the decoder instance from a given session. + * + * @param session The associated session we will get the decoder from + * @return The decoder instance + */ + private ProtocolDecoder getDecoder(IoSession session) { + return (ProtocolDecoder) session.getAttribute(DECODER); + } + + /** * dispose the decoder, removing its instance from the * session's attributes, and calling the associated * dispose method. @@ -580,6 +535,36 @@ } /** + * Return a reference to the decoder callback. If it's not already created + * and stored into the session, we create a new instance. + */ + private ProtocolDecoderOutput getDecoderOut(IoSession session, + NextFilter nextFilter) { + ProtocolDecoderOutput out = (ProtocolDecoderOutput) session.getAttribute(DECODER_OUT); + + if (out == null) { + // Create a new instance, and stores it into the session + out = new ProtocolDecoderOutputImpl(); + session.setAttribute(DECODER_OUT, out); + } + + return out; + } + + private ProtocolEncoderOutput getEncoderOut(IoSession session, + NextFilter nextFilter, WriteRequest writeRequest) { + ProtocolEncoderOutput out = (ProtocolEncoderOutput) session.getAttribute(ENCODER_OUT); + + if (out == null) { + // Create a new instance, and stores it into the session + out = new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest); + session.setAttribute(ENCODER_OUT, out); + } + + return out; + } + + /** * Remove the decoder callback from the session's attributes. */ private void disposeDecoderOut(IoSession session) {