[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / Proxy.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 org.hedgecode.chess.scanner.proxy.client.HttpRequestClient;
20 import org.hedgecode.chess.scanner.proxy.client.SocksRequestClient;
21 import org.hedgecode.chess.scanner.proxy.system.SystemProxy;
22 import org.hedgecode.chess.scanner.request.PlainRequestClient;
23 import org.hedgecode.chess.scanner.request.RequestClient;
24
25 /**
26  * Proxy settings main class.
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public final class Proxy {
31
32     private static Proxy proxy;
33
34     private RequestClient requestClient;
35
36     private Proxy() {
37         requestClient = new PlainRequestClient();
38     }
39
40     public static void setProxy(ProxyParams proxyParams) {
41         getProxy().set(proxyParams);
42     }
43
44     public static RequestClient getRequestClient() {
45         return getProxy().client();
46     }
47
48     private void set(ProxyParams proxyParams) {
49         if (proxyParams.isSystem()) {
50             SystemProxy.setProxy(proxyParams);
51             requestClient = new PlainRequestClient();
52         } else {
53             switch (proxyParams.getType()) {
54                 case HTTP:
55                     requestClient = new HttpRequestClient(proxyParams);
56                     break;
57                 case SOCKS:
58                     requestClient = new SocksRequestClient(proxyParams);
59                     break;
60                 default:
61                     requestClient = new PlainRequestClient();
62             }
63         }
64     }
65
66     private RequestClient client() {
67         return requestClient;
68     }
69
70     private static Proxy getProxy() {
71         if (proxy == null) {
72             proxy = new Proxy();
73         }
74         return proxy;
75     }
76
77 }