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.ChessHogScannerException;
31 import org.hedgecode.chess.scanner.Scanner;
32 import org.hedgecode.chess.scanner.regex.RegexMatcher;
33 import org.hedgecode.chess.scanner.regex.RegexResult;
34 import org.hedgecode.chess.scanner.regex.RegexType;
35 import org.hedgecode.chess.scanner.regex.RegexTypeMatcher;
37 import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
40 * AbstractRequestScanner
42 * @author Dmitry Samoshin aka gotty
44 public abstract class AbstractRequestScanner implements Scanner {
46 protected String request(String url) throws ChessHogScannerException {
47 return request(url, true);
50 protected String request(String url, boolean withCrlf) throws ChessHogScannerException {
51 HttpGet request = new HttpGet(url);
52 CloseableHttpResponse response = null;
53 StringBuilder sb = new StringBuilder();
55 response = getClient().execute(request);
56 try (BufferedReader br = new BufferedReader(
57 new InputStreamReader(
58 response.getEntity().getContent(), CHARSET
62 while ((line = br.readLine()) != null) {
69 } catch (IOException cause) {
70 throw new ChessHogScannerException(
71 String.format("Error occurred for requesting URL: %s", url), cause
74 if (response != null) {
77 } catch (IOException ignored) {}
83 protected List<String> split(String url, String splitMatch) throws ChessHogScannerException {
84 RegexResult result = splitRequest(url, splitMatch);
85 return result.resultList();
88 protected Map<String, String> splitMap(String url, String splitMatch) throws ChessHogScannerException {
89 RegexResult result = splitRequest(url, splitMatch);
90 return result.resultMap();
93 protected RegexResult splitRequest(String url, String splitMatch) throws ChessHogScannerException {
94 RegexMatcher matcher = new RegexTypeMatcher(
95 RegexType.SPLIT, splitMatch
97 return matchRequest(url, matcher, false);
100 protected String match(String url, String match) throws ChessHogScannerException {
101 List<String> result = match(url, match, true);
102 return result.isEmpty() ? null : result.get(0);
105 protected List<String> match(String url, String match, boolean isFirst)
106 throws ChessHogScannerException
108 RegexMatcher matcher = new RegexTypeMatcher(
109 RegexType.FIND, match
111 RegexResult result = matchRequest(
116 return result.resultList();
119 protected Map<String, String> matchMap(String url, String match, boolean isFirst)
120 throws ChessHogScannerException
122 RegexMatcher matcher = new RegexTypeMatcher(
123 RegexType.FIND, match
125 RegexResult result = matchRequest(
130 return result.resultMap();
133 protected String match(String url, String startMatch, String endMatch)
134 throws ChessHogScannerException
136 List<String> result = match(url, startMatch, endMatch, true);
137 return result.isEmpty() ? null : result.get(0);
140 protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
141 throws ChessHogScannerException
143 RegexMatcher matcher = new RegexTypeMatcher(
146 RegexResult result = matchRequest(
151 return result.resultList();
154 protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
155 throws ChessHogScannerException
157 RegexMatcher matcher = new RegexTypeMatcher(
160 RegexResult result = matchRequest(
165 return result.resultMap();
168 protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
169 throws ChessHogScannerException
171 HttpGet request = new HttpGet(url);
172 CloseableHttpResponse response = null;
174 response = getClient().execute(request);
175 try (BufferedReader br = new BufferedReader(
176 new InputStreamReader(
177 response.getEntity().getContent(),
182 while ((line = br.readLine()) != null) {
184 if (isFirst && !matcher.result().isEmpty()) {
189 } catch (IOException cause) {
190 throw new ChessHogScannerException(
191 String.format("Error occurred for requesting URL: %s", url), cause
194 if (response != null) {
197 } catch (IOException ignored) {}
200 return matcher.result();
203 private CloseableHttpClient getClient() {
204 return HttpClients.createMinimal();