[LIB-13] Locale resource settings
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / AbstractRequestScanner.java
index a82422e..1ed66d5 100644 (file)
@@ -24,17 +24,19 @@ 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.ChessHogScannerConstants;
-import org.hedgecode.chess.scanner.ChessHogScannerException;
 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;
 import org.hedgecode.chess.scanner.regex.RegexTypeMatcher;
 
+import static org.hedgecode.chess.scanner.ScannerConstants.*;
+
 /**
  * AbstractRequestScanner
  *
@@ -42,67 +44,60 @@ import org.hedgecode.chess.scanner.regex.RegexTypeMatcher;
  */
 public abstract class AbstractRequestScanner implements Scanner {
 
-    protected String request(String url) throws ChessHogScannerException {
-        HttpGet request = new HttpGet(url);
-        CloseableHttpResponse response = null;
+    protected String request(String url) throws ScannerException {
+        return request(url, true);
+    }
+
+    protected String request(String url, boolean withCrlf) throws ScannerException {
         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(),
-                            ChessHogScannerConstants.CHARSET
+                            response.getEntity().getContent(), CHARSET
                     )
             )) {
                 String line;
                 while ((line = br.readLine()) != null) {
-                    sb.append(
-                            line
-                    ).append(
-                            ChessHogScannerConstants.CRLF
-                    );
+                    sb.append(line);
+                    if (withCrlf) {
+                        sb.append(CRLF);
+                    }
                 }
             }
         } catch (IOException cause) {
-            throw new ChessHogScannerException(
-                    String.format("Error occurred for requesting URL: %s", url), cause
+            throw new ScannerException(
+                    "scanner.request.error", cause, url
             );
-        } finally {
-            if (response != null) {
-                try {
-                    response.close();
-                } catch (IOException ignored) {}
-            }
         }
         return sb.toString();
     }
 
-    protected List<String> split(String url, String splitMatch) throws ChessHogScannerException {
+    protected List<String> split(String url, String splitMatch) throws ScannerException {
         RegexResult result = splitRequest(url, splitMatch);
         return result.resultList();
     }
 
-    protected Map<String, String> splitMap(String url, String splitMatch) throws ChessHogScannerException {
+    protected Map<String, String> splitMap(String url, String splitMatch) throws ScannerException {
         RegexResult result = splitRequest(url, splitMatch);
         return result.resultMap();
     }
 
-    protected RegexResult splitRequest(String url, String splitMatch) throws ChessHogScannerException {
+    protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException {
         RegexMatcher matcher = new RegexTypeMatcher(
                 RegexType.SPLIT, splitMatch
         );
         return matchRequest(url, matcher, false);
     }
 
-    protected String match(String url, String match)
-            throws ChessHogScannerException
-    {
+    protected String match(String url, String match) throws ScannerException {
         List<String> result = match(url, match, true);
         return result.isEmpty() ? null : result.get(0);
     }
 
     protected List<String> match(String url, String match, boolean isFirst)
-            throws ChessHogScannerException
+            throws ScannerException
     {
         RegexMatcher matcher = new RegexTypeMatcher(
                 RegexType.FIND, match
@@ -116,7 +111,7 @@ public abstract class AbstractRequestScanner implements Scanner {
     }
 
     protected Map<String, String> matchMap(String url, String match, boolean isFirst)
-            throws ChessHogScannerException
+            throws ScannerException
     {
         RegexMatcher matcher = new RegexTypeMatcher(
                 RegexType.FIND, match
@@ -130,14 +125,14 @@ public abstract class AbstractRequestScanner implements Scanner {
     }
 
     protected String match(String url, String startMatch, String endMatch)
-            throws ChessHogScannerException
+            throws ScannerException
     {
         List<String> result = match(url, startMatch, endMatch, true);
         return result.isEmpty() ? null : result.get(0);
     }
 
     protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
-            throws ChessHogScannerException
+            throws ScannerException
     {
         RegexMatcher matcher = new RegexTypeMatcher(
                 startMatch, endMatch
@@ -151,7 +146,7 @@ public abstract class AbstractRequestScanner implements Scanner {
     }
 
     protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
-            throws ChessHogScannerException
+            throws ScannerException
     {
         RegexMatcher matcher = new RegexTypeMatcher(
                 startMatch, endMatch
@@ -165,16 +160,15 @@ public abstract class AbstractRequestScanner implements Scanner {
     }
 
     protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
-            throws ChessHogScannerException
+            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(),
-                            ChessHogScannerConstants.CHARSET
+                            CHARSET
                     )
             )) {
                 String line;
@@ -186,21 +180,19 @@ public abstract class AbstractRequestScanner implements Scanner {
                 }
             }
         } catch (IOException cause) {
-            throw new ChessHogScannerException(
-                    String.format("Error occurred for requesting URL: %s", url), cause
+            throw new ScannerException(
+                    "scanner.request.error", cause, url
             );
-        } 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();
     }
 
 }