/* * 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.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 input * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void build(final File input) throws StatoolInfosException, IOException { logger.info("Build {}", input.getAbsolutePath()); PathPropertyList inputProperties = PathPropertyUtils.load(input); String buildDirectoryName = inputProperties.get("conf.build.directory"); if (StringUtils.isBlank(buildDirectoryName)) { throw new StatoolInfosException("Build directory target is undefined."); } else { File targetDirectory = new File(buildDirectoryName); if (targetDirectory.exists()) { // Load configuration file. File targetFile = new File(targetDirectory, input.getName()); // PathPropertyList targetProperties = new PathPropertyList(); // Add generator paths. PathPropertyList fileSection = new PathPropertyList(); fileSection.put("file.class", inputProperties.get("conf.class")); fileSection.put("file.generator", "StatoolInfos"); fileSection.put("file.datetime", LocalDateTime.now().toString()); fileSection.put("file.protocol", inputProperties.get("conf.protocol")); targetProperties.addAll(fileSection); // targetProperties.addAll(inputProperties); // Clear configuration paths. targetProperties.removeSection("conf"); // Save target file. PathPropertyUtils.save(targetFile, targetProperties); } else { throw new StatoolInfosException("Build directory target does not exist."); } } } /** * Clear. * * @param input * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void clear(final File input) throws StatoolInfosException, IOException { PathPropertyList inputProperties = PathPropertyUtils.load(input); { String crawlCacheName = inputProperties.get("conf.crawl.cache"); if (StringUtils.isBlank(crawlCacheName)) { throw new StatoolInfosException("Crawl cache directory is undefined."); } else { File crawlCacheDirectory = new File(crawlCacheName); if (crawlCacheDirectory.exists()) { CrawlCache cache = new CrawlCache(crawlCacheDirectory); cache.clear(); } else { throw new StatoolInfosException("Crawl cache directory does not exist."); } } } { String buildDirectoryName = inputProperties.get("conf.build.directory"); if (StringUtils.isBlank(buildDirectoryName)) { throw new StatoolInfosException("Build directory is undefined."); } else { File buildDirectory = new File(buildDirectoryName); if (buildDirectory.exists()) { CrawlCache cache = new CrawlCache(buildDirectory); cache.clear(); } else { throw new StatoolInfosException("Crawl cache directory does not exist."); } } } } /** * Crawl. * * @param input * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void crawl(final File input) throws StatoolInfosException, IOException { PathPropertyList configuration = PathPropertyUtils.load(input); String crawlCachePath = configuration.get("conf.crawl.cache"); logger.info("Cache setting: {}", configuration.get("conf.crawl.cache")); CrawlCache cache = new CrawlCache(new File(crawlCachePath)); PathPropertyList subs = configuration.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); PathPropertyList properties = PathPropertyUtils.load(file); PathPropertyList crawlSection = new PathPropertyList(); crawlSection.put("crawl.crawler", "StatoolInfos"); crawlSection.put("crawl.datetime", LocalDateTime.now().toString()); crawlSection.put("crawl.url", url.toString()); properties.addAll(crawlSection); cache.storeProperties(url, properties); cache.storeQuietly(properties.getURL("federation.logo")); cache.storeQuietly(properties.getURL("federation.logo.url")); cache.storeQuietly(properties.getURL("organization.logo")); cache.storeQuietly(properties.getURL("organization.logo.url")); cache.storeQuietly(properties.getURL("service.logo")); cache.storeQuietly(properties.getURL("service.logo.url")); // PathPropertyList subs = properties.getByPrefix("subs"); for (PathProperty property : subs) { URL subUrl = new URL(property.getValue()); crawl(subUrl, cache); } } /** * Htmlize. * * @param input * the input * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void htmlize(final File input) throws StatoolInfosException, IOException { PathPropertyList properties = PathPropertyUtils.load(input); String className = properties.get("conf.class"); if (StringUtils.equals(className, "federation")) { Federation federation = Factory.loadFederation(properties); Htmlizer.htmlize(federation); } else if (StringUtils.equals(className, "organization")) { Organization organization = Factory.loadOrganization(properties); Htmlizer.htmlize(organization); } else if (StringUtils.equals(className, "service")) { Service service = Factory.loadService(properties); Htmlizer.htmlize(service); } else { // TODO } } }