/* * 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 org.hedgecode.chess.scanner.ChessHogScannerException; import org.hedgecode.chess.scanner.Initiable; import org.hedgecode.chess.scanner.Settings; import org.hedgecode.chess.scanner.SettingsBuilder; import org.hedgecode.chess.scanner.regex.RegexBuilder; import org.hedgecode.chess.scanner.regex.RegexParams; import org.hedgecode.chess.scanner.spi.ServiceRegistry; import static org.hedgecode.chess.scanner.regex.RegexBuilder.Type; /** * AbstractSettingsScanner * * @author Dmitry Samoshin aka gotty */ public abstract class AbstractSettingsScanner extends AbstractRequestScanner implements Initiable { private Settings settings; protected abstract String getResourceName(); @Override public void init() throws ChessHogScannerException { SettingsBuilder settingsBuilder = ServiceRegistry.singleProvider( SettingsBuilder.class ); settings = settingsBuilder.build( getResourceName() ); } protected Settings getSettings() { return settings; } protected String assignUrl(String tournamentId, String pageId) { String tournamentUrl = assignUrlWithParams( settings.getTournamentUrl(), settings.getTournamentQueryParams() ); return RegexBuilder.build( Type.TOURNAMENT, tournamentUrl, new RegexParams( tournamentId, settings.isTournamentMultiPage() ? pageId : null ) ); } protected String assignUrl(String gameId) { String gameUrl = settings.getGamePgnUrl() != null ? settings.getGamePgnUrl() : settings.getGameUrl(); return RegexBuilder.build( Type.GAME, assignUrlWithParams(gameUrl, settings.getGameQueryParams()), new RegexParams(gameId) ); } protected String assignUrl(String gameId, String tournamentId, boolean withTournament) { String gameUrl = settings.getGamePgnUrl() != null ? settings.getGamePgnUrl() : settings.getGameUrl(); return RegexBuilder.build( Type.GAME, assignUrlWithParams(gameUrl, settings.getGameQueryParams()), new RegexParams( gameId, tournamentId, withTournament ) ); } protected String assignUrl(String query, boolean isUrlEncode) { return RegexBuilder.build( Type.QUERY, settings.getTournamentQuery(), new RegexParams( query, isUrlEncode ) ); } private String assignUrlWithParams(String url, String params) { return params != null ? url.concat(params) : url; } }