[LIB-13] Locale resource settings
[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.client.protocol.HttpClientContext;
28 import org.apache.http.impl.client.CloseableHttpClient;
29
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;
37
38 import static org.hedgecode.chess.scanner.ScannerConstants.*;
39
40 /**
41  * AbstractRequestScanner
42  *
43  * @author Dmitry Samoshin aka gotty
44  */
45 public abstract class AbstractRequestScanner implements Scanner {
46
47     protected String request(String url) throws ScannerException {
48         return request(url, true);
49     }
50
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()
55         )) {
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                     "scanner.request.error", cause, url
72             );
73         }
74         return sb.toString();
75     }
76
77     protected List<String> split(String url, String splitMatch) throws ScannerException {
78         RegexResult result = splitRequest(url, splitMatch);
79         return result.resultList();
80     }
81
82     protected Map<String, String> splitMap(String url, String splitMatch) throws ScannerException {
83         RegexResult result = splitRequest(url, splitMatch);
84         return result.resultMap();
85     }
86
87     protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException {
88         RegexMatcher matcher = new RegexTypeMatcher(
89                 RegexType.SPLIT, splitMatch
90         );
91         return matchRequest(url, matcher, false);
92     }
93
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);
97     }
98
99     protected List<String> match(String url, String match, boolean isFirst)
100             throws ScannerException
101     {
102         RegexMatcher matcher = new RegexTypeMatcher(
103                 RegexType.FIND, match
104         );
105         RegexResult result = matchRequest(
106                 url,
107                 matcher,
108                 isFirst
109         );
110         return result.resultList();
111     }
112
113     protected Map<String, String> matchMap(String url, String match, boolean isFirst)
114             throws ScannerException
115     {
116         RegexMatcher matcher = new RegexTypeMatcher(
117                 RegexType.FIND, match
118         );
119         RegexResult result = matchRequest(
120                 url,
121                 matcher,
122                 isFirst
123         );
124         return result.resultMap();
125     }
126
127     protected String match(String url, String startMatch, String endMatch)
128             throws ScannerException
129     {
130         List<String> result = match(url, startMatch, endMatch, true);
131         return result.isEmpty() ? null : result.get(0);
132     }
133
134     protected List<String> match(String url, String startMatch, String endMatch, boolean isFirst)
135             throws ScannerException
136     {
137         RegexMatcher matcher = new RegexTypeMatcher(
138                 startMatch, endMatch
139         );
140         RegexResult result = matchRequest(
141                 url,
142                 matcher,
143                 isFirst
144         );
145         return result.resultList();
146     }
147
148     protected Map<String, String> matchMap(String url, String startMatch, String endMatch, boolean isFirst)
149             throws ScannerException
150     {
151         RegexMatcher matcher = new RegexTypeMatcher(
152                 startMatch, endMatch
153         );
154         RegexResult result = matchRequest(
155                 url,
156                 matcher,
157                 isFirst
158         );
159         return result.resultMap();
160     }
161
162     protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst)
163             throws ScannerException
164     {
165         try (CloseableHttpResponse response = getClient().execute(
166                 new HttpGet(url), getContext()
167         )) {
168             try (BufferedReader br = new BufferedReader(
169                     new InputStreamReader(
170                             response.getEntity().getContent(),
171                             CHARSET
172                     )
173             )) {
174                 String line;
175                 while ((line = br.readLine()) != null) {
176                     matcher.match(line);
177                     if (isFirst && !matcher.result().isEmpty()) {
178                         break;
179                     }
180                 }
181             }
182         } catch (IOException cause) {
183             throw new ScannerException(
184                     "scanner.request.error", cause, url
185             );
186         }
187         return matcher.result();
188     }
189
190     private CloseableHttpClient getClient() {
191         return Proxy.getRequestClient().getClient();
192     }
193
194     private HttpClientContext getContext() {
195         return Proxy.getRequestClient().getContext();
196     }
197
198 }