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