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;
}
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>";
}
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"));
}
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()));
}
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);
}
}
Aggregations