[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / system / SystemProxy.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.system;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.hedgecode.chess.scanner.proxy.ProxyParams;
23 import org.hedgecode.chess.scanner.proxy.ProxyType;
24
25 /**
26  * Setter for system proxy settings.
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public final class SystemProxy {
31
32     private static final Map<ProxyType, ProxySetter> setters = new HashMap<ProxyType, ProxySetter>() {
33         {
34             put( ProxyType.HTTP, new HttpSetter() );
35             put( ProxyType.SOCKS, new SocksSetter() );
36         }
37     };
38
39     public static void setProxy(ProxyParams proxyParams) {
40         setProxy(
41                 proxyParams.getType(),
42                 proxyParams.getHost(),
43                 proxyParams.getPort(),
44                 proxyParams.getUser(),
45                 proxyParams.getPassword()
46         );
47     }
48
49     public static void setProxy(ProxyType type, String host, int port) {
50         setProxy(type, host, port, null, null);
51     }
52
53     public static void setProxy(ProxyType type, String host, int port, String user, String password) {
54         ProxySetter proxySetter = setters.get(type);
55         if (proxySetter != null) {
56             proxySetter.setProxyHost(host);
57             proxySetter.setProxyPort(port);
58             if (user != null) {
59                 proxySetter.setProxyAuth(user, password);
60             }
61         }
62     }
63
64 }