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 @SuppressWarnings("this-escape") 045 public ProxyInfo(ProxyType pType, String pHost, int pPort, String pUser, 046 String pPass) { 047 this.proxyType = pType; 048 this.proxyAddress = pHost; 049 this.proxyPort = pPort; 050 this.proxyUsername = pUser; 051 this.proxyPassword = pPass; 052 this.proxySocketConnection = 053 ProxySocketConnection 054 .forProxyType(proxyType) 055 .apply(this); 056 } 057 058 public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser, 059 String pPass) { 060 return new ProxyInfo(ProxyType.HTTP, pHost, pPort, pUser, pPass); 061 } 062 063 public static ProxyInfo forSocks4Proxy(String pHost, int pPort, String pUser, 064 String pPass) { 065 return new ProxyInfo(ProxyType.SOCKS4, pHost, pPort, pUser, pPass); 066 } 067 068 public static ProxyInfo forSocks5Proxy(String pHost, int pPort, String pUser, 069 String pPass) { 070 return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass); 071 } 072 073 public Proxy.Type getJavaProxyType() { 074 switch (proxyType) { 075 case HTTP: 076 return Proxy.Type.HTTP; 077 case SOCKS4: 078 case SOCKS5: 079 return Proxy.Type.SOCKS; 080 default: 081 throw new AssertionError("Invalid proxy type: " + proxyType); 082 } 083 } 084 085 public ProxyType getProxyType() { 086 return proxyType; 087 } 088 089 public String getProxyAddress() { 090 return proxyAddress; 091 } 092 093 public int getProxyPort() { 094 return proxyPort; 095 } 096 097 public String getProxyUsername() { 098 return proxyUsername; 099 } 100 101 public String getProxyPassword() { 102 return proxyPassword; 103 } 104 105 public ProxySocketConnection getProxySocketConnection() { 106 return proxySocketConnection; 107 } 108 109 public Proxy toJavaProxy() { 110 final SocketAddress proxySocketAddress = new InetSocketAddress(proxyAddress, proxyPort); 111 final Proxy.Type type = getJavaProxyType(); 112 return new Proxy(type, proxySocketAddress); 113 } 114 115}