Search in sources :

Example 6 with UserAgent

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

the class ParseUserAgent method exec.

@Override
public Tuple exec(Tuple tuple) throws IOException {
    initialize();
    String userAgentString = (String) tuple.get(0);
    UserAgent agent = analyzer.parse(userAgentString);
    Tuple result = TUPLE_FACTORY.newTuple();
    for (String fieldName : requestedFields) {
        result.append(agent.getValue(fieldName));
    }
    return result;
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent) Tuple(org.apache.pig.data.Tuple)

Example 7 with UserAgent

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

the class ApiXMLOutput method createOutput.

// -------------------------------------------------
private String createOutput(String userAgentString) {
    if (userAgentString == null) {
        throw new MissingUserAgentException();
    }
    ensureStartedForApis(OutputType.XML);
    if (userAgentAnalyzerIsAvailable()) {
        UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
        List<String> result = new ArrayList<>(2048);
        for (String input : splitPerFilledLine(userAgentString)) {
            UserAgent userAgent = userAgentAnalyzer.parse(input);
            result.add(userAgent.toXML(userAgent.getCleanedAvailableFieldNamesSorted()));
        }
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + String.join("\n", result);
    }
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Yauaa></Yauaa>";
}
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 8 with UserAgent

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

the class TestBuilder method testLoadAdditionalRules.

@Test
void testLoadAdditionalRules() {
    UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer.newBuilder().withField("DeviceClass").withoutCache().hideMatcherLoadStats().addResources("ExtraLoadedRule1.yaml").withField("ExtraValue2").withField("ExtraValue1").addResources("ExtraLoadedRule2.yaml").build();
    UserAgent parsedAgent = userAgentAnalyzer.parse("Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36");
    // The requested fields
    assertEquals("Phone", parsedAgent.getValue("DeviceClass"));
    assertEquals("One", parsedAgent.getValue("ExtraValue1"));
    assertEquals("Two", parsedAgent.getValue("ExtraValue2"));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Example 9 with UserAgent

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

the class TestBuilder method testWantedFieldNamesOne.

@Test
void testWantedFieldNamesOne() {
    UserAgentAnalyzer uaa = createWithWantedFieldNames(OPERATING_SYSTEM_NAME_VERSION);
    Set<String> expectedWantedFields = fields(DEVICE_CLASS, OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_VERSION, OPERATING_SYSTEM_NAME_VERSION, SET_ALL_FIELDS);
    assertEquals(expectedWantedFields, uaa.getWantedFieldNames());
    Set<String> expectedPossibleFields = fields(SYNTAX_ERROR, DEVICE_CLASS, OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_VERSION, OPERATING_SYSTEM_NAME_VERSION);
    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 10 with UserAgent

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

the class TestBuilder method testWantedFieldNamesAll.

@Test
void testWantedFieldNamesAll() {
    UserAgentAnalyzer uaa = createWithWantedFieldNames();
    assertNull(uaa.getWantedFieldNames());
    Set<String> expectedFields = fields(SYNTAX_ERROR, DEVICE_CLASS, DEVICE_BRAND, DEVICE_NAME, DEVICE_CPU, DEVICE_CPU_BITS, DEVICE_FIRMWARE_VERSION, DEVICE_VERSION, OPERATING_SYSTEM_CLASS, OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_VERSION, OPERATING_SYSTEM_VERSION_MAJOR, OPERATING_SYSTEM_NAME_VERSION, OPERATING_SYSTEM_NAME_VERSION_MAJOR, OPERATING_SYSTEM_VERSION_BUILD, LAYOUT_ENGINE_CLASS, LAYOUT_ENGINE_NAME, LAYOUT_ENGINE_VERSION, LAYOUT_ENGINE_VERSION_MAJOR, LAYOUT_ENGINE_NAME_VERSION, LAYOUT_ENGINE_NAME_VERSION_MAJOR, AGENT_CLASS, AGENT_NAME, AGENT_VERSION, AGENT_VERSION_MAJOR, AGENT_NAME_VERSION, AGENT_NAME_VERSION_MAJOR, AGENT_INFORMATION_EMAIL, AGENT_INFORMATION_URL, AGENT_LANGUAGE, AGENT_LANGUAGE_CODE, AGENT_NAME, AGENT_NAME_VERSION, AGENT_NAME_VERSION_MAJOR, AGENT_SECURITY, AGENT_UUID, AGENT_VERSION, AGENT_VERSION_MAJOR, HACKER_ATTACK_VECTOR, HACKER_TOOLKIT, LAYOUT_ENGINE_BUILD, LAYOUT_ENGINE_CLASS, LAYOUT_ENGINE_NAME, LAYOUT_ENGINE_NAME_VERSION, LAYOUT_ENGINE_NAME_VERSION_MAJOR, LAYOUT_ENGINE_VERSION, LAYOUT_ENGINE_VERSION_MAJOR, WEBVIEW_APP_NAME, WEBVIEW_APP_NAME_VERSION, WEBVIEW_APP_NAME_VERSION_MAJOR, WEBVIEW_APP_VERSION, WEBVIEW_APP_VERSION_MAJOR);
    // We only specify a subset of all fields to test for because otherwise each new field will fail this test.
    List<String> allPossibleFieldNames = uaa.getAllPossibleFieldNamesSorted();
    for (String expectedField : expectedFields) {
        assertTrue(allPossibleFieldNames.contains(expectedField), "Missing field: " + expectedField);
    }
    UserAgent userAgent = uaa.parse(TEST_UA);
    List<String> available = userAgent.getAvailableFieldNamesSorted();
    // We only specify a subset of all fields to test for because otherwise each new field will fail this test.
    for (String expectedField : expectedFields) {
        assertTrue(available.contains(expectedField), "Missing field: " + expectedField);
    }
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

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