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;
|
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
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.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.
|
|
|
|
*
|
2020-09-13 02:30:53 +02:00
|
|
|
* @param directory
|
|
|
|
* the directory
|
2020-09-13 01:28:27 +02:00
|
|
|
* @throws StatoolInfosException
|
2020-09-13 02:30:53 +02:00
|
|
|
* 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("Blank directory.");
|
|
|
|
}
|
|
|
|
else if (!directory.exists())
|
|
|
|
{
|
|
|
|
throw new IllegalArgumentException("Directory does not exist.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.directory = directory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
private File buildFile(final String url, final String additionalName)
|
2020-09-13 01:28:27 +02:00
|
|
|
{
|
|
|
|
File result;
|
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
result = new File(this.directory, DigestUtils.md5Hex(url) + additionalName);
|
2020-09-13 01:28:27 +02:00
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
// result = new File(this.directory, DigestUtils.sha1Hex(url));
|
|
|
|
// String normalizeUrl = StringUtils.removeAll(url,
|
|
|
|
// "^https*://").replace('/', '+');
|
|
|
|
// result = new File(this.directory, normalizeUrl);
|
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")))
|
|
|
|
{
|
|
|
|
logger.info("Deleting " + file.getName());
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the url
|
|
|
|
* @return the path property list
|
|
|
|
* @throws IOException
|
2020-09-13 02:30:53 +02:00
|
|
|
* Signals that an I/O exception has occurred.
|
2020-09-13 01:28:27 +02:00
|
|
|
*/
|
|
|
|
public PathPropertyList load(final String url) throws IOException
|
|
|
|
{
|
|
|
|
PathPropertyList result;
|
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
File file = buildFile(url, ".properties");
|
2020-09-13 01:28:27 +02:00
|
|
|
|
|
|
|
result = PathPropertyUtils.load(file);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the url
|
|
|
|
* @param properties
|
|
|
|
* the properties
|
2020-09-13 02:30:53 +02:00
|
|
|
* @return the file
|
2020-09-13 01:28:27 +02:00
|
|
|
* @throws IOException
|
2020-09-13 02:30:53 +02:00
|
|
|
* Signals that an I/O exception has occurred.
|
2020-09-13 01:28:27 +02:00
|
|
|
*/
|
|
|
|
public File store(final String url, final PathPropertyList properties) throws IOException
|
|
|
|
{
|
|
|
|
File result;
|
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
if (StringUtils.startsWith(url, "http"))
|
|
|
|
{
|
|
|
|
result = buildFile(url, ".properties");
|
2020-09-13 01:28:27 +02:00
|
|
|
|
2020-09-15 03:16:26 +02:00
|
|
|
PathPropertyUtils.save(result, properties);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
2020-09-13 01:28:27 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|