Search in sources :

Example 1 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class ApiYamlOutput method createOutput.

// -------------------------------------------------
private String createOutput(String userAgentString) {
    if (userAgentString == null) {
        throw new MissingUserAgentException();
    }
    ensureStartedForApis(OutputType.JSON);
    if (userAgentAnalyzerIsAvailable()) {
        UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
        List<String> result = new ArrayList<>(2048);
        for (String input : splitPerFilledLine(userAgentString)) {
            if (input.startsWith("#")) {
                result.add(input);
            } else {
                UserAgent userAgent = userAgentAnalyzer.parse(input);
                result.add(userAgent.toYamlTestCase(userAgent.getCleanedAvailableFieldNamesSorted()));
            }
        }
        return String.join("\n", result);
    }
    return "";
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ArrayList(java.util.ArrayList) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)

Example 2 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class HumanHtml method doHTML.

// ===========================================
private String doHTML(String userAgentString) {
    long start = System.nanoTime();
    long startParse = 0;
    long stopParse = 0;
    if (userAgentString == null) {
        throw new MissingUserAgentException();
    }
    StringBuilder sb = new StringBuilder(4096);
    try {
        htmlHead(sb);
        if (userAgentAnalyzerIsAvailable()) {
            startParse = System.nanoTime();
            List<UserAgent> userAgents = new ArrayList<>();
            final List<String> userAgentStrings = splitPerFilledLine(userAgentString);
            userAgentStrings.forEach(ua -> userAgents.add(getUserAgentAnalyzer().parse(ua)));
            stopParse = System.nanoTime();
            for (UserAgent userAgent : userAgents) {
                sb.append("<hr/>");
                sb.append("<h2 class=\"title\">The UserAgent</h2>");
                sb.append("<p class=\"input\">").append(escapeHtml4(userAgent.getUserAgentString())).append("</p>");
                sb.append("<h2 class=\"title\">The analysis result</h2>");
                List<String> tags = getTags(userAgent);
                sb.append("<p class=\"tags\">").append("DeviceClass : ").append(userAgent.getValue(DEVICE_CLASS)).append("<br/>").append(String.join(" - ", tags)).append("</p>");
                sb.append("<table id=\"result\">");
                sb.append("<tr><th colspan=2>Field</th><th>Value</th></tr>");
                Map<String, Integer> fieldGroupCounts = new HashMap<>();
                List<Pair<String, Pair<String, String>>> fields = new ArrayList<>(32);
                for (String fieldname : userAgent.getAvailableFieldNamesSorted()) {
                    Pair<String, String> split = prefixSplitter(fieldname);
                    if (!STANDARD_FIELDS.contains(fieldname)) {
                        if (userAgent.get(fieldname).isDefaultValue()) {
                            // Skip the "non standard" fields that do not have a relevant value.
                            continue;
                        }
                    }
                    fields.add(new ImmutablePair<>(fieldname, split));
                    Integer count = fieldGroupCounts.get(split.getLeft());
                    if (count == null) {
                        count = 1;
                    } else {
                        count++;
                    }
                    fieldGroupCounts.put(split.getLeft(), count);
                }
                String currentGroup = "";
                for (Pair<String, Pair<String, String>> field : fields) {
                    String fieldname = field.getLeft();
                    String groupName = field.getRight().getLeft();
                    String fieldLabel = field.getRight().getRight();
                    sb.append("<tr>");
                    if (!currentGroup.equals(groupName)) {
                        currentGroup = groupName;
                        sb.append("<td rowspan=").append(fieldGroupCounts.get(currentGroup)).append("><b><u>").append(escapeHtml4(currentGroup)).append("</u></b></td>");
                    }
                    sb.append("<td>").append(camelStretcher(escapeHtml4(fieldLabel))).append("</td>").append("<td>").append(escapeHtml4(userAgent.getValue(fieldname))).append("</td>").append("</tr>");
                }
                sb.append("</table>");
            }
            documentationBlock(sb, userAgents);
            sb.append("<hr/>");
            sb.append("<form class=\"logobar tryyourown\" action=\"\" method=\"post\">");
            sb.append("<label for=\"useragent\">Manual testing of a useragent:</label>");
            sb.append("<textarea id=\"useragent\" name=\"useragent\" maxlength=\"2000\" rows=\"4\" " + "placeholder=\"Paste the useragent you want to test...\">").append("</textarea>");
            sb.append("<input class=\"testButton\" type=\"submit\" value=\"Analyze\">");
            sb.append("</form>");
            sb.append("<hr/>");
        } else {
            stillStartingUp(sb);
        }
    } catch (Exception e) {
        sb.append("<div class=\"failureBorder\">");
        sb.append("<p class=\"failureContent\">An exception occurred during parsing</p>");
        sb.append("<p class=\"failureContent\">Exception: ").append(e.getClass().getSimpleName()).append("</p>");
        sb.append("<p class=\"failureContent\">Message: ").append(e.getMessage().replace("\\n", "<br/>")).append("</p>");
        sb.append("</div>");
    } finally {
        long stop = System.nanoTime();
        double pageMilliseconds = (stop - start) / 1000000.0;
        double parseMilliseconds = (stopParse - startParse) / 1000000.0;
        sb.append("<p class=\"speed\">Building this page took ").append(String.format(Locale.ENGLISH, "%3.3f", pageMilliseconds)).append(" ms.</p>");
        if (parseMilliseconds > 0) {
            sb.append("<p class=\"speed\">Parsing took ").append(String.format(Locale.ENGLISH, "%3.3f", parseMilliseconds)).append(" ms.</p>");
        }
        htmlFooter(sb);
    }
    return sb.toString();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 3 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class YauaaProcessor method execute.

@Override
public IngestDocument execute(IngestDocument ingestDocument) {
    String content = ingestDocument.getFieldValue(field, String.class);
    UserAgent userAgent = uaa.parse(content);
    Map<String, String> resultMap = userAgent.toMap();
    resultMap.remove(USERAGENT_FIELDNAME);
    ingestDocument.setFieldValue(targetField, resultMap);
    return ingestDocument;
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent)

Example 4 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class Yauaa method filter.

@Override
public Collection<Event> filter(Collection<Event> events, FilterMatchListener filterMatchListener) {
    for (Event event : events) {
        Object rawField = event.getField(sourceField);
        if (rawField instanceof String) {
            String userAgentString = (String) rawField;
            UserAgent agent = userAgentAnalyzer.parse(userAgentString);
            for (String fieldName : requestedFieldNames) {
                event.setField(outputFields.get(fieldName), agent.getValue(fieldName));
            }
        }
    }
    return events;
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent) Event(co.elastic.logstash.api.Event)

Example 5 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class ParseUserAgent method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    // NOSONAR: Explicitly name the exception
    FlowFile flowFile = session.get();
    String userAgentString = flowFile.getAttribute(USERAGENTSTRING_ATTRIBUTENAME);
    if (userAgentString == null) {
        session.transfer(flowFile, MISSING);
    } else {
        UserAgent userAgent = uaa.parse(userAgentString);
        for (String fieldName : extractFieldNames) {
            String fieldValue = userAgent.getValue(fieldName);
            flowFile = session.putAttribute(flowFile, ATTRIBUTE_PREFIX + fieldName, fieldValue);
        }
        session.transfer(flowFile, SUCCESS);
    }
    session.commitAsync();
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) UserAgent(nl.basjes.parse.useragent.UserAgent)

Aggregations

UserAgent (nl.basjes.parse.useragent.UserAgent)30 UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)4 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)3 AgentField (nl.basjes.parse.useragent.AgentField)2 Matcher (nl.basjes.parse.useragent.analyze.Matcher)2 Event (co.elastic.logstash.api.Event)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 Description (io.trino.spi.function.Description)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 SqlType (io.trino.spi.function.SqlType)1 MapType (io.trino.spi.type.MapType)1 TypeOperators (io.trino.spi.type.TypeOperators)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ParsedField (nl.basjes.parse.core.ParsedField)1