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.io.UnsupportedEncodingException;
23 import java.net.URLEncoder;
24 import java.util.List;
27 import org.apache.http.client.methods.CloseableHttpResponse;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.impl.client.HttpClients;
32 import org.hedgecode.chess.scanner.ChessHogScannerConstants;
33 import org.hedgecode.chess.scanner.ChessHogScannerException;
34 import org.hedgecode.chess.scanner.Scanner;
35 import org.hedgecode.chess.scanner.regex.RegexMatcher;
36 import org.hedgecode.chess.scanner.regex.RegexMatcherResult;
37 import org.hedgecode.chess.scanner.regex.RegexResult;
40 * AbstractRequestScanner
42 * @author Dmitry Samoshin aka gotty
44 public abstract class AbstractRequestScanner implements Scanner {
46 protected String request(String url) throws ChessHogScannerException {
47 HttpGet request = new HttpGet(url);
48 CloseableHttpResponse response = null;
49 StringBuilder sb = new StringBuilder();
51 response = getClient().execute(request);
52 try (BufferedReader br = new BufferedReader(
53 new InputStreamReader(
54 response.getEntity().getContent(),
55 ChessHogScannerConstants.CHARSET
59 while ((line = br.readLine()) != null) {
63 ChessHogScannerConstants.CRLF
67 } catch (IOException cause) {
68 throw new ChessHogScannerException(
69 String.format("Error occurred for requesting URL: %s", url), cause
72 if (response != null) {
75 } catch (IOException ignored) {}
81 protected String matchRequest(String url, String match)
82 throws ChessHogScannerException
84 List<String> result = matchRequest(url, match, true);
85 return result.isEmpty() ? null : result.get(0);
88 protected List<String> matchRequest(String url, String match, boolean isFirst)
89 throws ChessHogScannerException
91 RegexResult result = matchRequest(
93 new RegexMatcherResult(
99 return result.resultList();
102 protected String matchRequest(String url, String startMatch, String endMatch)
103 throws ChessHogScannerException
105 return matchRequest(url, startMatch, endMatch, true);
108 protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst)
109 throws ChessHogScannerException
111 RegexResult result = matchRequest(
113 new RegexMatcherResult(
120 return result.isEmpty() ? null : result.resultList().get(0);
124 protected String matchRequest(String url, String match)
125 throws ChessHogScannerException
127 List<String> result = matchRequest(url, match, true);
128 return result.isEmpty() ? null : result.get(0);
131 protected List<String> matchRequest(String url, String match, boolean isFirst)
132 throws ChessHogScannerException
134 HttpGet request = new HttpGet(url);
135 CloseableHttpResponse response = null;
136 List<String> result = new ArrayList<>();
138 response = getClient().execute(request);
139 Pattern pattern = Pattern.compile(match);
140 try (BufferedReader br = new BufferedReader(
141 new InputStreamReader(
142 response.getEntity().getContent(),
143 ChessHogScannerConstants.CHARSET
147 while ((line = br.readLine()) != null) {
148 Matcher matcher = pattern.matcher(line);
149 if (matcher.find()) {
159 } catch (IOException cause) {
160 throw new ChessHogScannerException(
161 String.format("Error occurred for requesting URL: %s", url), cause
164 if (response != null) {
167 } catch (IOException ignored) {}
173 protected String matchRequest(String url, String startMatch, String endMatch)
174 throws ChessHogScannerException
176 return matchRequest(url, startMatch, endMatch, true);
179 protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst)
180 throws ChessHogScannerException
182 HttpGet request = new HttpGet(url);
183 CloseableHttpResponse response = null;
184 StringBuilder sb = new StringBuilder();
186 response = getClient().execute(request);
187 Pattern startPattern = Pattern.compile(startMatch);
188 Pattern endPattern = Pattern.compile(endMatch);
189 try (BufferedReader br = new BufferedReader(
190 new InputStreamReader(
191 response.getEntity().getContent(),
192 ChessHogScannerConstants.CHARSET
196 boolean isMatch = false;
197 while ((line = br.readLine()) != null) {
198 Matcher matcher = isMatch
199 ? endPattern.matcher(line)
200 : startPattern.matcher(line);
201 if (matcher.find()) {
203 if (isMatch && isFirst) {
214 } catch (IOException cause) {
215 throw new ChessHogScannerException(
216 String.format("Error occurred for requesting URL: %s", url), cause
219 if (response != null) {
222 } catch (IOException ignored) {}
225 return sb.length() > 0 ? sb.toString() : null;
229 protected RegexResult matchRequest(String url, RegexMatcher matcher) throws ChessHogScannerException {
230 HttpGet request = new HttpGet(url);
231 CloseableHttpResponse response = null;
233 response = getClient().execute(request);
234 try (BufferedReader br = new BufferedReader(
235 new InputStreamReader(
236 response.getEntity().getContent(),
237 ChessHogScannerConstants.CHARSET
241 while ((line = br.readLine()) != null) {
243 if (matcher.isBreak()) {
248 } catch (IOException cause) {
249 throw new ChessHogScannerException(
250 String.format("Error occurred for requesting URL: %s", url), cause
253 if (response != null) {
256 } catch (IOException ignored) {}
259 return matcher.result();
263 protected String urlEncode(String query) throws ChessHogScannerException {
266 encodeQuery = URLEncoder.encode(
267 query, ChessHogScannerConstants.CHARSET.name()
269 } catch (UnsupportedEncodingException cause) {
270 throw new ChessHogScannerException(
271 String.format("Unsupported encoding: %s", ChessHogScannerConstants.CHARSET.name()),
279 private CloseableHttpClient getClient() {
280 return HttpClients.createMinimal();