[LIB-13] Add common scanner functional
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / portal / AbstractRequestScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner.portal;
18
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;
25 import java.util.Map;
26
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;
31
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;
38
39 /**
40  * AbstractRequestScanner
41  *
42  * @author Dmitry Samoshin aka gotty
43  */
44 public abstract class AbstractRequestScanner implements Scanner {
45
46     protected String request(String url) throws ChessHogScannerException {
47         HttpGet request = new HttpGet(url);
48         CloseableHttpResponse response = null;
49         StringBuilder sb = new StringBuilder();
50         try {
51             response = getClient().execute(request);
52             try (BufferedReader br = new BufferedReader(
53                     new InputStreamReader(
54                             response.getEntity().getContent(),
55                             ChessHogScannerConstants.CHARSET
56                     )
57             )) {
58                 String line;
59                 while ((line = br.readLine()) != null) {
60                     sb.append(
61                             line
62                     ).append(
63                             ChessHogScannerConstants.CRLF
64                     );
65                 }
66             }
67         } catch (IOException cause) {
68             throw new ChessHogScannerException(
69                     String.format("Error occurred for requesting URL: %s", url), cause
70             );
71         } finally {
72             if (response != null) {
73                 try {
74                     response.close();
75                 } catch (IOException ignored) {}
76             }
77         }
78         return sb.toString();
79     }
80
81     protected String matchRequest(String url, String match)
82             throws ChessHogScannerException
83     {
84         List<String> result = matchRequest(url, match, true);
85         return result.isEmpty() ? null : result.get(0);
86     }
87
88     protected List<String> matchRequest(String url, String match, boolean isFirst)
89             throws ChessHogScannerException
90     {
91         RegexResult result = matchRequest(
92                 url,
93                 new RegexMatcherResult(
94                         match,
95                         isFirst,
96                         false
97                 )
98         );
99         return result.resultList();
100     }
101
102     protected String matchRequest(String url, String startMatch, String endMatch)
103             throws ChessHogScannerException
104     {
105         return matchRequest(url, startMatch, endMatch, true);
106     }
107
108     protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst)
109             throws ChessHogScannerException
110     {
111         RegexResult result = matchRequest(
112                 url,
113                 new RegexMatcherResult(
114                         startMatch,
115                         endMatch,
116                         isFirst,
117                         false
118                 )
119         );
120         return result.isEmpty() ? null : result.resultList().get(0);
121     }
122
123 /*
124     protected String matchRequest(String url, String match)
125             throws ChessHogScannerException
126     {
127         List<String> result = matchRequest(url, match, true);
128         return result.isEmpty() ? null : result.get(0);
129     }
130
131     protected List<String> matchRequest(String url, String match, boolean isFirst)
132             throws ChessHogScannerException
133     {
134         HttpGet request = new HttpGet(url);
135         CloseableHttpResponse response = null;
136         List<String> result = new ArrayList<>();
137         try {
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
144                     )
145             )) {
146                 String line;
147                 while ((line = br.readLine()) != null) {
148                     Matcher matcher = pattern.matcher(line);
149                     if (matcher.find()) {
150                         result.add(
151                                 matcher.group(1)
152                         );
153                         if (isFirst) {
154                             break;
155                         }
156                     }
157                 }
158             }
159         } catch (IOException cause) {
160             throw new ChessHogScannerException(
161                     String.format("Error occurred for requesting URL: %s", url), cause
162             );
163         } finally {
164             if (response != null) {
165                 try {
166                     response.close();
167                 } catch (IOException ignored) {}
168             }
169         }
170         return result;
171     }
172
173     protected String matchRequest(String url, String startMatch, String endMatch)
174             throws ChessHogScannerException
175     {
176         return matchRequest(url, startMatch, endMatch, true);
177     }
178
179     protected String matchRequest(String url, String startMatch, String endMatch, boolean isFirst)
180             throws ChessHogScannerException
181     {
182         HttpGet request = new HttpGet(url);
183         CloseableHttpResponse response = null;
184         StringBuilder sb = new StringBuilder();
185         try {
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
193                     )
194             )) {
195                 String line;
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()) {
202                         sb.append(line);
203                         if (isMatch && isFirst) {
204                             break;
205                         }
206                         isMatch = !isMatch;
207                     } else {
208                         if (isMatch) {
209                             sb.append(line);
210                         }
211                     }
212                 }
213             }
214         } catch (IOException cause) {
215             throw new ChessHogScannerException(
216                     String.format("Error occurred for requesting URL: %s", url), cause
217             );
218         } finally {
219             if (response != null) {
220                 try {
221                     response.close();
222                 } catch (IOException ignored) {}
223             }
224         }
225         return sb.length() > 0 ? sb.toString() : null;
226     }
227 */
228
229     protected RegexResult matchRequest(String url, RegexMatcher matcher) throws ChessHogScannerException {
230         HttpGet request = new HttpGet(url);
231         CloseableHttpResponse response = null;
232         try {
233             response = getClient().execute(request);
234             try (BufferedReader br = new BufferedReader(
235                     new InputStreamReader(
236                             response.getEntity().getContent(),
237                             ChessHogScannerConstants.CHARSET
238                     )
239             )) {
240                 String line;
241                 while ((line = br.readLine()) != null) {
242                     matcher.match(line);
243                     if (matcher.isBreak()) {
244                         break;
245                     }
246                 }
247             }
248         } catch (IOException cause) {
249             throw new ChessHogScannerException(
250                     String.format("Error occurred for requesting URL: %s", url), cause
251             );
252         } finally {
253             if (response != null) {
254                 try {
255                     response.close();
256                 } catch (IOException ignored) {}
257             }
258         }
259         return matcher.result();
260     }
261
262 /*
263     protected String urlEncode(String query) throws ChessHogScannerException {
264         String encodeQuery;
265         try {
266             encodeQuery = URLEncoder.encode(
267                     query, ChessHogScannerConstants.CHARSET.name()
268             );
269         } catch (UnsupportedEncodingException cause) {
270             throw new ChessHogScannerException(
271                     String.format("Unsupported encoding: %s", ChessHogScannerConstants.CHARSET.name()),
272                     cause
273             );
274         }
275         return encodeQuery;
276     }
277 */
278
279     private CloseableHttpClient getClient() {
280         return HttpClients.createMinimal();
281     }
282
283 }