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.LocalDate;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
import fr.devinsy.strings.StringsUtils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class StatoolInfosUtils.
|
|
|
|
*/
|
|
|
|
public class StatoolInfosUtils
|
|
|
|
{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(StatoolInfosUtils.class);
|
|
|
|
|
|
|
|
public static final DateTimeFormatter PATTERN_SHORTDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.FRANCE);
|
|
|
|
public static final DateTimeFormatter PATTERN_LONGDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy HH':'mm", Locale.FRANCE);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate cat logo.
|
|
|
|
*
|
|
|
|
* @param seed
|
|
|
|
* the seed
|
|
|
|
* @param target
|
|
|
|
* the target
|
|
|
|
* @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 static void generateCatLogo(final String seed, final File target) throws IOException
|
|
|
|
{
|
|
|
|
URL source = new URL("https://www.peppercarrot.com/extras/html/2016_cat-generator/avatar.php?seed=" + seed);
|
|
|
|
|
|
|
|
FileUtils.copyURLToFile(source, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current time in long format.
|
|
|
|
*
|
|
|
|
* @return the long
|
|
|
|
*/
|
|
|
|
public static long now()
|
|
|
|
{
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human long.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanLong(final LocalDateTime value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = toHumanLong(value, null);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human long.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @param defaultValue
|
|
|
|
* the default value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanLong(final LocalDateTime value, final String defaultValue)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = value.format(PATTERN_LONGDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human short.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanShort(final LocalDateTime value)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = toHumanShort(value, null);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To human short.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* the value
|
|
|
|
* @param defaultValue
|
|
|
|
* the default value
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toHumanShort(final LocalDateTime value, final String defaultValue)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = value.format(PATTERN_SHORTDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Integer toInteger(final String value)
|
|
|
|
{
|
|
|
|
Integer result;
|
|
|
|
|
|
|
|
if ((value == null) || (!NumberUtils.isDigits(value)))
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = Integer.parseInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To Json numbers.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toJSonNumbers(final StringList source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = StringsUtils.toString(source, "[", ",", "]");
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To J son numbers.
|
|
|
|
*
|
|
|
|
* @param labels
|
|
|
|
* the labels
|
|
|
|
* @param values
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toJSonNumbers(final StringList labels, final StringList values)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
Iterator<String> labelIterator = labels.iterator();
|
|
|
|
Iterator<String> valueIterator = values.iterator();
|
|
|
|
|
|
|
|
StringList buffer = new StringList();
|
|
|
|
while (labelIterator.hasNext())
|
|
|
|
{
|
|
|
|
String label = labelIterator.next();
|
|
|
|
String value = valueIterator.next();
|
|
|
|
|
|
|
|
// buffer.append("{t: new Date('" + label + "'), y: " + value +
|
|
|
|
// "}");
|
|
|
|
buffer.append("{t: '" + label + "', y: " + value + "}");
|
|
|
|
}
|
|
|
|
result = StringsUtils.toString(buffer, "[", ",", "]");
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To Json strings.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toJSonStrings(final StringList source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
StringList target = new StringList();
|
|
|
|
|
|
|
|
target.append("[");
|
|
|
|
for (String string : source)
|
|
|
|
{
|
|
|
|
target.append("'");
|
|
|
|
target.append(string);
|
|
|
|
target.append("'");
|
|
|
|
target.append(",");
|
|
|
|
}
|
|
|
|
target.removeLast();
|
|
|
|
target.append("]");
|
|
|
|
|
|
|
|
result = target.toString();
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To technical name.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toTechnicalName(final String source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = StringUtils.stripAccents(source).replaceAll("[^A-Za-z0-9]+", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To year week.
|
|
|
|
*
|
|
|
|
* @param source
|
|
|
|
* the source
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public static String toYearWeek(final LocalDate source)
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
{
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = source.format(DateTimeFormatter.ofPattern("YYYYww", Locale.FRANCE));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unactivate SSL check.
|
|
|
|
*/
|
|
|
|
public static void unactivateSSLCheck()
|
|
|
|
{
|
|
|
|
// Create a trust manager that does not validate certificate chains.
|
|
|
|
TrustManager[] trustAllCerts = new TrustManager[] {
|
|
|
|
new X509TrustManager()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Install the all-trusting trust manager.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// SSLContext context = SSLContext.getInstance("SSL");
|
|
|
|
SSLContext context = SSLContext.getInstance("TLS");
|
|
|
|
context.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|