/* * Copyright (c) 2019-2020. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.scanner.portal; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Map; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import org.hedgecode.chess.scanner.Scanner; import org.hedgecode.chess.scanner.ScannerException; import org.hedgecode.chess.scanner.proxy.Proxy; import org.hedgecode.chess.scanner.regex.RegexMatcher; import org.hedgecode.chess.scanner.regex.RegexResult; import org.hedgecode.chess.scanner.regex.RegexType; import org.hedgecode.chess.scanner.regex.RegexTypeMatcher; import static org.hedgecode.chess.scanner.ScannerConstants.*; /** * AbstractRequestScanner * * @author Dmitry Samoshin aka gotty */ public abstract class AbstractRequestScanner implements Scanner { protected String request(String url) throws ScannerException { return request(url, true); } protected String request(String url, boolean withCrlf) throws ScannerException { StringBuilder sb = new StringBuilder(); try (CloseableHttpResponse response = getClient().execute( new HttpGet(url), getContext() )) { try (BufferedReader br = new BufferedReader( new InputStreamReader( response.getEntity().getContent(), CHARSET ) )) { String line; while ((line = br.readLine()) != null) { sb.append(line); if (withCrlf) { sb.append(CRLF); } } } } catch (IOException cause) { throw new ScannerException( String.format("Error occurred for requesting URL: %s", url), cause ); } return sb.toString(); } protected List split(String url, String splitMatch) throws ScannerException { RegexResult result = splitRequest(url, splitMatch); return result.resultList(); } protected Map splitMap(String url, String splitMatch) throws ScannerException { RegexResult result = splitRequest(url, splitMatch); return result.resultMap(); } protected RegexResult splitRequest(String url, String splitMatch) throws ScannerException { RegexMatcher matcher = new RegexTypeMatcher( RegexType.SPLIT, splitMatch ); return matchRequest(url, matcher, false); } protected String match(String url, String match) throws ScannerException { List result = match(url, match, true); return result.isEmpty() ? null : result.get(0); } protected List match(String url, String match, boolean isFirst) throws ScannerException { RegexMatcher matcher = new RegexTypeMatcher( RegexType.FIND, match ); RegexResult result = matchRequest( url, matcher, isFirst ); return result.resultList(); } protected Map matchMap(String url, String match, boolean isFirst) throws ScannerException { RegexMatcher matcher = new RegexTypeMatcher( RegexType.FIND, match ); RegexResult result = matchRequest( url, matcher, isFirst ); return result.resultMap(); } protected String match(String url, String startMatch, String endMatch) throws ScannerException { List result = match(url, startMatch, endMatch, true); return result.isEmpty() ? null : result.get(0); } protected List match(String url, String startMatch, String endMatch, boolean isFirst) throws ScannerException { RegexMatcher matcher = new RegexTypeMatcher( startMatch, endMatch ); RegexResult result = matchRequest( url, matcher, isFirst ); return result.resultList(); } protected Map matchMap(String url, String startMatch, String endMatch, boolean isFirst) throws ScannerException { RegexMatcher matcher = new RegexTypeMatcher( startMatch, endMatch ); RegexResult result = matchRequest( url, matcher, isFirst ); return result.resultMap(); } protected RegexResult matchRequest(String url, RegexMatcher matcher, boolean isFirst) throws ScannerException { try (CloseableHttpResponse response = getClient().execute( new HttpGet(url), getContext() )) { try (BufferedReader br = new BufferedReader( new InputStreamReader( response.getEntity().getContent(), CHARSET ) )) { String line; while ((line = br.readLine()) != null) { matcher.match(line); if (isFirst && !matcher.result().isEmpty()) { break; } } } } catch (IOException cause) { throw new ScannerException( String.format("Error occurred for requesting URL: %s", url), cause ); } return matcher.result(); } private CloseableHttpClient getClient() { return Proxy.getRequestClient().getClient(); } private HttpClientContext getContext() { return Proxy.getRequestClient().getContext(); } }