statoolinfosweb/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLog.java

318 lines
6.9 KiB
Java
Raw Normal View History

2021-02-11 02:36:03 +01:00
/*
* Copyright (C) 2021 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.metrics.http;
2021-02-11 02:36:03 +01:00
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.regex.Pattern;
2021-05-25 12:39:59 +02:00
import org.apache.commons.lang3.StringUtils;
2021-02-11 02:36:03 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.metrics.TimeMarkUtils;
2021-02-11 02:36:03 +01:00
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
2021-02-20 02:00:00 +01:00
* The Class HttpAccessLog.
2021-02-11 02:36:03 +01:00
*/
2021-02-20 02:00:00 +01:00
public class HttpAccessLog
2021-02-11 02:36:03 +01:00
{
2021-02-20 02:00:00 +01:00
private static Logger logger = LoggerFactory.getLogger(HttpAccessLog.class);
2021-02-11 02:36:03 +01:00
public static final Pattern IPV4_PATTERN = Pattern.compile("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}");
public static final Pattern IPV6_PATTERN = Pattern.compile("([0-9a-f]{1,4}:{1,2}){4,7}([0-9a-f]){1,4}", Pattern.CASE_INSENSITIVE);
2021-06-05 17:56:07 +02:00
private String ip;
2021-02-11 02:36:03 +01:00
private String remoteUser;
private LocalDateTime time;
private String request;
private HttpStatus status;
private long bodyBytesSent;
private String referer;
2021-02-18 05:33:27 +01:00
private UserAgent userAgent;
2021-02-11 02:36:03 +01:00
/**
2021-02-20 02:00:00 +01:00
* Instantiates a new http access log.
2021-02-11 02:36:03 +01:00
*/
2021-02-20 02:00:00 +01:00
public HttpAccessLog()
2021-02-11 02:36:03 +01:00
{
2021-06-05 17:56:07 +02:00
this.ip = null;
2021-02-11 02:36:03 +01:00
this.remoteUser = null;
this.time = null;
this.request = null;
this.status = null;
this.bodyBytesSent = 0;
this.referer = null;
2021-02-18 05:33:27 +01:00
this.userAgent = null;
2021-02-11 02:36:03 +01:00
}
public long getBodyBytesSent()
{
return this.bodyBytesSent;
}
public String getDate()
{
String result;
result = this.time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.FRANCE));
//
return result;
}
2021-06-05 17:56:07 +02:00
public String getIp()
2021-02-11 02:36:03 +01:00
{
2021-06-05 17:56:07 +02:00
return this.ip;
2021-02-11 02:36:03 +01:00
}
2021-06-05 17:56:07 +02:00
public String getReferer()
2021-02-11 02:36:03 +01:00
{
2021-06-05 17:56:07 +02:00
return this.referer;
2021-02-11 02:36:03 +01:00
}
public String getRemoteUser()
{
return this.remoteUser;
}
public String getRequest()
{
return this.request;
}
public HttpStatus getStatus()
{
return this.status;
}
public LocalDateTime getTime()
{
return this.time;
}
2021-02-18 05:33:27 +01:00
public UserAgent getUserAgent()
{
return this.userAgent;
}
2021-02-11 02:36:03 +01:00
/**
* Gets the year.
*
* @return the year
*/
public String getYear()
{
String result;
if (this.time == null)
{
result = null;
}
else
{
2021-02-18 05:33:27 +01:00
result = TimeMarkUtils.yearOf(this.time);
2021-02-11 02:36:03 +01:00
}
//
return result;
}
/**
* Gets the year month.
*
* @return the year month
*/
public String getYearMonth()
{
String result;
if (this.time == null)
{
result = null;
}
else
{
2021-02-18 05:33:27 +01:00
result = TimeMarkUtils.yearMonthOf(this.time);
2021-02-11 02:36:03 +01:00
}
//
return result;
}
/**
* Gets the year week.
*
* @return the year week
*/
public String getYearWeek()
{
String result;
if (this.time == null)
{
result = null;
}
else
{
2021-02-18 05:33:27 +01:00
result = TimeMarkUtils.yearWeekOf(this.time);
2021-02-11 02:36:03 +01:00
}
//
return result;
}
/**
* @return
*/
public boolean isBot()
{
boolean result;
2021-06-05 17:56:07 +02:00
if (StringsUtils.containsAnyIgnoreCase(this.userAgent.toString(), "bot", "crawler", "monitoring", "HeadlessChrome"))
2021-05-25 12:39:59 +02:00
{
result = true;
}
2021-06-05 00:36:46 +02:00
else if (StringUtils.startsWithAny(this.userAgent.toString(), "Apache-HttpClient/", "git/", "github-camo", "http.rb/", "Go-http-client", "GoModuleMirror/", "HotJava/", "Java/", "JGit/"))
2021-05-25 12:39:59 +02:00
{
result = true;
}
else if (StringUtils.equalsAnyIgnoreCase(this.userAgent.toString(), "-"))
{
result = true;
}
2021-05-25 12:39:59 +02:00
else
{
result = false;
}
2021-02-11 02:36:03 +01:00
//
return result;
}
/**
* Checks if is ipv4.
*
* @return true, if is ipv4
*/
public boolean isIPv4()
{
boolean result;
2021-06-05 17:56:07 +02:00
if (this.ip == null)
2021-02-11 02:36:03 +01:00
{
result = false;
}
else
{
2021-06-05 17:56:07 +02:00
result = this.ip.contains(".");
2021-02-11 02:36:03 +01:00
}
//
return result;
}
/**
* Checks if is ipv6.
*
* @return true, if is ipv6
*/
public boolean isIPv6()
{
boolean result;
2021-06-05 17:56:07 +02:00
if (this.ip == null)
2021-02-11 02:36:03 +01:00
{
result = false;
}
else
{
2021-06-05 17:56:07 +02:00
result = this.ip.contains(":");
2021-02-11 02:36:03 +01:00
}
//
return result;
}
public void setBodyBytesSent(final long bodyBytesSent)
{
this.bodyBytesSent = bodyBytesSent;
}
2021-06-05 17:56:07 +02:00
public void setIp(final String ip)
2021-02-11 02:36:03 +01:00
{
2021-06-05 17:56:07 +02:00
this.ip = ip;
2021-02-11 02:36:03 +01:00
}
2021-06-05 17:56:07 +02:00
public void setReferer(final String referer)
2021-02-11 02:36:03 +01:00
{
2021-06-05 17:56:07 +02:00
this.referer = referer;
2021-02-11 02:36:03 +01:00
}
public void setRemoteUser(final String remoteUser)
{
this.remoteUser = remoteUser;
}
public void setRequest(final String request)
{
this.request = request;
}
public void setStatus(final HttpStatus status)
{
this.status = status;
}
public void setTime(final LocalDateTime time)
{
this.time = time;
}
2021-02-18 05:33:27 +01:00
public void setUserAgent(final UserAgent userAgent)
{
this.userAgent = userAgent;
}
2021-02-11 02:36:03 +01:00
/**
* To string.
*
* @return the string
*/
@Override
public String toString()
{
String result;
StringList buffer = new StringList();
2021-06-05 17:56:07 +02:00
buffer.append("[ip=").append(this.ip).append("]");
2021-02-11 02:36:03 +01:00
buffer.append("[remoteUser=").append(this.remoteUser).append("]");
buffer.append("[time=").append(this.time).append("]");
buffer.append("[request=").append(this.request).append("]");
buffer.append("[status=").append(this.status.getCode()).append("]");
buffer.append("[bodyBytesSent=").append(this.bodyBytesSent).append("]");
buffer.append("[referer=").append(this.referer).append("]");
2021-02-18 05:33:27 +01:00
buffer.append("[userAgent=").append(this.userAgent).append("]");
2021-02-11 02:36:03 +01:00
result = buffer.toString();
//
return result;
}
}