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.client.protocol.HttpClientContext;
28 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.hedgecode.chess.scanner.Scanner;
31 import org.hedgecode.chess.scanner.ScannerException;
32 import org.hedgecode.chess.scanner.proxy.Proxy;
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;
38 import static org.hedgecode.chess.scanner.ScannerConstants.*;
41 * AbstractRequestScanner
43 * @author Dmitry Samoshin aka gotty
45 public abstract class AbstractRequestScanner implements Scanner {
47 protected String request(String url) throws ScannerException {
48 return request(url, true);
51 protected String request(String url, boolean withCrlf) throws ScannerException {
52 StringBuilder sb = new StringBuilder();
53 try (CloseableHttpResponse response = getClient().execute(
54 new HttpGet(url), getContext()
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 ScannerException(
71 String.format("Error occurred for requesting URL: %s", url), cause
77 protected List<String> split(String url, String splitMatch) throws ScannerException {
78 RegexResult result = splitRequest(url, splitMatch);
79 return result.resultList();
82 protected Map<String, String> splitMap(String url, String splitMatch) throws ScannerException {
83 RegexResult result = splitRequest(url, splitMatch);
84 return result.resultMap();
87 protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException {
88 RegexMatcher matcher = new RegexTypeMatcher(
89 RegexType.SPLIT, splitMatch
91 return matchRequest(url, matcher, false);
94 protected String match(String url, String match) throws ScannerException {
95 List<String> result = match(url, match, true);
96 return result.isEmpty() ? null : result.get(0);
99 protected List<String> match(String url, String match, boolean isFirst)
100 throws ScannerException
102 RegexMatcher matcher = new RegexTypeMatcher(
103 RegexType.FIND, match
105 RegexResult result = matchRequest(
110 return result.resultList();
113 protected Map<String, String> matchMap(String url, String match, boolean isFirst)
114 throws ScannerException
116 RegexMatcher matcher = new RegexTypeMatcher(
117 RegexType.FIND, match
119 RegexResult result = matchRequest(
124 return result.resultMap();
127 protected String match(String url, String startMatch, String endMatch)
128 throws ScannerException
130 List<String> result = match(url, startMatch, endMatch, true);
131 return result.isEmpty() ? null : result.get(0);
134 protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
135 throws ScannerException
137 RegexMatcher matcher = new RegexTypeMatcher(
140 RegexResult result = matchRequest(
145 return result.resultList();
148 protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
149 throws ScannerException
151 RegexMatcher matcher = new RegexTypeMatcher(
154 RegexResult result = matchRequest(
159 return result.resultMap();
162 protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
163 throws ScannerException
165 try (CloseableHttpResponse response = getClient().execute(
166 new HttpGet(url), getContext()
168 try (BufferedReader br = new BufferedReader(
169 new InputStreamReader(
170 response.getEntity().getContent(),
175 while ((line = br.readLine()) != null) {
177 if (isFirst && !matcher.result().isEmpty()) {
182 } catch (IOException cause) {
183 throw new ScannerException(
184 String.format("Error occurred for requesting URL: %s", url), cause
187 return matcher.result();
190 private CloseableHttpClient getClient() {
191 return Proxy.getRequestClient().getClient();
194 private HttpClientContext getContext() {
195 return Proxy.getRequestClient().getContext();