View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.util;
21  
22  import org.apache.mina.core.filterchain.IoFilter;
23  import org.apache.mina.core.filterchain.IoFilterAdapter;
24  import org.apache.mina.core.session.IoEventType;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.core.write.WriteRequest;
27  import org.apache.mina.core.write.WriteRequestWrapper;
28  
29  /**
30   * An abstract {@link IoFilter} that simplifies the implementation of
31   * an {@link IoFilter} that filters an {@link IoEventType#WRITE} event.
32   *
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   *
35   */
36  public abstract class WriteRequestFilter extends IoFilterAdapter {
37      @Override
38      public void filterWrite(NextFilter nextFilter, IoSession session,
39              WriteRequest writeRequest) throws Exception {
40          Object filteredMessage = doFilterWrite(nextFilter, session, writeRequest);
41          if (filteredMessage != null && filteredMessage != writeRequest.getMessage()) {
42              nextFilter.filterWrite(
43                      session, new FilteredWriteRequest(
44                              filteredMessage, writeRequest));
45          } else {
46              nextFilter.filterWrite(session, writeRequest);
47          }
48      }
49  
50      @Override
51      public void messageSent(NextFilter nextFilter, IoSession session,
52              WriteRequest writeRequest) throws Exception {
53          if (writeRequest instanceof FilteredWriteRequest) {
54              FilteredWriteRequest req = (FilteredWriteRequest) writeRequest;
55              if (req.getParent() == this) {
56                  nextFilter.messageSent(session, req.getParentRequest());
57                  return;
58              }
59          }
60  
61          nextFilter.messageSent(session, writeRequest);
62      }
63  
64      protected abstract Object doFilterWrite(
65              NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception;
66  
67      private class FilteredWriteRequest extends WriteRequestWrapper {
68          private final Object filteredMessage;
69  
70          public FilteredWriteRequest(Object filteredMessage, WriteRequest writeRequest) {
71              super(writeRequest);
72  
73              if (filteredMessage == null) {
74                  throw new NullPointerException("filteredMessage");
75              }
76              this.filteredMessage = filteredMessage;
77          }
78  
79          public WriteRequestFilter getParent() {
80              return WriteRequestFilter.this;
81          }
82  
83          @Override
84          public Object getMessage() {
85              return filteredMessage;
86          }
87      }
88  }