+/*
+ * Copyright (c) 2019-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.scanner.proxy;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Aggregate the proxy parameters for {@link Proxy}.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+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 Proxy type;
+
+ private String host;
+ private String port;
+
+ private String user = null;
+ private String password = null;
+
+ public ProxyParams(String proxyServer, String proxyAuth) {
+
+ Matcher matcher = PROXY_SERVER_PATTERN.matcher(proxyServer);
+ if (matcher.find()) {
+ type = Proxy.byName(matcher.group(1));
+ host = matcher.group(2);
+ port = matcher.group(3);
+ }
+
+ if (proxyAuth != null && !proxyAuth.isEmpty()) {
+ matcher = PROXY_AUTH_PATTERN.matcher(proxyAuth);
+ if (matcher.find()) {
+ user = matcher.group(1);
+ password = matcher.group(2);
+ }
+ }
+ }
+
+ 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 Proxy getType() {
+ return type;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public String getPort() {
+ return port;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public String toString() {
+ return String.format(
+ "[%s] %s:%s%s",
+ type,
+ host,
+ port,
+ user != null ? " (with auth)" : ""
+ );
+ }
+
+}