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.example.proxy;
21  
22  import java.nio.charset.Charset;
23  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.service.IoHandlerAdapter;
26  import org.apache.mina.core.session.IoSession;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Base class of {@link org.apache.mina.core.service.IoHandler} classes which handle
32   * proxied connections.
33   *
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
37      private static final Charset CHARSET = Charset.forName("iso8859-1");
38      public static final String OTHER_IO_SESSION = AbstractProxyIoHandler.class.getName()+".OtherIoSession";
39  
40      private final static Logger LOGGER = LoggerFactory.getLogger(AbstractProxyIoHandler.class);
41      
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      public void sessionCreated(IoSession session) throws Exception {
47          session.suspendRead();
48          session.suspendWrite();
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public void sessionClosed(IoSession session) throws Exception {
56          if (session.getAttribute( OTHER_IO_SESSION ) != null) {
57              IoSession sess = (IoSession) session.getAttribute(OTHER_IO_SESSION);
58              sess.setAttribute(OTHER_IO_SESSION, null);
59              sess.close(false);
60              session.setAttribute(OTHER_IO_SESSION, null);
61          }
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public void messageReceived(IoSession session, Object message)
69              throws Exception {
70          IoBuffer rb = (IoBuffer) message;
71          IoBuffer wb = IoBuffer.allocate(rb.remaining());
72          rb.mark();
73          wb.put(rb);
74          wb.flip();
75          ((IoSession) session.getAttribute(OTHER_IO_SESSION)).write(wb);
76          rb.reset();
77          LOGGER.info(rb.getString(CHARSET.newDecoder()));
78      }
79  }