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      /**
38       * {@inheritDoc}
39       */
40      @Override
41      public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
42          Object filteredMessage = doFilterWrite(nextFilter, session, writeRequest);
43          
44          if (filteredMessage != null && filteredMessage != writeRequest.getMessage()) {
45              nextFilter.filterWrite(session, new FilteredWriteRequest(filteredMessage, writeRequest));
46          } else {
47              nextFilter.filterWrite(session, writeRequest);
48          }
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
56          if (writeRequest instanceof FilteredWriteRequest) {
57              FilteredWriteRequest req = (FilteredWriteRequest) writeRequest;
58              
59              if (req.getParent() == this) {
60                  nextFilter.messageSent(session, req.getParentRequest());
61                  return;
62              }
63          }
64  
65          nextFilter.messageSent(session, writeRequest);
66      }
67  
68      protected abstract Object doFilterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
69              throws Exception;
70  
71      private class FilteredWriteRequest extends WriteRequestWrapper {
72          private final Object filteredMessage;
73  
74          public FilteredWriteRequest(Object filteredMessage, WriteRequest writeRequest) {
75              super(writeRequest);
76  
77              if (filteredMessage == null) {
78                  throw new IllegalArgumentException("filteredMessage");
79              }
80              this.filteredMessage = filteredMessage;
81          }
82  
83          public WriteRequestFilter getParent() {
84              return WriteRequestFilter.this;
85          }
86  
87          @Override
88          public Object getMessage() {
89              return filteredMessage;
90          }
91      }
92  }