X-Git-Url: https://git.hedgecode.org/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fportal%2FAbstractRequestScanner.java;h=1ed66d5643e1fe4f0662d1aa3b6fd8ea290ee7e2;hb=refs%2Fheads%2Fpgn;hp=fd72e6ead50d38ea3a99568d3bfcacd2e50e0536;hpb=c26e9e6ccd734c44467cb12f3d9087c1df73ceac;p=chesshog-scanner.git diff --git a/src/main/java/org/hedgecode/chess/scanner/portal/AbstractRequestScanner.java b/src/main/java/org/hedgecode/chess/scanner/portal/AbstractRequestScanner.java index fd72e6e..1ed66d5 100644 --- a/src/main/java/org/hedgecode/chess/scanner/portal/AbstractRequestScanner.java +++ b/src/main/java/org/hedgecode/chess/scanner/portal/AbstractRequestScanner.java @@ -19,22 +19,23 @@ package org.hedgecode.chess.scanner.portal; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.List; 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.RegexMatcherResult; 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 @@ -43,241 +44,155 @@ import org.hedgecode.chess.scanner.regex.RegexResult; */ 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 String matchRequest(String url, String match) - throws ChessHogScannerException - { - List result = matchRequest(url, match, true); + protected List split(String url, String splitMatch) throws ScannerException { + RegexResult result = splitRequest(url, splitMatch); + return result.resultList(); + } + + protected Map splitMap(String url, String splitMatch) throws ScannerException { + RegexResult result = splitRequest(url, splitMatch); + return result.resultMap(); + } + + 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 ScannerException { + List result = match(url, match, true); return result.isEmpty() ? null : result.get(0); } - protected List matchRequest(String url, String match, boolean isFirst) - throws ChessHogScannerException + protected List match(String url, String match, boolean isFirst) + throws ScannerException { + RegexMatcher matcher = new RegexTypeMatcher( + RegexType.FIND, match + ); RegexResult result = matchRequest( url, - new RegexMatcherResult( - match, - isFirst, - false - ) + matcher, + isFirst ); return result.resultList(); } - protected String matchRequest(String url, String startMatch, String endMatch) - throws ChessHogScannerException - { - return matchRequest(url, startMatch, endMatch, true); - } - - protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst) - throws ChessHogScannerException + protected Map matchMap(String url, String match, boolean isFirst) + throws ScannerException { + RegexMatcher matcher = new RegexTypeMatcher( + RegexType.FIND, match + ); RegexResult result = matchRequest( url, - new RegexMatcherResult( - startMatch, - endMatch, - isFirst, - false - ) + matcher, + isFirst ); - return result.isEmpty() ? null : result.resultList().get(0); + return result.resultMap(); } -/* - protected String matchRequest(String url, String match) - throws ChessHogScannerException + protected String match(String url, String startMatch, String endMatch) + throws ScannerException { - List result = matchRequest(url, match, true); + List result = match(url, startMatch, endMatch, true); return result.isEmpty() ? null : result.get(0); } - protected List matchRequest(String url, String match, boolean isFirst) - throws ChessHogScannerException + protected List match(String url, String startMatch, String endMatch, boolean isFirst) + throws ScannerException { - HttpGet request = new HttpGet(url); - CloseableHttpResponse response = null; - List result = new ArrayList<>(); - try { - response = getClient().execute(request); - Pattern pattern = Pattern.compile(match); - try (BufferedReader br = new BufferedReader( - new InputStreamReader( - response.getEntity().getContent(), - ChessHogScannerConstants.CHARSET - ) - )) { - String line; - while ((line = br.readLine()) != null) { - Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - result.add( - matcher.group(1) - ); - if (isFirst) { - break; - } - } - } - } - } catch (IOException cause) { - throw new ChessHogScannerException( - String.format("Error occurred for requesting URL: %s", url), cause - ); - } finally { - if (response != null) { - try { - response.close(); - } catch (IOException ignored) {} - } - } - return result; + RegexMatcher matcher = new RegexTypeMatcher( + startMatch, endMatch + ); + RegexResult result = matchRequest( + url, + matcher, + isFirst + ); + return result.resultList(); } - protected String matchRequest(String url, String startMatch, String endMatch) - throws ChessHogScannerException + protected Map matchMap(String url, String startMatch, String endMatch, boolean isFirst) + throws ScannerException { - return matchRequest(url, startMatch, endMatch, true); + RegexMatcher matcher = new RegexTypeMatcher( + startMatch, endMatch + ); + RegexResult result = matchRequest( + url, + matcher, + isFirst + ); + return result.resultMap(); } - protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst) - throws ChessHogScannerException + protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst) + throws ScannerException { - HttpGet request = new HttpGet(url); - CloseableHttpResponse response = null; - StringBuilder sb = new StringBuilder(); - try { - response = getClient().execute(request); - Pattern startPattern = Pattern.compile(startMatch); - Pattern endPattern = Pattern.compile(endMatch); - try (BufferedReader br = new BufferedReader( - new InputStreamReader( - response.getEntity().getContent(), - ChessHogScannerConstants.CHARSET - ) - )) { - String line; - boolean isMatch = false; - while ((line = br.readLine()) != null) { - Matcher matcher = isMatch - ? endPattern.matcher(line) - : startPattern.matcher(line); - if (matcher.find()) { - sb.append(line); - if (isMatch && isFirst) { - break; - } - isMatch = !isMatch; - } else { - if (isMatch) { - sb.append(line); - } - } - } - } - } catch (IOException cause) { - throw new ChessHogScannerException( - String.format("Error occurred for requesting URL: %s", url), cause - ); - } finally { - if (response != null) { - try { - response.close(); - } catch (IOException ignored) {} - } - } - return sb.length() > 0 ? sb.toString() : null; - } -*/ - - protected RegexResult matchRequest(String url, RegexMatcher matcher) throws ChessHogScannerException { - 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; while ((line = br.readLine()) != null) { matcher.match(line); - if (matcher.isBreak()) { + if (isFirst && !matcher.result().isEmpty()) { break; } } } } 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(); } -/* - protected String urlEncode(String query) throws ChessHogScannerException { - String encodeQuery; - try { - encodeQuery = URLEncoder.encode( - query, ChessHogScannerConstants.CHARSET.name() - ); - } catch (UnsupportedEncodingException cause) { - throw new ChessHogScannerException( - String.format("Unsupported encoding: %s", ChessHogScannerConstants.CHARSET.name()), - cause - ); - } - return encodeQuery; + private CloseableHttpClient getClient() { + return Proxy.getRequestClient().getClient(); } -*/ - private CloseableHttpClient getClient() { - return HttpClients.createMinimal(); + private HttpClientContext getContext() { + return Proxy.getRequestClient().getContext(); } }