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 javax.net.SocketFactory; 020 021/** 022 * Class which stores proxy information such as proxy type, host, port, 023 * authentication etc. 024 * 025 * @author Atul Aggarwal 026 */ 027 028public class ProxyInfo 029{ 030 public static enum ProxyType 031 { 032 NONE, 033 HTTP, 034 SOCKS4, 035 SOCKS5 036 } 037 038 private String proxyAddress; 039 private int proxyPort; 040 private String proxyUsername; 041 private String proxyPassword; 042 private ProxyType proxyType; 043 044 public ProxyInfo( ProxyType pType, String pHost, int pPort, String pUser, 045 String pPass) 046 { 047 this.proxyType = pType; 048 this.proxyAddress = pHost; 049 this.proxyPort = pPort; 050 this.proxyUsername = pUser; 051 this.proxyPassword = pPass; 052 } 053 054 public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser, 055 String pPass) 056 { 057 return new ProxyInfo(ProxyType.HTTP, pHost, pPort, pUser, pPass); 058 } 059 060 public static ProxyInfo forSocks4Proxy(String pHost, int pPort, String pUser, 061 String pPass) 062 { 063 return new ProxyInfo(ProxyType.SOCKS4, pHost, pPort, pUser, pPass); 064 } 065 066 public static ProxyInfo forSocks5Proxy(String pHost, int pPort, String pUser, 067 String pPass) 068 { 069 return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass); 070 } 071 072 public static ProxyInfo forNoProxy() 073 { 074 return new ProxyInfo(ProxyType.NONE, null, 0, null, null); 075 } 076 077 public static ProxyInfo forDefaultProxy() 078 { 079 return new ProxyInfo(ProxyType.NONE, null, 0, null, null); 080 } 081 082 public ProxyType getProxyType() 083 { 084 return proxyType; 085 } 086 087 public String getProxyAddress() 088 { 089 return proxyAddress; 090 } 091 092 public int getProxyPort() 093 { 094 return proxyPort; 095 } 096 097 public String getProxyUsername() 098 { 099 return proxyUsername; 100 } 101 102 public String getProxyPassword() 103 { 104 return proxyPassword; 105 } 106 107 public SocketFactory getSocketFactory() 108 { 109 if(proxyType == ProxyType.NONE) 110 { 111 return new DirectSocketFactory(); 112 } 113 else if(proxyType == ProxyType.HTTP) 114 { 115 return new HTTPProxySocketFactory(this); 116 } 117 else if(proxyType == ProxyType.SOCKS4) 118 { 119 return new Socks4ProxySocketFactory(this); 120 } 121 else if(proxyType == ProxyType.SOCKS5) 122 { 123 return new Socks5ProxySocketFactory(this); 124 } 125 else 126 { 127 return null; 128 } 129 } 130}