2 * Copyright (c) 2019-2020. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.scanner.portal;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.List;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.apache.http.impl.client.HttpClients;
30 import org.hedgecode.chess.scanner.ChessHogScannerConstants;
31 import org.hedgecode.chess.scanner.ChessHogScannerException;
32 import org.hedgecode.chess.scanner.Scanner;
33 import org.hedgecode.chess.scanner.regex.RegexMatcher;
34 import org.hedgecode.chess.scanner.regex.RegexResult;
35 import org.hedgecode.chess.scanner.regex.RegexType;
36 import org.hedgecode.chess.scanner.regex.RegexTypeMatcher;
39 * AbstractRequestScanner
41 * @author Dmitry Samoshin aka gotty
43 public abstract class AbstractRequestScanner implements Scanner {
45 protected String request(String url) throws ChessHogScannerException {
46 HttpGet request = new HttpGet(url);
47 CloseableHttpResponse response = null;
48 StringBuilder sb = new StringBuilder();
50 response = getClient().execute(request);
51 try (BufferedReader br = new BufferedReader(
52 new InputStreamReader(
53 response.getEntity().getContent(),
54 ChessHogScannerConstants.CHARSET
58 while ((line = br.readLine()) != null) {
62 ChessHogScannerConstants.CRLF
66 } catch (IOException cause) {
67 throw new ChessHogScannerException(
68 String.format("Error occurred for requesting URL: %s", url), cause
71 if (response != null) {
74 } catch (IOException ignored) {}
80 protected List<String> split(String url, String splitMatch) throws ChessHogScannerException {
81 RegexResult result = splitRequest(url, splitMatch);
82 return result.resultList();
85 protected Map<String, String> splitMap(String url, String splitMatch) throws ChessHogScannerException {
86 RegexResult result = splitRequest(url, splitMatch);
87 return result.resultMap();
90 protected RegexResult splitRequest(String url, String splitMatch) throws ChessHogScannerException {
91 RegexMatcher matcher = new RegexTypeMatcher(
92 RegexType.SPLIT, splitMatch
94 return matchRequest(url, matcher, false);
97 protected String match(String url, String match)
98 throws ChessHogScannerException
100 List<String> result = match(url, match, true);
101 return result.isEmpty() ? null : result.get(0);
104 protected List<String> match(String url, String match, boolean isFirst)
105 throws ChessHogScannerException
107 RegexMatcher matcher = new RegexTypeMatcher(
108 RegexType.FIND, match
110 RegexResult result = matchRequest(
115 return result.resultList();
118 protected Map<String, String> matchMap(String url, String match, boolean isFirst)
119 throws ChessHogScannerException
121 RegexMatcher matcher = new RegexTypeMatcher(
122 RegexType.FIND, match
124 RegexResult result = matchRequest(
129 return result.resultMap();
132 protected String match(String url, String startMatch, String endMatch)
133 throws ChessHogScannerException
135 List<String> result = match(url, startMatch, endMatch, true);
136 return result.isEmpty() ? null : result.get(0);
139 protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
140 throws ChessHogScannerException
142 RegexMatcher matcher = new RegexTypeMatcher(
145 RegexResult result = matchRequest(
150 return result.resultList();
153 protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
154 throws ChessHogScannerException
156 RegexMatcher matcher = new RegexTypeMatcher(
159 RegexResult result = matchRequest(
164 return result.resultMap();
167 protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
168 throws ChessHogScannerException
170 HttpGet request = new HttpGet(url);
171 CloseableHttpResponse response = null;
173 response = getClient().execute(request);
174 try (BufferedReader br = new BufferedReader(
175 new InputStreamReader(
176 response.getEntity().getContent(),
177 ChessHogScannerConstants.CHARSET
181 while ((line = br.readLine()) != null) {
183 if (isFirst && !matcher.result().isEmpty()) {
188 } catch (IOException cause) {
189 throw new ChessHogScannerException(
190 String.format("Error occurred for requesting URL: %s", url), cause
193 if (response != null) {
196 } catch (IOException ignored) {}
199 return matcher.result();
202 private CloseableHttpClient getClient() {
203 return HttpClients.createMinimal();