X-Git-Url: https://git.hedgecode.org/?p=chesshog-scanner.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fproxy%2FProxy.java;h=f190008a20b4b95d79236c588dff5283fa62650f;hp=b54feceb3d7cdd95f94ca0f1ee93a55234fd9122;hb=70d45ca74e19c98d0bef2a081f77fc1e3543691e;hpb=6e3a8590a26312b6cea579777db885107cae88df diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java b/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java index b54fece..f190008 100644 --- a/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java @@ -16,63 +16,62 @@ 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; } }