/* * Copyright (C) 2020 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * * StatoolInfos is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * StatoolInfos is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with StatoolInfos. If not, see . */ package fr.devinsy.statoolinfos.core; import java.io.File; import java.io.IOException; import java.net.URL; import java.time.LocalDateTime; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.htmlize.Htmlizer; import fr.devinsy.statoolinfos.properties.PathProperties; import fr.devinsy.statoolinfos.properties.PathProperty; import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyUtils; /** * The Class StatoolInfos. */ public class StatoolInfos { private static Logger logger = LoggerFactory.getLogger(StatoolInfos.class); /** * Builds the. * * @param configurationFile * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void build(final File configurationFile) throws StatoolInfosException, IOException { logger.info("Build {}", configurationFile.getAbsolutePath()); Configuration configuration = Factory.loadConfiguration(configurationFile); logger.info("Build input setting: {}", configuration.getBuildInput()); logger.info("Build directory setting: {}", configuration.getBuildDirectoryPath()); File inputFile = configuration.getBuildInput(); File buildDirectory = configuration.getBuildDirectory(); if (inputFile == null) { throw new StatoolInfosException("Input is undefined."); } else if (!inputFile.exists()) { throw new StatoolInfosException("Input does not exist."); } else if (!inputFile.isFile()) { throw new StatoolInfosException("Input is not a file."); } else if (buildDirectory == null) { throw new StatoolInfosException("Build directory is undefined."); } else if (!buildDirectory.exists()) { throw new StatoolInfosException("Build directory does not exist."); } else if (!buildDirectory.isDirectory()) { throw new StatoolInfosException("Build directory is not a directory."); } else { // Build file section. PathProperties target = new PathPropertyList(); target.put("file.class", configuration.get("conf.class")); target.put("file.generator", "StatoolInfos"); target.put("file.datetime", LocalDateTime.now().toString()); target.put("file.protocol", configuration.get("conf.protocol")); // Load input properties. PathProperties input = PathPropertyUtils.load(inputFile); // Add input properties with file section ones. target.add(input); // Save the build properties. File targetFile = new File(buildDirectory, configurationFile.getName()); PathPropertyUtils.save(targetFile, target); } } /** * Clear. * * @param configurationFile * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void clear(final File configurationFile) throws StatoolInfosException, IOException { Configuration configuration = Factory.loadConfiguration(configurationFile); { logger.info("Build directory setting: {}", configuration.getBuildDirectoryPath()); String path = configuration.getBuildDirectoryPath(); if (StringUtils.isBlank(path)) { logger.warn("Undefined build directory."); } else if (!new File(path).exists()) { logger.warn("Build directory does not exist: {}.", path); } else { File buildDirectory = configuration.getBuildDirectory(); for (File file : buildDirectory.listFiles()) { if ((file.isFile()) && (StringUtils.endsWithAny(file.getName(), ".properties"))) { logger.info("Deleting " + file.getName()); file.delete(); } } } } { logger.info("Cache setting: {}", configuration.getCrawlCachePath()); String path = configuration.getCrawlCachePath(); if (StringUtils.isBlank(path)) { logger.warn("Undefined crawl cache."); } else if (!new File(path).exists()) { logger.warn("Crawl cache does not exist: {}.", path); } else { CrawlCache cache = configuration.getCrawlCache(); cache.clear(); } } { logger.info("Htmlize directory setting: {}", configuration.getHtmlizeDirectoryPath()); String htmlDirectoryPath = configuration.getHtmlizeDirectoryPath(); if (StringUtils.isBlank(htmlDirectoryPath)) { logger.warn("Undefined htmlize directory."); } else if (!new File(htmlDirectoryPath).exists()) { logger.warn("Htmlize directory does not exist: {}.", htmlDirectoryPath); } else { File htmlizeDirectory = configuration.getHtmlizeDirectory(); for (File file : htmlizeDirectory.listFiles()) { if ((file.isFile()) && (StringUtils.endsWithAny(file.getName(), ".properties", ".js", ".html", ".ico", ".css", ".jpg", ".xhtml"))) { logger.info("Deleting " + file.getName()); file.delete(); } } } } } /** * Crawl. * * @param configurationFile * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void crawl(final File configurationFile) throws StatoolInfosException, IOException { Configuration configuration = Factory.loadConfiguration(configurationFile); logger.info("Crawl input setting: {}", configuration.getCrawlInputPath()); logger.info("Crawl cache setting: {}", configuration.getCrawlCachePath()); CrawlCache cache = configuration.getCrawlCache(); PathProperties input = PathPropertyUtils.load(configuration.getCrawlInput()); cache.storeQuietly(input.getURL("federation.logo")); cache.storeQuietly(input.getURL("organization.logo")); PathProperties subs = input.getByPrefix("subs"); for (PathProperty property : subs) { URL url = new URL(property.getValue()); crawl(url, cache); } } /** * Crawl. * * @param url * the input * @param cache * the cache * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void crawl(final URL url, final CrawlCache cache) throws StatoolInfosException, IOException { logger.info("Crawling " + url); File file = cache.store(url); PathProperties properties = PathPropertyUtils.load(file); PathProperties crawlSection = new PathPropertyList(); crawlSection.put("crawl.crawler", "StatoolInfos"); crawlSection.put("crawl.datetime", LocalDateTime.now().toString()); crawlSection.put("crawl.url", url.toString()); properties.add(crawlSection); cache.storeProperties(url, properties); cache.storeQuietly(properties.getURL("organization.logo")); cache.storeQuietly(properties.getURL("service.logo")); // PathProperties subs = properties.getByPrefix("subs"); for (PathProperty property : subs) { URL subUrl = new URL(property.getValue()); crawl(subUrl, cache); } } /** * Htmlize. * * @param configurationFile * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void htmlize(final File configurationFile) throws StatoolInfosException, IOException { Configuration configuration = Factory.loadConfiguration(configurationFile); logger.info("Cache setting: {}", configuration.getCrawlCachePath()); logger.info("Htmlize input setting: {}", configuration.getHtmlizeInputPath()); logger.info("Htmlize directory setting: {}", configuration.getHtmlizeDirectoryPath()); File htmlizeInput = configuration.getHtmlizeInput(); File htmlizeDirectory = configuration.getHtmlizeDirectory(); if (htmlizeInput == null) { throw new IllegalArgumentException("Htmlize input undefined."); } else if (!htmlizeInput.exists()) { throw new IllegalArgumentException("Htmlize input is missing."); } else if (htmlizeInput.isDirectory()) { throw new IllegalArgumentException("Htmlize input is a directory."); } else if (htmlizeDirectory == null) { throw new IllegalArgumentException("Htmlize directory undefined."); } else if (!htmlizeDirectory.exists()) { throw new IllegalArgumentException("Htmlize directory is missing."); } else if (!htmlizeDirectory.isDirectory()) { throw new IllegalArgumentException("Htmlize directory is not a directory."); } else { if (configuration.isFederation()) { Htmlizer.htmlizeFederation(configuration); } else if (configuration.isOrganization()) { Htmlizer.htmlizeOrganisation(configuration); } else { logger.warn("No htmlize for this input: {}.", configuration.getClassName()); } } } }