/* * 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; import static org.hedgecode.chess.scanner.ScannerConstants.*; /** * Aggregate the proxy parameters for {@link Proxy}. * * @author Dmitry Samoshin aka gotty */ public class ProxyParams { 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 ProxyType type; private boolean isSystem; private String host; private int port; private String user; private String password; public ProxyParams(String proxyServer, String proxyAuth, boolean isSystemProxy) { isSystem = isSystemProxy; Matcher matcher = PROXY_SERVER_PATTERN.matcher(proxyServer); if (matcher.find()) { type = ProxyType.byName(matcher.group(1)); host = matcher.group(2); port = Integer.parseInt(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 ProxyType getType() { return type; } public boolean isSystem() { return isSystem; } public String getHost() { return host; } public int getPort() { return port; } public String getUser() { return user; } public String getPassword() { return password; } @Override public String toString() { return String.format( "[%s] %s:%d %s", type, host, port, user != null ? String.format("(user: %s)", user) : "" ); } }