[LIB-13] Several options for working through a proxy server
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / AbstractRequestScanner.java
index e179d2c..d6aa38b 100644 (file)
@@ -24,11 +24,12 @@ import java.util.Map;
 
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
 
 import org.hedgecode.chess.scanner.Scanner;
 import org.hedgecode.chess.scanner.ScannerException;
+import org.hedgecode.chess.scanner.proxy.Proxy;
 import org.hedgecode.chess.scanner.regex.RegexMatcher;
 import org.hedgecode.chess.scanner.regex.RegexResult;
 import org.hedgecode.chess.scanner.regex.RegexType;
@@ -48,11 +49,10 @@ public abstract class AbstractRequestScanner implements Scanner {
     }
 
     protected String request(String url, boolean withCrlf) throws ScannerException {
-        HttpGet request = new HttpGet(url);
-        CloseableHttpResponse response = null;
         StringBuilder sb = new StringBuilder();
-        try {
-            response = getClient().execute(request);
+        try (CloseableHttpResponse response = getClient().execute(
+                new HttpGet(url), getContext()
+        )) {
             try (BufferedReader br = new BufferedReader(
                     new InputStreamReader(
                             response.getEntity().getContent(), CHARSET
@@ -70,12 +70,6 @@ public abstract class AbstractRequestScanner implements Scanner {
             throw new ScannerException(
                     String.format("Error occurred for requesting URL: %s", url), cause
             );
-        } finally {
-            if (response != null) {
-                try {
-                    response.close();
-                } catch (IOException ignored) {}
-            }
         }
         return sb.toString();
     }
@@ -168,10 +162,9 @@ public abstract class AbstractRequestScanner implements Scanner {
     protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
             throws ScannerException
     {
-        HttpGet request = new HttpGet(url);
-        CloseableHttpResponse response = null;
-        try {
-            response = getClient().execute(request);
+        try (CloseableHttpResponse response = getClient().execute(
+                new HttpGet(url), getContext()
+        )) {
             try (BufferedReader br = new BufferedReader(
                     new InputStreamReader(
                             response.getEntity().getContent(),
@@ -190,18 +183,16 @@ public abstract class AbstractRequestScanner implements Scanner {
             throw new ScannerException(
                     String.format("Error occurred for requesting URL: %s", url), cause
             );
-        } finally {
-            if (response != null) {
-                try {
-                    response.close();
-                } catch (IOException ignored) {}
-            }
         }
         return matcher.result();
     }
 
     private CloseableHttpClient getClient() {
-        return HttpClients.createMinimal();
+        return Proxy.getRequestClient().getClient();
+    }
+
+    private HttpClientContext getContext() {
+        return Proxy.getRequestClient().getContext();
     }
 
 }