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

332 lines
8.6 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-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
import org.apache.commons.codec.digest.DigestUtils;
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;
import fr.devinsy.catgenerator.core.CatGenerator;
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.PathPropertyList;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
2020-09-13 01:28:27 +02:00
/**
* The Class CrawlCache.
*/
public class CrawlCache
{
private static Logger logger = LoggerFactory.getLogger(CrawlCache.class);
private File directory;
/**
* Instantiates a new crawl cache.
*
* @param directory
* the directory
2020-09-13 01:28:27 +02:00
* @throws StatoolInfosException
* the statool infos exception
2020-09-13 01:28:27 +02:00
*/
public CrawlCache(final File directory) throws StatoolInfosException
{
if (directory == null)
{
throw new IllegalArgumentException("Null parameter.");
}
else if (StringUtils.isBlank(directory.getName()))
{
throw new IllegalArgumentException("Undefined directory.");
2020-09-13 01:28:27 +02:00
}
else if (!directory.exists())
{
throw new IllegalArgumentException("Directory does not exist.");
}
else
{
this.directory = directory;
}
}
/**
* Builds the file.
*
* @param key
* the key
* @return the file
*/
private File buildFile(final String key)
2020-09-13 01:28:27 +02:00
{
File result;
result = new File(this.directory, DigestUtils.md5Hex(key));
2020-09-13 01:28:27 +02:00
//
return result;
}
/**
* Clear file in cache. Only files ending with ".property" are deleted.
*/
public void clear()
{
for (File file : this.directory.listFiles())
{
if ((file.isFile()) && ((file.getName().endsWith(".properties")) || file.getName().length() == 32))
2020-09-13 01:28:27 +02:00
{
logger.info("Deleting " + file.getName());
file.delete();
}
}
}
/**
* Restore file.
*
* @param url
* the url
* @return the file
*/
public File restoreFile(final URL url)
{
File result;
if (url == null)
{
throw new IllegalArgumentException("Null parameter.");
}
else
{
result = buildFile(url.toString());
2020-09-17 03:59:11 +02:00
if (!result.exists())
{
result = null;
}
}
//
return result;
}
2020-09-17 03:59:11 +02:00
/**
* Restore file to.
*
* @param url
* the url
* @param target
* the target
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void restoreFileTo(final URL url, final File target) throws IOException
{
if (url != null)
{
File logoFile = restoreFile(url);
if (logoFile != null)
{
FileUtils.copyFile(logoFile, target);
}
}
}
/**
2020-09-19 02:37:52 +02:00
* Restore logo to.
2020-09-17 03:59:11 +02:00
*
* @param url
* the url
2020-09-19 02:37:52 +02:00
* @param target
* the target
* @param seed
* the seed
2020-09-17 03:59:11 +02:00
* @throws IOException
* Signals that an I/O exception has occurred.
*/
2020-09-19 02:37:52 +02:00
public void restoreLogoTo(final URL url, final File target, final String seed) throws IOException
2020-09-17 03:59:11 +02:00
{
2020-09-19 02:37:52 +02:00
if ((target == null) || (seed == null))
2020-09-17 03:59:11 +02:00
{
2020-09-19 02:37:52 +02:00
throw new IllegalArgumentException("Null parameter.");
2020-09-17 03:59:11 +02:00
}
else
{
2020-09-19 02:37:52 +02:00
if (url == null)
2020-09-17 03:59:11 +02:00
{
2020-09-19 02:37:52 +02:00
try
{
logger.info("CatGeneratoring cat avatar (1): {}", target.getAbsoluteFile());
CatGenerator.buildAvatarTo(seed, target);
2020-09-19 02:37:52 +02:00
}
catch (IOException exception)
{
logger.warn("CatGeneratoring failed for {}: {}", seed, exception.getMessage());
StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/default-organization-logo.png", target);
}
2020-09-17 03:59:11 +02:00
}
else
{
2020-09-19 02:37:52 +02:00
File logoFile = restoreFile(url);
if (logoFile == null)
{
try
{
logger.info("CatGeneratoring cat avatar (2): {}", target.getAbsoluteFile());
CatGenerator.buildAvatarTo(seed, target);
2020-09-19 02:37:52 +02:00
}
catch (IOException exception)
{
logger.warn("CatGeneratoring failed for {}: {}", seed, exception.getMessage());
StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/default-organization-logo.png", target);
}
}
else
{
FileUtils.copyFile(logoFile, target);
}
2020-09-17 03:59:11 +02:00
}
}
}
/**
* Restore properties.
2020-09-13 01:28:27 +02:00
*
* @param url
* the url
* @return the path property list
* @throws IOException
* Signals that an I/O exception has occurred.
2020-09-13 01:28:27 +02:00
*/
2020-09-17 02:28:37 +02:00
public PathProperties restoreProperties(final URL url) throws IOException
2020-09-13 01:28:27 +02:00
{
2020-09-17 02:28:37 +02:00
PathProperties result;
2020-09-13 01:28:27 +02:00
if (url == null)
{
result = new PathPropertyList();
}
else
{
File file = buildFile(url.toString() + ".properties");
result = PathPropertyUtils.load(file);
}
//
return result;
}
/**
* Store.
*
* @param url
* the url
* @return the file
* @throws IOException
*/
public File store(final URL url) throws IOException
{
File result;
2020-09-13 01:28:27 +02:00
if (StringUtils.startsWith(url.getProtocol(), "http"))
{
final int TIMEOUT = 5000;
2020-09-17 03:59:11 +02:00
result = buildFile(url.toString());
FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT);
}
else
{
result = null;
}
2020-09-13 01:28:27 +02:00
//
return result;
}
/**
* Store.
*
* @param url
* the url
* @param properties
* the properties
* @return the file
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
*/
2020-09-17 02:28:37 +02:00
public File storeProperties(final URL url, final PathProperties properties) throws IOException
2020-09-13 01:28:27 +02:00
{
File result;
if ((url == null) || (!StringUtils.startsWith(url.getProtocol(), "http")))
2020-09-15 03:16:26 +02:00
{
result = null;
}
else
{
String key = url.toString() + ".properties";
result = buildFile(key);
2020-09-13 01:28:27 +02:00
2020-09-15 03:16:26 +02:00
PathPropertyUtils.save(result, properties);
}
//
return result;
}
/**
* Store quietly.
*
* @param url
* the url
* @return the file
*/
public File storeQuietly(final URL url)
{
File result;
try
{
if ((url == null) || (!StringUtils.startsWith(url.getProtocol(), "http")))
{
result = null;
}
else
{
final int TIMEOUT = 5000;
2020-09-17 03:59:11 +02:00
result = buildFile(url.toString());
FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT);
2020-09-17 03:59:11 +02:00
logger.info("Crawled {}", url);
}
}
catch (IOException exception)
2020-09-15 03:16:26 +02:00
{
logger.info("Store faile for {}: {}", url, exception.getMessage());
2020-09-15 03:16:26 +02:00
result = null;
}
2020-09-13 01:28:27 +02:00
//
return result;
}
}