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

331 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;
2020-09-15 03:16:26 +02:00
import java.nio.charset.StandardCharsets;
2020-09-13 01:28:27 +02:00
import java.time.LocalDateTime;
2020-09-15 03:16:26 +02:00
import org.apache.commons.io.FileUtils;
2020-09-13 01:28:27 +02:00
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-09-15 03:16:26 +02:00
import fr.devinsy.statoolinfos.htmlize.OrganizationPage;
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 input
* 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 input) throws StatoolInfosException, IOException
{
2020-09-13 04:12:59 +02:00
logger.info("Build {}", input.getAbsolutePath());
2020-09-13 01:28:27 +02:00
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.
2020-09-13 01:28:27 +02:00
*/
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.
2020-09-13 01:28:27 +02:00
*/
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 section = configuration.getByPrefix("subs");
for (PathProperty property : section)
{
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
{
PathPropertyList inputProperties = PathPropertyUtils.load(url);
logger.info("Crawling " + url);
PathPropertyList target = PathPropertyUtils.load(url);
PathPropertyList crawlSection = new PathPropertyList();
crawlSection.put("crawl.crawler", "StatoolInfos");
crawlSection.put("crawl.datetime", LocalDateTime.now().toString());
crawlSection.put("crawl.url", url.toString());
target.addAll(crawlSection);
2020-09-13 04:12:59 +02:00
cache.store(url.toString(), target);
//
2020-09-13 01:28:27 +02:00
PathPropertyList section = inputProperties.getByPrefix("subs");
for (PathProperty property : section)
{
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.
2020-09-13 01:28:27 +02:00
*/
public static void htmlize(final File input) throws StatoolInfosException, IOException
{
2020-09-15 03:16:26 +02:00
PathPropertyList properties = PathPropertyUtils.load(input);
2020-09-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
String className = properties.get("conf.class");
2020-09-13 01:28:27 +02:00
if (StringUtils.equals(className, "federation"))
{
2020-09-15 03:16:26 +02:00
Federation federation = Factory.loadFederation(properties);
htmlizeFederation(federation);
2020-09-13 01:28:27 +02:00
}
else if (StringUtils.equals(className, "organization"))
{
2020-09-15 03:16:26 +02:00
Organization organization = Factory.loadOrganization(properties);
htmlizeOrganization(organization);
2020-09-13 01:28:27 +02:00
}
else if (StringUtils.equals(className, "service"))
{
2020-09-15 03:16:26 +02:00
Service service = Factory.loadService(properties);
htmlizeService(service);
2020-09-13 01:28:27 +02:00
}
else
{
2020-09-13 04:12:59 +02:00
// TODO
2020-09-13 01:28:27 +02:00
}
}
/**
* Htmlize federation.
*
* @param federation
* the federation
* @param cache
* the cache
*/
2020-09-15 03:16:26 +02:00
private static void htmlizeFederation(final PathPropertyList federation)
2020-09-13 01:28:27 +02:00
{
}
2020-09-15 03:16:26 +02:00
/**
* Htmlize organization.
*
* @param federation
* the federation
* @param targetDirectory
* the target directory
* @throws IOException
* @throws StatoolInfosException
*/
private static void htmlizeOrganization(final Organization organization) throws IOException, StatoolInfosException
2020-09-13 01:28:27 +02:00
{
2020-09-15 03:16:26 +02:00
File targetDirectory = new File(organization.get("conf.htmlize.directory"));
logger.info("Htmlize target directory: {}", targetDirectory.getAbsoluteFile());
if (!targetDirectory.exists())
{
throw new IllegalArgumentException("Htmlize target directory is missing.");
}
else if (!targetDirectory.isDirectory())
{
throw new IllegalArgumentException("Htmlize target directory is not a directory.");
}
else
{
// Copy commons files (index, images, favicon, css…).
if (!new File(targetDirectory, "index.html").exists())
{
FileUtils.copyURLToFile(StatoolInfos.class.getResource("/fr/devinsy/statoolinfos/htmlize/stuff/index.html"), new File(targetDirectory, "index.html"));
FileUtils.copyURLToFile(StatoolInfos.class.getResource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos.css"), new File(targetDirectory, "statoolinfos.css"));
FileUtils.copyURLToFile(StatoolInfos.class.getResource("/fr/devinsy/statoolinfos/htmlize/stuff/Chart.bundle.min.js"), new File(targetDirectory, "Chart.bundle.min.js"));
}
2020-09-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
//
String page = OrganizationPage.build(organization);
FileUtils.write(new File(targetDirectory, organization.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8);
2020-09-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
// Download federation stuff (favicon, logo…).
// Build the federation page.
2020-09-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
// For each organization
// Download organization stuff (favicon, logo…).
// Build organization page.
// for each service
// Download service stuff (favicon, logo…).
// Build service page.
}
2020-09-13 01:28:27 +02:00
}
2020-09-15 03:16:26 +02:00
/**
* Htmlize service.
*
* @param federation
* the federation
*/
private static void htmlizeService(final PathPropertyList federation)
2020-09-13 01:28:27 +02:00
{
}
}