[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / client / HttpRequestClient.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.client;
18
19 import org.apache.http.HttpHost;
20 import org.apache.http.auth.AuthScope;
21 import org.apache.http.auth.UsernamePasswordCredentials;
22 import org.apache.http.client.CredentialsProvider;
23 import org.apache.http.client.protocol.HttpClientContext;
24 import org.apache.http.impl.client.BasicCredentialsProvider;
25 import org.apache.http.impl.client.CloseableHttpClient;
26
27 import org.apache.http.impl.client.HttpClientBuilder;
28 import org.hedgecode.chess.scanner.proxy.ProxyParams;
29 import org.hedgecode.chess.scanner.request.RequestClient;
30
31 /**
32  * HttpRequestClient
33  *
34  * @author Dmitry Samoshin aka gotty
35  */
36 public class HttpRequestClient implements RequestClient {
37
38     private final HttpClientBuilder clientBuilder;
39
40     public HttpRequestClient(ProxyParams params) {
41         HttpHost proxy = new HttpHost(
42                 params.getHost(), params.getPort()
43         );
44         clientBuilder = HttpClientBuilder.create().setProxy(proxy);
45         if (params.getUser() != null) {
46             CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
47             credentialsProvider.setCredentials(
48                     new AuthScope(
49                             params.getHost(), params.getPort()
50                     ),
51                     new UsernamePasswordCredentials(
52                             params.getUser(), params.getPassword()
53                     )
54             );
55             clientBuilder.setDefaultCredentialsProvider(
56                     credentialsProvider
57             );
58         }
59     }
60
61     @Override
62     public CloseableHttpClient getClient() {
63         return clientBuilder.build();
64     }
65
66     @Override
67     public HttpClientContext getContext() {
68         return null;
69     }
70
71 /*
72     private void ntlm() {
73         NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );
74
75         CredentialsProvider credsProvider = new BasicCredentialsProvider();
76         credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds );
77         HttpClientBuilder clientBuilder = HttpClientBuilder.create();
78
79         clientBuilder.useSystemProperties();
80         clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort()));
81         clientBuilder.setDefaultCredentialsProvider(credsProvider);
82         clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
83
84         CloseableHttpClient client = clientBuilder.build();
85     }
86 */
87
88 }