Search in sources :

Example 1 with ParsedField

use of nl.basjes.parse.core.ParsedField in project logparser by nielsbasjes.

the class AbstractGeoIPDissector method dissect.

// --------------------------------------------
@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(INPUT_TYPE, inputname);
    String fieldValue = field.getValue().getString();
    if (fieldValue == null || fieldValue.isEmpty()) {
        // Nothing to do here
        return;
    }
    InetAddress ipAddress;
    try {
        ipAddress = InetAddress.getByName(fieldValue);
    } catch (UnknownHostException e) {
        return;
    }
    dissect(parsable, inputname, ipAddress);
}
Also used : UnknownHostException(java.net.UnknownHostException) ParsedField(nl.basjes.parse.core.ParsedField) InetAddress(java.net.InetAddress)

Example 2 with ParsedField

use of nl.basjes.parse.core.ParsedField in project logparser by nielsbasjes.

the class UpstreamListDissector method dissect.

@Override
public void dissect(Parsable<?> parsable, String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(inputType, inputname);
    String fieldValue = field.getValue().getString();
    if (fieldValue == null || fieldValue.isEmpty()) {
        // Nothing to do.
        return;
    }
    String[] servers = fieldValue.split(", ");
    int serverNr = 0;
    for (String server : servers) {
        String[] parts = server.split(": ");
        if (parts.length == 1) {
            parsable.addDissection(inputname, outputOriginalType, serverNr + OUTPUT_ORIGINAL_NAME, parts[0].trim());
            parsable.addDissection(inputname, outputRedirectedType, serverNr + OUTPUT_REDIRECTED_NAME, parts[0].trim());
        } else {
            parsable.addDissection(inputname, outputOriginalType, serverNr + OUTPUT_ORIGINAL_NAME, parts[0].trim());
            parsable.addDissection(inputname, outputRedirectedType, serverNr + OUTPUT_REDIRECTED_NAME, parts[1].trim());
        }
        serverNr++;
    }
}
Also used : ParsedField(nl.basjes.parse.core.ParsedField)

Example 3 with ParsedField

use of nl.basjes.parse.core.ParsedField in project logparser by nielsbasjes.

the class TokenFormatDissector method dissect.

@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    if (!isUsable) {
        throw new DissectionFailure("Dissector in unusable state");
    }
    final ParsedField line = parsable.getParsableField(inputType, inputname);
    // Now we create a matcher for this line
    final Matcher matcher = logFormatPattern.matcher(line.getValue().getString());
    // Is it all as expected?
    final boolean matches = matcher.find();
    if (matches) {
        for (int i = 1; i <= matcher.groupCount(); i++) {
            String matchedStr = matcher.group(i);
            Token token = logFormatUsedTokens.get(i - 1);
            for (TokenOutputField tokenOutputField : token.getOutputFields()) {
                final String matchedName = tokenOutputField.getName();
                final String matchedType = tokenOutputField.getType();
                parsable.addDissection(inputname, matchedType, matchedName, decodeExtractedValue(matchedName, matchedStr));
            }
        }
    } else {
        throw new DissectionFailure("The input line does not match the specified log format." + "Line     : " + line.getValue() + "\n" + "LogFormat: " + logFormat + "\n" + "RegEx    : " + logFormatRegEx);
    }
}
Also used : Matcher(java.util.regex.Matcher) ParsedField(nl.basjes.parse.core.ParsedField) DissectionFailure(nl.basjes.parse.core.exceptions.DissectionFailure)

Example 4 with ParsedField

use of nl.basjes.parse.core.ParsedField in project logparser by nielsbasjes.

the class RequestCookieListDissector method dissect.

@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(INPUT_TYPE, inputname);
    final String fieldValue = field.getValue().getString();
    if (fieldValue == null || fieldValue.isEmpty()) {
        // Nothing to do here
        return;
    }
    String[] allValues = fieldSeparatorPattern.split(fieldValue);
    for (String value : allValues) {
        int equalPos = value.indexOf('=');
        if (equalPos == -1) {
            if (!"".equals(value)) {
                // Just a name, no value
                String theName = value.trim().toLowerCase();
                if (wantAllCookies || requestedCookies.contains(theName)) {
                    parsable.addDissection(inputname, "HTTP.COOKIE", theName, "");
                }
            }
        } else {
            String theName = value.substring(0, equalPos).trim().toLowerCase();
            if (wantAllCookies || requestedCookies.contains(theName)) {
                String theValue = value.substring(equalPos + 1, value.length()).trim();
                try {
                    parsable.addDissection(inputname, "HTTP.COOKIE", theName, Utils.resilientUrlDecode(theValue));
                } catch (IllegalArgumentException e) {
                    // This usually means that there was invalid encoding in the line
                    throw new DissectionFailure(e.getMessage());
                }
            }
        }
    }
}
Also used : ParsedField(nl.basjes.parse.core.ParsedField) DissectionFailure(nl.basjes.parse.core.exceptions.DissectionFailure)

Example 5 with ParsedField

use of nl.basjes.parse.core.ParsedField in project logparser by nielsbasjes.

the class ResponseSetCookieListDissector method dissect.

@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(INPUT_TYPE, inputname);
    final String fieldValue = field.getValue().getString();
    if (fieldValue == null || fieldValue.isEmpty()) {
        // Nothing to do here
        return;
    }
    // This input is a ', ' separated list.
    // But the expires field can contain a ','
    // and HttpCookie.parse(...) doesn't always work :(
    String[] parts = fieldValue.split(SPLIT_BY);
    String previous = "";
    for (String part : parts) {
        int expiresIndex = part.toLowerCase().indexOf("expires=");
        if (expiresIndex != -1) {
            if (part.length() - minimalExpiresLength < expiresIndex) {
                previous = part;
                continue;
            }
        }
        String value = part;
        if (!previous.isEmpty()) {
            value = previous + SPLIT_BY + part;
            previous = "";
        }
        List<HttpCookie> cookies = HttpCookie.parse(value);
        for (HttpCookie cookie : cookies) {
            cookie.setVersion(1);
            String cookieName = cookie.getName().toLowerCase();
            if (wantAllCookies || requestedCookies.contains(cookieName)) {
                parsable.addDissection(inputname, "HTTP.SETCOOKIE", cookieName, value);
            }
        }
    }
}
Also used : ParsedField(nl.basjes.parse.core.ParsedField) HttpCookie(java.net.HttpCookie)

Aggregations

ParsedField (nl.basjes.parse.core.ParsedField)16 DissectionFailure (nl.basjes.parse.core.exceptions.DissectionFailure)4 Matcher (java.util.regex.Matcher)3 HttpCookie (java.net.HttpCookie)1 InetAddress (java.net.InetAddress)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 UserAgent (nl.basjes.parse.useragent.UserAgent)1