statoolinfosweb/src/fr/devinsy/statoolinfos/core/StatoolInfos.java

327 lines
11 KiB
Java
Raw Normal View History

2020-09-13 01:28:27 +02:00
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
2020-09-17 02:28:37 +02:00
import fr.devinsy.statoolinfos.properties.PathProperties;
2020-09-15 03:16:26 +02:00
import fr.devinsy.statoolinfos.properties.PathProperty;
import fr.devinsy.statoolinfos.properties.PathPropertyList;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
2020-09-13 01:28:27 +02:00
/**
* 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
2020-09-13 01:28:27 +02:00
* @throws IOException
* Signals that an I/O exception has occurred.
2020-09-13 01:28:27 +02:00
*/
public static void build(final File configurationFile) throws StatoolInfosException, IOException
2020-09-13 01:28:27 +02:00
{
logger.info("Build {}", configurationFile.getAbsolutePath());
2020-09-13 01:28:27 +02:00
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)
2020-09-13 01:28:27 +02:00
{
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.");
2020-09-13 01:28:27 +02:00
}
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"));
2020-09-13 01:28:27 +02:00
// Load input properties.
PathProperties input = PathPropertyUtils.load(inputFile);
2020-09-13 01:28:27 +02:00
// Add input properties with file section ones.
target.add(input);
2020-09-13 01:28:27 +02:00
// Save the build properties.
File targetFile = new File(buildDirectory, configurationFile.getName());
PathPropertyUtils.save(targetFile, target);
2020-09-13 01:28:27 +02:00
}
}
/**
* Clear.
*
* @param configurationFile
2020-09-13 01:28:27 +02:00
* the input
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
2020-09-13 01:28:27 +02:00
*/
public static void clear(final File configurationFile) throws StatoolInfosException, IOException
2020-09-13 01:28:27 +02:00
{
Configuration configuration = Factory.loadConfiguration(configurationFile);
2020-09-13 01:28:27 +02:00
{
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())
2020-09-13 01:28:27 +02:00
{
logger.warn("Build directory does not exist: {}.", path);
2020-09-13 01:28:27 +02:00
}
else
{
File buildDirectory = configuration.getBuildDirectory();
2020-09-13 01:28:27 +02:00
for (File file : buildDirectory.listFiles())
2020-09-13 01:28:27 +02:00
{
if ((file.isFile()) && (StringUtils.endsWithAny(file.getName(), ".properties")))
{
logger.info("Deleting " + file.getName());
file.delete();
}
2020-09-13 01:28:27 +02:00
}
}
}
{
logger.info("Cache setting: {}", configuration.getCrawlCachePath());
String path = configuration.getCrawlCachePath();
if (StringUtils.isBlank(path))
2020-09-13 01:28:27 +02:00
{
logger.warn("Undefined crawl cache.");
}
else if (!new File(path).exists())
{
logger.warn("Crawl cache does not exist: {}.", path);
2020-09-13 01:28:27 +02:00
}
else
{
CrawlCache cache = configuration.getCrawlCache();
cache.clear();
}
}
2020-09-13 01:28:27 +02:00
{
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())
2020-09-13 01:28:27 +02:00
{
if ((file.isFile()) && (StringUtils.endsWithAny(file.getName(), ".properties", ".js", ".html", ".ico", ".css", ".jpg", ".xhtml")))
{
logger.info("Deleting " + file.getName());
file.delete();
}
2020-09-13 01:28:27 +02:00
}
}
}
}
/**
* Crawl.
*
* @param configurationFile
2020-09-13 01:28:27 +02:00
* the input
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
2020-09-13 01:28:27 +02:00
*/
public static void crawl(final File configurationFile) throws StatoolInfosException, IOException
2020-09-13 01:28:27 +02:00
{
Configuration configuration = Factory.loadConfiguration(configurationFile);
2020-09-13 01:28:27 +02:00
logger.info("Crawl input setting: {}", configuration.getCrawlInputPath());
logger.info("Crawl cache setting: {}", configuration.getCrawlCachePath());
2020-09-13 01:28:27 +02:00
CrawlCache cache = configuration.getCrawlCache();
2020-09-17 03:59:11 +02:00
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)
2020-09-13 01:28:27 +02:00
{
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);
2020-09-17 02:28:37 +02:00
PathProperties properties = PathPropertyUtils.load(file);
2020-09-13 01:28:27 +02:00
2020-09-17 02:28:37 +02:00
PathProperties crawlSection = new PathPropertyList();
2020-09-13 01:28:27 +02:00
crawlSection.put("crawl.crawler", "StatoolInfos");
crawlSection.put("crawl.datetime", LocalDateTime.now().toString());
crawlSection.put("crawl.url", url.toString());
2020-09-17 02:28:37 +02:00
properties.add(crawlSection);
cache.storeProperties(url, properties);
2020-09-13 01:28:27 +02:00
cache.storeQuietly(properties.getURL("organization.logo"));
cache.storeQuietly(properties.getURL("service.logo"));
2020-09-13 04:12:59 +02:00
//
2020-09-17 02:28:37 +02:00
PathProperties subs = properties.getByPrefix("subs");
for (PathProperty property : subs)
2020-09-13 01:28:27 +02:00
{
URL subUrl = new URL(property.getValue());
crawl(subUrl, cache);
}
}
/**
* Htmlize.
*
* @param configurationFile
2020-09-13 01:28:27 +02:00
* the input
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
2020-09-13 01:28:27 +02:00
*/
public static void htmlize(final File configurationFile) throws StatoolInfosException, IOException
2020-09-13 01:28:27 +02:00
{
Configuration configuration = Factory.loadConfiguration(configurationFile);
2020-09-13 01:28:27 +02:00
logger.info("Cache setting: {}", configuration.getCrawlCachePath());
logger.info("Htmlize input setting: {}", configuration.getHtmlizeInputPath());
logger.info("Htmlize directory setting: {}", configuration.getHtmlizeDirectoryPath());
2020-09-17 03:59:11 +02:00
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())
2020-09-13 01:28:27 +02:00
{
throw new IllegalArgumentException("Htmlize directory is missing.");
2020-09-13 01:28:27 +02:00
}
else if (!htmlizeDirectory.isDirectory())
2020-09-13 01:28:27 +02:00
{
throw new IllegalArgumentException("Htmlize directory is not a directory.");
2020-09-13 01:28:27 +02:00
}
else
{
if (configuration.isFederation())
{
Htmlizer.htmlizeFederation(configuration);
}
else if (configuration.isOrganization())
{
Htmlizer.htmlizeOrganisation(configuration);
}
else
{
logger.warn("No htmlize for this input: {}.", configuration.getClassName());
}
2020-09-13 01:28:27 +02:00
}
}
}