[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / proxy / client / HttpRequestClient.java
diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/client/HttpRequestClient.java b/src/main/java/org/hedgecode/chess/scanner/proxy/client/HttpRequestClient.java
new file mode 100644 (file)
index 0000000..f3b8517
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * 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.client;
+
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.hedgecode.chess.scanner.proxy.ProxyParams;
+import org.hedgecode.chess.scanner.request.RequestClient;
+
+/**
+ * HttpRequestClient
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class HttpRequestClient implements RequestClient {
+
+    private final HttpClientBuilder clientBuilder;
+
+    public HttpRequestClient(ProxyParams params) {
+        HttpHost proxy = new HttpHost(
+                params.getHost(), params.getPort()
+        );
+        clientBuilder = HttpClientBuilder.create().setProxy(proxy);
+        if (params.getUser() != null) {
+            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+            credentialsProvider.setCredentials(
+                    new AuthScope(
+                            params.getHost(), params.getPort()
+                    ),
+                    new UsernamePasswordCredentials(
+                            params.getUser(), params.getPassword()
+                    )
+            );
+            clientBuilder.setDefaultCredentialsProvider(
+                    credentialsProvider
+            );
+        }
+    }
+
+    @Override
+    public CloseableHttpClient getClient() {
+        return clientBuilder.build();
+    }
+
+    @Override
+    public HttpClientContext getContext() {
+        return null;
+    }
+
+/*
+    private void ntlm() {
+        NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );
+
+        CredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds );
+        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
+
+        clientBuilder.useSystemProperties();
+        clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort()));
+        clientBuilder.setDefaultCredentialsProvider(credsProvider);
+        clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
+
+        CloseableHttpClient client = clientBuilder.build();
+    }
+*/
+
+}