You will have to provide your own ProtocolDecoderOutput which delegates to the real one after inspecting the output from the TextLineDecoder:

public class MyDecoder implements ProtocolDecoder {
 private final TextLineDecoder textDecoder;

public void decode(IoSession session, ByteBuffer in, final ProtocolDecoderOutput out) {
   textDecoder.decode(session, in, new ProtocolDecoderOutput () {
     public void write(Object message) {
       if (((String) message).startsWith("attachment:") {
         ...
       }
       out.write(message);
     }
   });
 }
}

Yes, I know it's a bit of a mess. :-) However, this is how I have done similar things before and it has worked.

/Niklas

Johan Haleby skrev:
Hi,

Thanks for your reply. However I don't think I can delegate to the
TextLineDecoder from my decoder since TextLineDecoder doesn't return any
input, it just writes the data to a ProtocolDecoderOutput. And you cannot
seem to get any data from the ProtocolDecoderOutput, you can only write to
it. Or is there a way to get the data from another decoder?

Thanks,
Johan

On Thu, Mar 6, 2008 at 11:31 AM, Niklas Therning <[EMAIL PROTECTED]> wrote:

Johan Haleby skrev:
Hi,

I'm trying to implement a protocol that looks like this in Apache Mina (
1.x
):
1: The client send a text line message ending with a newline.
2: Optional binary data sent as raw byte data.

Depending on the content of the string in 1 the codec in 2 should read
the
raw data or not. For example, if the text-line in 1 ends with
"attachment:<size>" the codec should expect to read binary data of size
<size>, otherwise it should not expect any more data. My first thought
was
to reuse the TextLineDecoder in step1 and then delegate to another
decoder
that inspects the content of the first step and see if it should read
the
raw data or not. This codec could then assemble the whole attachment as
a
byte[] in memory and put it in a key in the session so that fitlers can
take
care of assembling the domain object (unfortunately I have to store the
whole attachment in memory for other reasons, i.e. I cannot just
 delegate
the chunks). Does this seem like a feasible solution? I cannot
understand
how to use multiple decoders in this way though and I don't think I can
use
a filter for step 2 right? The only solution I have right now is to copy
parts of the TextLineDecoder into my own decoder which does both 1 & 2.
But
this doesn't seem right due to bad reuse?

Thanks,
Johan


IIRC there was some problem with using many ProtocolCodecFilters in your
filter chain in MINA 1.x. Someone else may have more info on that.

I think you could do this as one decoder yet use the TextLineDecoder.
Just create your own Decoder which delegates to a TextLineDecoder for
the most part. Your Decoder inspects what the TextLineDecoder returns.
If it is "attachment:size" you will temporarily disable the delegation
to the TextLineDecoder for size bytes and do whatever you need to do
with the binary data. After size bytes have been read the
TextLineDeocder will be used again.

HTH

/Niklas




Reply via email to