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/>.
|
|
|
|
*/
|
2021-02-19 02:16:56 +01:00
|
|
|
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;
|
|
|
|
|
2021-02-19 02:16:56 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
private String remoteAddress;
|
|
|
|
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
|
|
|
{
|
|
|
|
this.remoteAddress = null;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getReferer()
|
|
|
|
{
|
|
|
|
return this.referer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRemoteAddress()
|
|
|
|
{
|
|
|
|
return this.remoteAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-25 12:39:59 +02:00
|
|
|
if (StringsUtils.containsAnyIgnoreCase(this.userAgent.toString(), "bot", "monitoring", "Apache-HttpClient"))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else if (StringUtils.startsWithAny(this.userAgent.toString(), "Java/", "HotJava/"))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (this.remoteAddress == null)
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = this.remoteAddress.contains(".");
|
|
|
|
}
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if is ipv6.
|
|
|
|
*
|
|
|
|
* @return true, if is ipv6
|
|
|
|
*/
|
|
|
|
public boolean isIPv6()
|
|
|
|
{
|
|
|
|
boolean result;
|
|
|
|
|
|
|
|
if (this.remoteAddress == null)
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = this.remoteAddress.contains(":");
|
|
|
|
}
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBodyBytesSent(final long bodyBytesSent)
|
|
|
|
{
|
|
|
|
this.bodyBytesSent = bodyBytesSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReferer(final String referer)
|
|
|
|
{
|
|
|
|
this.referer = referer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRemoteAddress(final String remoteAddress)
|
|
|
|
{
|
|
|
|
this.remoteAddress = remoteAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
buffer.append("[remoteAddress=").append(this.remoteAddress).append("]");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|