Search in sources :

Example 21 with UserAgent

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

the class TestBuilder method testWantedFieldNamesOneAndCalc.

@Test
void testWantedFieldNamesOneAndCalc() {
    UserAgentAnalyzer uaa = createWithWantedFieldNames(AGENT_NAME_VERSION_MAJOR);
    Set<String> expectedWantedFields = fields(DEVICE_CLASS, DEVICE_BRAND, AGENT_INFORMATION_EMAIL, AGENT_INFORMATION_URL, AGENT_NAME, AGENT_VERSION, AGENT_VERSION_MAJOR, AGENT_NAME_VERSION_MAJOR, SET_ALL_FIELDS);
    assertEquals(expectedWantedFields, uaa.getWantedFieldNames());
    Set<String> expectedPossibleFields = fields(SYNTAX_ERROR, DEVICE_CLASS, DEVICE_BRAND, AGENT_INFORMATION_EMAIL, AGENT_INFORMATION_URL, AGENT_NAME, AGENT_VERSION, AGENT_VERSION_MAJOR, AGENT_NAME_VERSION_MAJOR);
    assertEquals(expectedPossibleFields, uaa.getAllPossibleFieldNames());
    UserAgent userAgent = uaa.parse(TEST_UA);
    assertEquals(expectedPossibleFields, new TreeSet<>(userAgent.getAvailableFieldNamesSorted()));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Example 22 with UserAgent

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

the class TestBuilder method testLoadOnlyCompanyCustomFormatRules.

@Test
void testLoadOnlyCompanyCustomFormatRules() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().withoutCache().hideMatcherLoadStats().dropDefaultResources().addResources("CompanyInternalUserAgents.yaml").withFields("ApplicationName", "ApplicationVersion").withFields(Arrays.asList("ApplicationInstance", "ApplicationGitCommit")).withField("ServerName").build();
    UserAgent parsedAgent = userAgentAnalyzer.parse("TestApplication/1.2.3 (node123.datacenter.example.nl; 1234; d71922715c2bfe29343644b14a4731bf5690e66e)");
    // The requested fields
    assertEquals("TestApplication", parsedAgent.getValue("ApplicationName"));
    assertEquals("1.2.3", parsedAgent.getValue("ApplicationVersion"));
    assertEquals("1234", parsedAgent.getValue("ApplicationInstance"));
    assertEquals("d71922715c2bfe29343644b14a4731bf5690e66e", parsedAgent.getValue("ApplicationGitCommit"));
    assertEquals("node123.datacenter.example.nl", parsedAgent.getValue("ServerName"));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Example 23 with UserAgent

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

the class TestUserAgentFlattening method validateUserAgent.

private void validateUserAgent(String useragent, String... requiredValues) {
    boolean developmentMode = requiredValues.length == 0;
    if (developmentMode) {
        LOG.info("Developing {}", useragent);
    } else {
        LOG.info("Validating {}", useragent);
    }
    StringBuilder sb = new StringBuilder(2048);
    sb.append('\n');
    sb.append("|====================================== \n");
    sb.append("| ").append(useragent).append('\n');
    sb.append("|-------------------------------------- \n");
    GetAllPathsAnalyzer analyzer = UserAgentAnalyzer.getAllPathsAnalyzer(useragent);
    UserAgent parsedUseragent = analyzer.getResult();
    if (parsedUseragent.hasAmbiguity()) {
        sb.append("| Ambiguity \n");
    }
    if (parsedUseragent.hasSyntaxError()) {
        sb.append("| Syntax Error \n");
    }
    List<String> paths = analyzer.getValues();
    boolean ok = true;
    for (String value : requiredValues) {
        if (paths.contains(value)) {
            sb.append("|             : ").append(value).append('\n');
        } else {
            sb.append("| Missing --> : ").append(value).append('\n');
            ok = false;
        }
    }
    if (requiredValues.length == 0 || !ok) {
        sb.append("|-------------------------------------- \n");
        for (String value : paths) {
            if (value.contains("=")) {
                sb.append("      ,\"").append(value.replaceAll("\\\"", "\\\\\"")).append("\"\n");
            }
        }
        sb.append("|====================================== \n");
    }
    if (developmentMode) {
        LOG.info(sb.toString());
        return;
    }
    if (!ok) {
        LOG.error(sb.toString());
        fail("Not everything was found");
    }
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent) GetAllPathsAnalyzer(nl.basjes.parse.useragent.AbstractUserAgentAnalyzerDirect.GetAllPathsAnalyzer)

Example 24 with UserAgent

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

the class UserAgentDissector method dissect.

@Override
public void dissect(Parsable<?> parsable, String inputname) throws DissectionFailure {
    final ParsedField agentField = parsable.getParsableField(INPUT_TYPE, inputname);
    String userAgentString = agentField.getValue().getString();
    if (userAgentString == null) {
        // Weird, but it happens
        return;
    }
    UserAgent agent = userAgentAnalyzer.parse(userAgentString);
    for (String fieldName : requestedFieldNames) {
        parsable.addDissection(inputname, getFieldOutputType(fieldName), fieldNameToDissectionName(fieldName), agent.getValue(fieldName));
    }
}
Also used : ParsedField(nl.basjes.parse.core.ParsedField) UserAgent(nl.basjes.parse.useragent.UserAgent)

Example 25 with UserAgent

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

the class RunBenchmarks method doTest.

private void doTest(Triple<Counter, String, String> test) {
    long start = System.nanoTime();
    UserAgent agent = uaa.parse(test.getRight());
    long stop = System.nanoTime();
    if (agent.getValue("DeviceClass") == null) {
        LOG.error("This should not happen, yet we keep this test to avoid the parse being optimized out.");
    }
    test.getLeft().increment((double) (stop - start));
}
Also used : 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