001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.proxy;
021
022import org.apache.mina.core.service.IoHandler;
023import org.apache.mina.core.service.IoHandlerAdapter;
024import org.apache.mina.core.session.IoSession;
025import org.apache.mina.proxy.handlers.socks.SocksProxyRequest;
026import org.apache.mina.proxy.session.ProxyIoSession;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * AbstractProxyIoHandler.java - {@link IoHandler} that intercepts events until handshake is complete.
032 * 
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 * @since MINA 2.0.0-M3
035 */
036public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
037    private final static Logger logger = LoggerFactory.getLogger(AbstractProxyIoHandler.class);
038
039    /**
040     * Method called only when handshake has completed.
041     * 
042     * @param session the io session
043     * @throws Exception If the proxy session can't be opened
044     */
045    public abstract void proxySessionOpened(IoSession session) throws Exception;
046
047    /**
048     * Hooked session opened event.
049     * 
050     * @param session the io session
051     */
052    @Override
053    public final void sessionOpened(IoSession session) throws Exception {
054        ProxyIoSession proxyIoSession = (ProxyIoSession) session.getAttribute(ProxyIoSession.PROXY_SESSION);
055
056        if (proxyIoSession.getRequest() instanceof SocksProxyRequest || proxyIoSession.isAuthenticationFailed()
057                || proxyIoSession.getHandler().isHandshakeComplete()) {
058            proxySessionOpened(session);
059        } else {
060            logger.debug("Filtered session opened event !");
061        }
062    }
063}