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.example.proxy.telnet;
021
022import java.net.InetSocketAddress;
023import java.nio.charset.Charset;
024import java.util.HashMap;
025
026import org.apache.mina.core.RuntimeIoException;
027import org.apache.mina.core.future.ConnectFuture;
028import org.apache.mina.core.session.IoSession;
029import org.apache.mina.filter.codec.ProtocolCodecFilter;
030import org.apache.mina.filter.codec.textline.LineDelimiter;
031import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
032import org.apache.mina.proxy.ProxyConnector;
033import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
034import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
035import org.apache.mina.proxy.session.ProxyIoSession;
036import org.apache.mina.transport.socket.nio.NioSocketConnector;
037
038/**
039 * ProxyTelnetTestClient.java - Tests a classical text communication through a proxy.
040 * Changing the params and request type will allow to test the multiple options
041 * (http or socks proxying, various authentications methods, ...).
042 * 
043 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
044 * @since MINA 2.0.0-M3
045 */
046public class ProxyTelnetTestClient {
047    
048    /**
049     * The user login used to authenticate with the proxy.
050     */
051    public final static String USER = "TED_KODS";
052
053    /**
054     * The password used to authenticate with the proxy.
055     */
056    public final static String PWD = "EDOUARD";
057
058    /**
059     * The address we really want to connect to.
060     */
061    public final static InetSocketAddress serverAddress = new InetSocketAddress(
062            "localhost", 25);
063
064    /**
065     * The address of the proxy server.
066     */
067    public final static InetSocketAddress proxyAddress = new InetSocketAddress(
068            "localhost", 8080);
069    
070    /**
071     * Connects to the endpoint running a text based protocol server through the
072     * proxy and allows user to type commands in the console to dialog with the
073     * server.
074     * 
075     * @throws Exception
076     */
077    public ProxyTelnetTestClient() throws Exception {
078        // Create proxy connector.
079        NioSocketConnector targetConnector = new NioSocketConnector(Runtime
080                .getRuntime().availableProcessors() + 1);
081        ProxyConnector connector = new ProxyConnector(targetConnector);
082
083        /*
084        // Example of socks v5 proxy use
085        SocksProxyRequest req = new SocksProxyRequest(
086                SocksProxyConstants.SOCKS_VERSION_5,
087                SocksProxyConstants.ESTABLISH_TCPIP_STREAM, serverAddress, USER);
088        req.setPassword(PWD);
089        */
090
091        HttpProxyRequest req = new HttpProxyRequest(serverAddress);
092        HashMap<String, String> props = new HashMap<String, String>();
093        props.put(HttpProxyConstants.USER_PROPERTY, USER);
094        props.put(HttpProxyConstants.PWD_PROPERTY, PWD);
095        req.setProperties(props);        
096
097        ProxyIoSession proxyIoSession = new ProxyIoSession(proxyAddress, req);
098        connector.setProxyIoSession(proxyIoSession);
099
100        LineDelimiter delim = new LineDelimiter("\r\n");
101        targetConnector.getFilterChain().addLast(
102                "codec",
103                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
104                        .forName("UTF-8"), delim, delim)));
105
106        connector.setHandler(new TelnetSessionHandler());
107
108        IoSession session;
109        for (;;) {
110            try {
111                ConnectFuture future = connector.connect();
112                future.awaitUninterruptibly();
113                session = future.getSession();
114                break;
115            } catch (RuntimeIoException e) {
116                System.err.println("Failed to connect. Retrying in 5 secs ...");
117                Thread.sleep(5000);
118            }
119        }
120
121        // Wait until done
122        if (session != null) {
123            session.getCloseFuture().awaitUninterruptibly();
124        }
125        connector.dispose();
126        System.exit(0);
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    public static void main(String[] args) throws Exception {
133        new ProxyTelnetTestClient();
134    }
135}