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.common.ByteBuffer;
    25  import org.apache.mina.common.IoHandlerAdapter;
    26  import org.apache.mina.common.IoSession;
    27  import org.apache.mina.common.TrafficMask;
    28  import org.apache.mina.util.SessionLog;
    29
    30  /**
    31   * Base class of {@link org.apache.mina.common.IoHandler} classes which handle
    32   * proxied connections.
    33   *
    34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    35   * @version $Rev$, $Date$
    36   *
    37   */
    38  public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
    39      private static Charset CHARSET = Charset.forName("iso8859-1");
    40
    41      public void sessionCreated(IoSession session) throws Exception {
    42          session.setTrafficMask(TrafficMask.NONE);
    43      }
    44
    45      public void sessionClosed(IoSession session) throws Exception {
    46          if (session.getAttachment() != null) {
    47              ((IoSession) session.getAttachment()).setAttachment(null);
    48              ((IoSession) session.getAttachment()).close();
    49              session.setAttachment(null);
    50          }
    51      }
    52
    53      public void messageReceived(IoSession session, Object message)
    54              throws Exception {
    55          ByteBuffer rb = (ByteBuffer) message;
    56          ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
    57          rb.mark();
    58          wb.put(rb);
    59          wb.flip();
    60          ((IoSession) session.getAttachment()).write(wb);
    61          rb.reset();
    62          SessionLog.info(session, rb.getString(CHARSET.newDecoder()));
    63      }
    64  }