[LIB-13] Add classes to work with proxy servers
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / ProxyParams.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.scanner.proxy;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 /**
23  * Aggregate the proxy parameters for {@link Proxy}.
24  *
25  * @author Dmitry Samoshin aka gotty
26  */
27 public class ProxyParams {
28
29     private static final Pattern PROXY_SERVER_PATTERN = Pattern.compile(
30             "^([^:]+):([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}):([0-9]{1,5})$"
31     );
32
33     private static final Pattern PROXY_AUTH_PATTERN = Pattern.compile(
34             "^([^:]+):(.+)$"
35     );
36
37     private Proxy type;
38
39     private String host;
40     private String port;
41
42     private String user = null;
43     private String password = null;
44
45     public ProxyParams(String proxyServer, String proxyAuth) {
46
47         Matcher matcher = PROXY_SERVER_PATTERN.matcher(proxyServer);
48         if (matcher.find()) {
49             type = Proxy.byName(matcher.group(1));
50             host = matcher.group(2);
51             port = matcher.group(3);
52         }
53
54         if (proxyAuth != null && !proxyAuth.isEmpty()) {
55             matcher = PROXY_AUTH_PATTERN.matcher(proxyAuth);
56             if (matcher.find()) {
57                 user = matcher.group(1);
58                 password = matcher.group(2);
59             }
60         }
61     }
62
63     public ProxyParams(Proxy type, String host, String port) {
64         this.type = type;
65         this.host = host;
66         this.port = port;
67     }
68
69     public ProxyParams(Proxy type, String host, String port, String user, String password) {
70         this.type = type;
71         this.host = host;
72         this.port = port;
73         this.user = user;
74         this.password = password;
75     }
76
77     public Proxy getType() {
78         return type;
79     }
80
81     public String getHost() {
82         return host;
83     }
84
85     public String getPort() {
86         return port;
87     }
88
89     public String getUser() {
90         return user;
91     }
92
93     public String getPassword() {
94         return password;
95     }
96
97     @Override
98     public String toString() {
99         return String.format(
100                 "[%s] %s:%s%s",
101                 type,
102                 host,
103                 port,
104                 user != null ? " (with auth)" : ""
105         );
106     }
107
108 }