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