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-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 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-17 02:28:37 +02:00
PathProperties inputProperties = PathPropertyUtils.load(input);
2020-09-13 01:28:27 +02:00
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.
2020-09-17 02:28:37 +02:00
PathProperties targetProperties = new PathPropertyList();
2020-09-13 01:28:27 +02:00
// Add generator paths.
2020-09-17 02:28:37 +02:00
PathProperties fileSection = new PathPropertyList();
2020-09-13 01:28:27 +02:00
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"));
2020-09-17 02:28:37 +02:00
targetProperties.add(fileSection);
2020-09-13 01:28:27 +02:00
//
2020-09-17 02:28:37 +02:00
targetProperties.add(inputProperties);
2020-09-13 01:28:27 +02:00
// Clear configuration paths.
targetProperties.removeSection("conf");
// Save target file.
2020-09-17 02:28:37 +02:00
File targetFile = new File(targetDirectory, input.getName());
2020-09-13 01:28:27 +02:00
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
{
2020-09-17 02:28:37 +02:00
PathProperties inputProperties = PathPropertyUtils.load(input);
2020-09-13 01:28:27 +02:00
{
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
{
2020-09-17 02:28:37 +02:00
PathProperties configuration = PathPropertyUtils.load(input);
2020-09-13 01:28:27 +02:00
String crawlCachePath = configuration.get("conf.crawl.cache");
logger.info("Cache setting: {}", configuration.get("conf.crawl.cache"));
CrawlCache cache = new CrawlCache(new File(crawlCachePath));
2020-09-17 02:28:37 +02:00
PathProperties 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);
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("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
//
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 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-17 02:28:37 +02:00
PathProperties 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
}
}
}