use of nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException 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.servlet.exceptions.MissingUserAgentException 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.servlet.exceptions.MissingUserAgentException 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.servlet.exceptions.MissingUserAgentException in project yauaa by nielsbasjes.
the class ApiJsonOutput 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)) {
UserAgent userAgent = userAgentAnalyzer.parse(input);
result.add(userAgent.toJson(userAgent.getCleanedAvailableFieldNamesSorted()));
}
return "[" + String.join(",\n", result) + "]";
}
return "[{}]";
}
Aggregations