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