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

121 lines
2.8 KiB
Java

/*
* 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;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.util.LineIterator;
/**
* The Class HttpLogIterator.
*/
public class HttpLogIterator
{
private static Logger logger = LoggerFactory.getLogger(HttpLogIterator.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
private long errorCount;
private LineIterator iterator;
/**
* Instantiates a new http log iterator.
*
* @param source
* the source
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public HttpLogIterator(final File source) throws IOException
{
this.errorCount = 0;
this.iterator = new LineIterator(source);
}
/**
* Close.
*/
public void close()
{
this.iterator.close();
}
/**
* Gets the error count.
*
* @return the error count
*/
public long getErrorCount()
{
return this.errorCount;
}
/**
* Checks for next.
*
* @return true, if successful
* @throws IOException
*/
public boolean hasNext() throws IOException
{
boolean result;
result = this.iterator.hasNext();
//
return result;
}
/**
* Next.
*
* @return the http log
* @throws IOException
*/
public HttpAccessLog next() throws IOException
{
HttpAccessLog result;
try
{
String line = this.iterator.next();
result = HttpAccessLogAnalyzer.parseLog(line, HttpAccessLogAnalyzer.COMBINED_PATTERN);
}
catch (Exception exception)
{
exception.printStackTrace();
this.errorCount += 1;
if (this.iterator.hasNext())
{
result = next();
}
else
{
result = null;
}
}
//
return result;
}
}