/* * Copyright (C) 2021 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.metrics.http; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.metrics.TimeMarkUtils; import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringsUtils; /** * The Class HttpAccessLog. */ public class HttpAccessLog { private static Logger logger = LoggerFactory.getLogger(HttpAccessLog.class); 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; private UserAgent userAgent; /** * Instantiates a new http access log. */ public HttpAccessLog() { this.remoteAddress = null; this.remoteUser = null; this.time = null; this.request = null; this.status = null; this.bodyBytesSent = 0; this.referer = null; this.userAgent = null; } 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; } public UserAgent getUserAgent() { return this.userAgent; } /** * Gets the year. * * @return the year */ public String getYear() { String result; if (this.time == null) { result = null; } else { result = TimeMarkUtils.yearOf(this.time); } // return result; } /** * Gets the year month. * * @return the year month */ public String getYearMonth() { String result; if (this.time == null) { result = null; } else { result = TimeMarkUtils.yearMonthOf(this.time); } // return result; } /** * Gets the year week. * * @return the year week */ public String getYearWeek() { String result; if (this.time == null) { result = null; } else { result = TimeMarkUtils.yearWeekOf(this.time); } // return result; } /** * @return */ public boolean isBot() { boolean result; 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; } // 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; } public void setUserAgent(final UserAgent userAgent) { this.userAgent = userAgent; } /** * 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("]"); buffer.append("[userAgent=").append(this.userAgent).append("]"); result = buffer.toString(); // return result; } }