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