001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.proxy;
018
019import java.net.InetSocketAddress;
020import java.net.Proxy;
021import java.net.SocketAddress;
022
023/**
024 * Class which stores proxy information such as proxy type, host, port,
025 * authentication etc.
026 *
027 * @author Atul Aggarwal
028 */
029
030public class ProxyInfo {
031    public enum ProxyType {
032        HTTP,
033        SOCKS4,
034        SOCKS5
035    }
036
037    private String proxyAddress;
038    private int proxyPort;
039    private String proxyUsername;
040    private String proxyPassword;
041    private ProxyType proxyType;
042    private final ProxySocketConnection proxySocketConnection;
043
044    public ProxyInfo(ProxyType pType, String pHost, int pPort, String pUser,
045                        String pPass) {
046        this.proxyType = pType;
047        this.proxyAddress = pHost;
048        this.proxyPort = pPort;
049        this.proxyUsername = pUser;
050        this.proxyPassword = pPass;
051        this.proxySocketConnection =
052                ProxySocketConnection
053                        .forProxyType(proxyType)
054                        .apply(this);
055    }
056
057    public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser,
058                                    String pPass) {
059        return new ProxyInfo(ProxyType.HTTP, pHost, pPort, pUser, pPass);
060    }
061
062    public static ProxyInfo forSocks4Proxy(String pHost, int pPort, String pUser,
063                                    String pPass) {
064        return new ProxyInfo(ProxyType.SOCKS4, pHost, pPort, pUser, pPass);
065    }
066
067    public static ProxyInfo forSocks5Proxy(String pHost, int pPort, String pUser,
068                                    String pPass) {
069        return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass);
070    }
071
072    public Proxy.Type getJavaProxyType() {
073        switch (proxyType) {
074            case HTTP:
075                return Proxy.Type.HTTP;
076            case SOCKS4:
077            case SOCKS5:
078                return Proxy.Type.SOCKS;
079            default:
080                throw new AssertionError("Invalid proxy type: " + proxyType);
081        }
082    }
083
084    public ProxyType getProxyType() {
085        return proxyType;
086    }
087
088    public String getProxyAddress() {
089        return proxyAddress;
090    }
091
092    public int getProxyPort() {
093        return proxyPort;
094    }
095
096    public String getProxyUsername() {
097        return proxyUsername;
098    }
099
100    public String getProxyPassword() {
101        return proxyPassword;
102    }
103
104    public ProxySocketConnection getProxySocketConnection() {
105        return proxySocketConnection;
106    }
107
108    public Proxy toJavaProxy() {
109        final SocketAddress proxySocketAddress = new InetSocketAddress(proxyAddress, proxyPort);
110        final Proxy.Type type = getJavaProxyType();
111        return new Proxy(type, proxySocketAddress);
112    }
113
114}