[LIB-13] Rename some common classes
[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.util.List;
23 import java.util.Map;
24
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;
29
30 import org.hedgecode.chess.scanner.Scanner;
31 import org.hedgecode.chess.scanner.ScannerException;
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;
36
37 import static org.hedgecode.chess.scanner.ScannerConstants.*;
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 ScannerException {
47         return request(url, true);
48     }
49
50     protected String request(String url, boolean withCrlf) throws ScannerException {
51         HttpGet request = new HttpGet(url);
52         CloseableHttpResponse response = null;
53         StringBuilder sb = new StringBuilder();
54         try {
55             response = getClient().execute(request);
56             try (BufferedReader br = new BufferedReader(
57                     new InputStreamReader(
58                             response.getEntity().getContent(), CHARSET
59                     )
60             )) {
61                 String line;
62                 while ((line = br.readLine()) != null) {
63                     sb.append(line);
64                     if (withCrlf) {
65                         sb.append(CRLF);
66                     }
67                 }
68             }
69         } catch (IOException cause) {
70             throw new ScannerException(
71                     String.format("Error occurred for requesting URL: %s", url), cause
72             );
73         } finally {
74             if (response != null) {
75                 try {
76                     response.close();
77                 } catch (IOException ignored) {}
78             }
79         }
80         return sb.toString();
81     }
82
83     protected List<String> split(String url, String splitMatch) throws ScannerException {
84         RegexResult result = splitRequest(url, splitMatch);
85         return result.resultList();
86     }
87
88     protected Map<String, String> splitMap(String url, String splitMatch) throws ScannerException {
89         RegexResult result = splitRequest(url, splitMatch);
90         return result.resultMap();
91     }
92
93     protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException {
94         RegexMatcher matcher = new RegexTypeMatcher(
95                 RegexType.SPLIT, splitMatch
96         );
97         return matchRequest(url, matcher, false);
98     }
99
100     protected String match(String url, String match) throws ScannerException {
101         List<String> result = match(url, match, true);
102         return result.isEmpty() ? null : result.get(0);
103     }
104
105     protected List<String> match(String url, String match, boolean isFirst)
106             throws ScannerException
107     {
108         RegexMatcher matcher = new RegexTypeMatcher(
109                 RegexType.FIND, match
110         );
111         RegexResult result = matchRequest(
112                 url,
113                 matcher,
114                 isFirst
115         );
116         return result.resultList();
117     }
118
119     protected Map<String, String> matchMap(String url, String match, boolean isFirst)
120             throws ScannerException
121     {
122         RegexMatcher matcher = new RegexTypeMatcher(
123                 RegexType.FIND, match
124         );
125         RegexResult result = matchRequest(
126                 url,
127                 matcher,
128                 isFirst
129         );
130         return result.resultMap();
131     }
132
133     protected String match(String url, String startMatch, String endMatch)
134             throws ScannerException
135     {
136         List<String> result = match(url, startMatch, endMatch, true);
137         return result.isEmpty() ? null : result.get(0);
138     }
139
140     protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
141             throws ScannerException
142     {
143         RegexMatcher matcher = new RegexTypeMatcher(
144                 startMatch, endMatch
145         );
146         RegexResult result = matchRequest(
147                 url,
148                 matcher,
149                 isFirst
150         );
151         return result.resultList();
152     }
153
154     protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
155             throws ScannerException
156     {
157         RegexMatcher matcher = new RegexTypeMatcher(
158                 startMatch, endMatch
159         );
160         RegexResult result = matchRequest(
161                 url,
162                 matcher,
163                 isFirst
164         );
165         return result.resultMap();
166     }
167
168     protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
169             throws ScannerException
170     {
171         HttpGet request = new HttpGet(url);
172         CloseableHttpResponse response = null;
173         try {
174             response = getClient().execute(request);
175             try (BufferedReader br = new BufferedReader(
176                     new InputStreamReader(
177                             response.getEntity().getContent(),
178                             CHARSET
179                     )
180             )) {
181                 String line;
182                 while ((line = br.readLine()) != null) {
183                     matcher.match(line);
184                     if (isFirst && !matcher.result().isEmpty()) {
185                         break;
186                     }
187                 }
188             }
189         } catch (IOException cause) {
190             throw new ScannerException(
191                     String.format("Error occurred for requesting URL: %s", url), cause
192             );
193         } finally {
194             if (response != null) {
195                 try {
196                     response.close();
197                 } catch (IOException ignored) {}
198             }
199         }
200         return matcher.result();
201     }
202
203     private CloseableHttpClient getClient() {
204         return HttpClients.createMinimal();
205     }
206
207 }