ProxyInfo.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.proxy;

  18. import javax.net.SocketFactory;

  19. /**
  20.  * Class which stores proxy information such as proxy type, host, port,
  21.  * authentication etc.
  22.  *
  23.  * @author Atul Aggarwal
  24.  */

  25. public class ProxyInfo
  26. {
  27.     public static enum ProxyType
  28.     {
  29.         NONE,
  30.         HTTP,
  31.         SOCKS4,
  32.         SOCKS5
  33.     }
  34.    
  35.     private String proxyAddress;
  36.     private int proxyPort;
  37.     private String proxyUsername;
  38.     private String proxyPassword;
  39.     private ProxyType proxyType;
  40.    
  41.     public ProxyInfo(   ProxyType pType, String pHost, int pPort, String pUser,
  42.                         String pPass)
  43.     {
  44.         this.proxyType = pType;
  45.         this.proxyAddress = pHost;
  46.         this.proxyPort = pPort;
  47.         this.proxyUsername = pUser;
  48.         this.proxyPassword = pPass;
  49.     }
  50.    
  51.     public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser,
  52.                                     String pPass)
  53.     {
  54.         return new ProxyInfo(ProxyType.HTTP, pHost, pPort, pUser, pPass);
  55.     }
  56.    
  57.     public static ProxyInfo forSocks4Proxy(String pHost, int pPort, String pUser,
  58.                                     String pPass)
  59.     {
  60.         return new ProxyInfo(ProxyType.SOCKS4, pHost, pPort, pUser, pPass);
  61.     }
  62.    
  63.     public static ProxyInfo forSocks5Proxy(String pHost, int pPort, String pUser,
  64.                                     String pPass)
  65.     {
  66.         return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass);
  67.     }
  68.    
  69.     public static ProxyInfo forNoProxy()
  70.     {
  71.         return new ProxyInfo(ProxyType.NONE, null, 0, null, null);
  72.     }
  73.    
  74.     public static ProxyInfo forDefaultProxy()
  75.     {
  76.         return new ProxyInfo(ProxyType.NONE, null, 0, null, null);
  77.     }
  78.    
  79.     public ProxyType getProxyType()
  80.     {
  81.         return proxyType;
  82.     }
  83.    
  84.     public String getProxyAddress()
  85.     {
  86.         return proxyAddress;
  87.     }
  88.    
  89.     public int getProxyPort()
  90.     {
  91.         return proxyPort;
  92.     }
  93.    
  94.     public String getProxyUsername()
  95.     {
  96.         return proxyUsername;
  97.     }
  98.    
  99.     public String getProxyPassword()
  100.     {
  101.         return proxyPassword;
  102.     }
  103.    
  104.     public SocketFactory getSocketFactory()
  105.     {
  106.         if(proxyType == ProxyType.NONE)
  107.         {
  108.             return new DirectSocketFactory();
  109.         }
  110.         else if(proxyType == ProxyType.HTTP)
  111.         {
  112.             return new HTTPProxySocketFactory(this);
  113.         }
  114.         else if(proxyType == ProxyType.SOCKS4)
  115.         {
  116.             return new Socks4ProxySocketFactory(this);
  117.         }
  118.         else if(proxyType == ProxyType.SOCKS5)
  119.         {
  120.             return new Socks5ProxySocketFactory(this);
  121.         }
  122.         else
  123.         {
  124.             return null;
  125.         }
  126.     }
  127. }