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

261 lines
8.7 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-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 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 subs = configuration.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);
PathPropertyList properties = PathPropertyUtils.load(file);
2020-09-13 01:28:27 +02:00
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);
2020-09-13 01:28:27 +02:00
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"));
2020-09-13 04:12:59 +02:00
//
PathPropertyList 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 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);
Htmlizer.htmlize(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);
Htmlizer.htmlize(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);
Htmlizer.htmlize(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
}
}
}