You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Trustin Lee (JIRA)" <ji...@apache.org> on 2007/04/11 15:36:32 UTC

[jira] Updated: (DIRMINA-368) IoFilter.messageSent should have access to WriteRequest instead of the written message.

     [ https://issues.apache.org/jira/browse/DIRMINA-368?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Trustin Lee updated DIRMINA-368:
--------------------------------

    Description: 
IoFilter.filterWrite() accepts WriteRequest as its parameter to allow an IoFilter implementation to alter the WriteRequest or create a new WriteRequest.  This is particularly useful when the filter transforms a received message.  Good examples of this scenario are ProtocolCodecFilter and SSLFilter.

Because these examples are wrapping an outgoing message with an internal type, the filter implementor was able to get the original message by unwrapping in IoFilter.messageSent() like the following:

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        nextFilter.filterWrite(session, new WriteRequest(new TransformedMessage(req.getMessage())));
    }

    public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
        if (message instanceof TransformedMessage) {
            nextFilter.messageSent(session, ((TransformedMessage) message).getOriginalMessage());
            return;
        }
        ......
    }

But, what if the transformation is not wrapping but unwrapping?  We can't perform the reverse-transformation like we did in the example above, because we don't know what the original message was in IoFilter.messageSent().

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        nextFilter.filterWrite(session, new WriteRequest(
                ((OuterMessage) req.getMessage()).getInnerMessage()));  // Unwrap on write.
    }

    public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
        if (message instanceof InnerMessage) {
            // TODO Transform the message back to OuterMessage.  But how?
        }
        ......
    }

To fix this problem, there should be at least one common type that any IoFilter implementation can understand, and that type has to be the parameter of IoFilter.messageSent().  A good candidate is WriteRequest, which is already exposed in the IoFilter interface.  With this change applied, the IoFilter implementation will look like the following:

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        // Wrap on write.
        nextFilter.filterWrite(session, new TransformedWriteRequest(request));
    }

    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest request) {
        if (request instanceof TransformedWriteRequest) {
            // Unwrap on messageSent.
            nextFilter.messageSent(session, ((TransformedWriteRequest) request).getWriteRequest());
            return;
        }
        ......
    }

    private static class TransformedWriteRequest extends WriteRequestWrapper {
        private TransformedWriteRequest(WriteRequest req) {
            super(req);
        }

        public Object getMessage() {
            // Wrapping with TransformedWriteRequest is actually unwrapping the message.
            // and unwrapping this write request is actually wrapping the message back.
            return ((OuterMessage) super.getMessage()).getInnerMessage();
        }
    }

  was:
IoFilter.filterWrites accepts WriteRequest as its parameter to allow an IoFilter implementation to alter the WriteRequest or create a new WriteRequest.  This is particularly useful when the filter transforms a received message.  Good examples of this scenario are ProtocolCodecFilter and SSLFilter.

Because these examples are wrapping an outgoing message with an internal type, the filter implementor was able to get the original message by unwrapping in IoFilter.messageSent() like the following:

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        nextFilter.filterWrite(session, new WriteRequest(new TransformedMessage(req.getMessage())));
    }

    public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
        if (message instanceof TransformedMessage) {
            nextFilter.messageSent(session, ((TransformedMessage) message).getOriginalMessage());
            return;
        }
        ......
    }

But, what if the transformation is not wrapping but unwrapping?  We can't perform the reverse-transformation like we did in the example above, because we don't know what the original message was in IoFilter.messageSent().

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        nextFilter.filterWrite(session, new WriteRequest(
                ((MyMessage) req.getMessage()).getRealMessage()));  // Unwrap on write.
    }

    public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
        if (message instanceof RealMessage) {
            // TODO Transform the message back to MyMessage.  But how?
        }
        ......
    }

To fix this problem, there should be at least one common type that any IoFilter implementation can understand, and that type has to be the parameter of IoFilter.messageSent().  A good candidate is WriteRequest, which is already exposed in the IoFilter interface.  With this change applied, the IoFilter implementation will look like the following:

    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
        // Wrap on write.
        nextFilter.filterWrite(session, new TransformedWriteRequest(request));
    }

    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest request) {
        if (request instanceof TransformedWriteRequest) {
            // Unwrap on messageSent.
            nextFilter.messageSent(session, ((TransformedWriteRequest) request).getWriteRequest());
            return;
        }
        ......
    }

    private static class TransformedWriteRequest extends WriteRequestWrapper {
        private TransformedWriteRequest(WriteRequest req) {
            super(req);
        }

        public Object getMessage() {
            // Wrapping with TransformedWriteRequest is actually unwrapping the message.
            // and unwrapping this write request is actually wrapping the message back.
            return ((OuterMessage) super.getMessage()).getInnerMessage();
        }
    }


> IoFilter.messageSent should have access to WriteRequest instead of the written message.
> ---------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-368
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-368
>             Project: MINA
>          Issue Type: Improvement
>          Components: Core
>            Reporter: Trustin Lee
>         Assigned To: Trustin Lee
>             Fix For: 2.0.0-M1
>
>
> IoFilter.filterWrite() accepts WriteRequest as its parameter to allow an IoFilter implementation to alter the WriteRequest or create a new WriteRequest.  This is particularly useful when the filter transforms a received message.  Good examples of this scenario are ProtocolCodecFilter and SSLFilter.
> Because these examples are wrapping an outgoing message with an internal type, the filter implementor was able to get the original message by unwrapping in IoFilter.messageSent() like the following:
>     public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
>         nextFilter.filterWrite(session, new WriteRequest(new TransformedMessage(req.getMessage())));
>     }
>     public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
>         if (message instanceof TransformedMessage) {
>             nextFilter.messageSent(session, ((TransformedMessage) message).getOriginalMessage());
>             return;
>         }
>         ......
>     }
> But, what if the transformation is not wrapping but unwrapping?  We can't perform the reverse-transformation like we did in the example above, because we don't know what the original message was in IoFilter.messageSent().
>     public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
>         nextFilter.filterWrite(session, new WriteRequest(
>                 ((OuterMessage) req.getMessage()).getInnerMessage()));  // Unwrap on write.
>     }
>     public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
>         if (message instanceof InnerMessage) {
>             // TODO Transform the message back to OuterMessage.  But how?
>         }
>         ......
>     }
> To fix this problem, there should be at least one common type that any IoFilter implementation can understand, and that type has to be the parameter of IoFilter.messageSent().  A good candidate is WriteRequest, which is already exposed in the IoFilter interface.  With this change applied, the IoFilter implementation will look like the following:
>     public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest req) {
>         // Wrap on write.
>         nextFilter.filterWrite(session, new TransformedWriteRequest(request));
>     }
>     public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest request) {
>         if (request instanceof TransformedWriteRequest) {
>             // Unwrap on messageSent.
>             nextFilter.messageSent(session, ((TransformedWriteRequest) request).getWriteRequest());
>             return;
>         }
>         ......
>     }
>     private static class TransformedWriteRequest extends WriteRequestWrapper {
>         private TransformedWriteRequest(WriteRequest req) {
>             super(req);
>         }
>         public Object getMessage() {
>             // Wrapping with TransformedWriteRequest is actually unwrapping the message.
>             // and unwrapping this write request is actually wrapping the message back.
>             return ((OuterMessage) super.getMessage()).getInnerMessage();
>         }
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.