[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / ProxyParams.java
index 6dba49b..6802109 100644 (file)
@@ -19,6 +19,8 @@ package org.hedgecode.chess.scanner.proxy;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
+
 /**
  * Aggregate the proxy parameters for {@link Proxy}.
  *
@@ -26,29 +28,26 @@ import java.util.regex.Pattern;
  */
 public class ProxyParams {
 
-    private static final Pattern PROXY_SERVER_PATTERN = Pattern.compile(
-            "^([^:]+):([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}):([0-9]{1,5})$"
-    );
-
-    private static final Pattern PROXY_AUTH_PATTERN = Pattern.compile(
-            "^([^:]+):(.+)$"
-    );
+    private static final Pattern PROXY_SERVER_PATTERN = Pattern.compile(PROXY_SERVER_REGEX);
+    private static final Pattern PROXY_AUTH_PATTERN = Pattern.compile(PROXY_AUTH_REGEX);
 
-    private Proxy type;
+    private ProxyType type;
+    private boolean isSystem;
 
     private String host;
-    private String port;
+    private int port;
 
-    private String user = null;
-    private String password = null;
+    private String user;
+    private String password;
 
-    public ProxyParams(String proxyServer, String proxyAuth) {
+    public ProxyParams(String proxyServer, String proxyAuth, boolean isSystemProxy) {
+        isSystem = isSystemProxy;
 
         Matcher matcher = PROXY_SERVER_PATTERN.matcher(proxyServer);
         if (matcher.find()) {
-            type = Proxy.byName(matcher.group(1));
+            type = ProxyType.byName(matcher.group(1));
             host = matcher.group(2);
-            port = matcher.group(3);
+            port = Integer.parseInt(matcher.group(3));
         }
 
         if (proxyAuth != null && !proxyAuth.isEmpty()) {
@@ -60,29 +59,19 @@ public class ProxyParams {
         }
     }
 
-    public ProxyParams(Proxy type, String host, String port) {
-        this.type = type;
-        this.host = host;
-        this.port = port;
-    }
-
-    public ProxyParams(Proxy type, String host, String port, String user, String password) {
-        this.type = type;
-        this.host = host;
-        this.port = port;
-        this.user = user;
-        this.password = password;
+    public ProxyType getType() {
+        return type;
     }
 
-    public Proxy getType() {
-        return type;
+    public boolean isSystem() {
+        return isSystem;
     }
 
     public String getHost() {
         return host;
     }
 
-    public String getPort() {
+    public int getPort() {
         return port;
     }
 
@@ -97,11 +86,11 @@ public class ProxyParams {
     @Override
     public String toString() {
         return String.format(
-                "[%s] %s:%s%s",
+                "[%s] %s:%%s",
                 type,
                 host,
                 port,
-                user != null ? " (with auth)" : ""
+                user != null ? String.format("(user: %s)", user)  : ""
         );
     }