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

281 lines
8.9 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;
/**
* 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
{
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);
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
{
PathPropertyList inputProperties = PathPropertyUtils.load(input);
String crawlCachePath = inputProperties.get("conf.crawl.cache");
logger.info("Cache setting: {}", inputProperties.get("conf.crawl.cache"));
CrawlCache cache = new CrawlCache(new File(crawlCachePath));
String className = inputProperties.get("conf.class");
if (StringUtils.equals(className, "federation"))
{
htmlizeFederation(inputProperties, cache);
}
else if (StringUtils.equals(className, "organization"))
{
htmlizeOrganization(inputProperties, cache);
}
else if (StringUtils.equals(className, "service"))
{
htmlizeService(inputProperties, cache);
}
else
{
}
}
/**
* Htmlize federation.
*
* @param federation
* the federation
* @param cache
* the cache
*/
public static void htmlizeFederation(final PathPropertyList federation, final CrawlCache cache)
{
}
public static void htmlizeOrganization(final PathPropertyList federation, final CrawlCache cache)
{
// Copy commons files (index, images, favicon, css…).
// Download federation stuff (favicon, logo…).
// Build the federation page.
// For each organization
// Download organization stuff (favicon, logo…).
// Build organization page.
// for each service
// Download service stuff (favicon, logo…).
// Build service page.
}
public static void htmlizeService(final PathPropertyList federation, final CrawlCache cache)
{
}
}