/* * Copyright (C) 2020 Christian Pierre MOMON * * 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 . */ package fr.devinsy.statoolinfos.core; import java.io.File; import java.io.IOException; import java.net.URL; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyUtils; /** * 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 * @throws StatoolInfosException * the statool infos exception */ 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; } } /** * Builds the file. * * @param key * the key * @return the file */ private File buildFile(final String key) { File result; result = new File(this.directory, DigestUtils.md5Hex(key)); // 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)) { 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()); } // return result; } /** * Restore properties. * * @param url * the url * @return the path property list * @throws IOException * Signals that an I/O exception has occurred. */ public PathPropertyList restoreProperties(final URL url) throws IOException { PathPropertyList result; 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; if (StringUtils.startsWith(url.getProtocol(), "http")) { final int TIMEOUT = 5000; result = restoreFile(url); FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT); } else { result = null; } // return result; } /** * Store. * * @param url * the url * @param properties * the properties * @return the file * @throws IOException * Signals that an I/O exception has occurred. */ public File storeProperties(final URL url, final PathPropertyList properties) throws IOException { File result; if ((url == null) || (!StringUtils.startsWith(url.getProtocol(), "http"))) { result = null; } else { String key = url.toString() + ".properties"; result = buildFile(key); 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; result = restoreFile(url); FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT); } } catch (IOException exception) { logger.info("Store faile for {}: {}", url, exception.getMessage()); result = null; } // return result; } }