[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / Proxy.java
index b54fece..f190008 100644 (file)
 
 package org.hedgecode.chess.scanner.proxy;
 
-import org.hedgecode.chess.scanner.proxy.type.HTTPSetter;
-import org.hedgecode.chess.scanner.proxy.type.SOCKSSetter;
-
-import static org.hedgecode.chess.scanner.ScannerConstants.*;
+import org.hedgecode.chess.scanner.proxy.client.HttpRequestClient;
+import org.hedgecode.chess.scanner.proxy.client.SocksRequestClient;
+import org.hedgecode.chess.scanner.proxy.system.SystemProxy;
+import org.hedgecode.chess.scanner.request.PlainRequestClient;
+import org.hedgecode.chess.scanner.request.RequestClient;
 
 /**
- * Setter for proxy settings.
+ * Proxy settings main class.
  *
  * @author Dmitry Samoshin aka gotty
  */
-public enum Proxy {
+public final class Proxy {
 
-    UNDEFINED ( PROXY_UNDEFINED, null              ),
-    HTTP      ( PROXY_HTTP,      new HTTPSetter()  ),
-    SOCKS     ( PROXY_SOCKS,     new SOCKSSetter() );
+    private static Proxy proxy;
 
-    private String name;
-    private ProxySetter setter;
+    private RequestClient requestClient;
 
-    Proxy(String name, ProxySetter setter) {
-        this.name = name;
-        this.setter = setter;
+    private Proxy() {
+        requestClient = new PlainRequestClient();
     }
 
-    private ProxySetter getSetter() {
-        return setter;
+    public static void setProxy(ProxyParams proxyParams) {
+        getProxy().set(proxyParams);
     }
 
-    public static Proxy byName(String name) {
-        for (Proxy proxy : Proxy.values()) {
-            if (proxy.name.equals(name))
-                return proxy;
-        }
-        return UNDEFINED;
+    public static RequestClient getRequestClient() {
+        return getProxy().client();
     }
 
-    public static void setProxy(ProxyParams proxyParams) {
-        setProxy(
-                proxyParams.getType(),
-                proxyParams.getHost(), proxyParams.getPort(),
-                proxyParams.getUser(), proxyParams.getPassword()
-        );
+    private void set(ProxyParams proxyParams) {
+        if (proxyParams.isSystem()) {
+            SystemProxy.setProxy(proxyParams);
+            requestClient = new PlainRequestClient();
+        } else {
+            switch (proxyParams.getType()) {
+                case HTTP:
+                    requestClient = new HttpRequestClient(proxyParams);
+                    break;
+                case SOCKS:
+                    requestClient = new SocksRequestClient(proxyParams);
+                    break;
+                default:
+                    requestClient = new PlainRequestClient();
+            }
+        }
     }
 
-    public static void setProxy(Proxy type, String host, String port) {
-        setProxy(type, host, port, null, null);
+    private RequestClient client() {
+        return requestClient;
     }
 
-    public static void setProxy(Proxy type, String host, String port, String user, String password) {
-        ProxySetter proxySetter = type.getSetter();
-        if (proxySetter != null) {
-            proxySetter.setProxyHost(host);
-            proxySetter.setProxyPort(port);
-            if (user != null) {
-                proxySetter.setProxyAuth(user, password);
-            }
+    private static Proxy getProxy() {
+        if (proxy == null) {
+            proxy = new Proxy();
         }
+        return proxy;
     }
 
 }