Search in sources :

Example 1 with MissingUserAgentException

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 "";
}
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 2 with MissingUserAgentException

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();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 3 with MissingUserAgentException

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>";
}
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 4 with MissingUserAgentException

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 "[{}]";
}
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)

Aggregations

ArrayList (java.util.ArrayList)4 UserAgent (nl.basjes.parse.useragent.UserAgent)4 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)4 UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 MutablePair (org.apache.commons.lang3.tuple.MutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1