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.IoFilterAdapter;
23  import org.apache.mina.core.filterchain.IoFilterEvent;
24  import org.apache.mina.core.session.IdleStatus;
25  import org.apache.mina.core.session.IoEventType;
26  import org.apache.mina.core.session.IoSession;
27  import org.apache.mina.core.write.WriteRequest;
28  import org.apache.mina.filter.FilterEvent;
29  
30  /**
31   * Extend this class when you want to create a filter that
32   * wraps the same logic around all 11 IoEvents
33   * 
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public abstract class CommonEventFilter extends IoFilterAdapter {
37      protected abstract void filter(IoFilterEvent event) throws Exception;
38  
39      /**
40       * {@inheritDoc}
41       */
42      @Override
43      public void event(NextFilter nextFilter, IoSession session, FilterEvent event) throws Exception {
44          filter(new IoFilterEvent(nextFilter, IoEventType.EVENT, session, event));
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
52          filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause));
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
60          filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null));
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
68          filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest));
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public void inputClosed(NextFilter nextFilter, IoSession session) throws Exception {
76          filter(new IoFilterEvent(nextFilter, IoEventType.INPUT_CLOSED, session, null));
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
84          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message));
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
92          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest));
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
100         filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null));
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
108         filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null));
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
116         filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status));
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
124         filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null));
125     }
126 }