Example usage for com.google.common.base Ascii equalsIgnoreCase

List of usage examples for com.google.common.base Ascii equalsIgnoreCase

Introduction

In this page you can find the example usage for com.google.common.base Ascii equalsIgnoreCase.

Prototype

@Beta
public static boolean equalsIgnoreCase(CharSequence s1, CharSequence s2) 

Source Link

Document

Indicates whether the contents of the given character sequences s1 and s2 are equal, ignoring the case of any ASCII alphabetic characters between 'a' and 'z' or 'A' and 'Z' inclusive.

Usage

From source file:com.google.devtools.build.lib.bazel.repository.downloader.HttpUtils.java

static boolean isProtocol(URL url, String protocol) {
    // An implementation should accept uppercase letters as equivalent to lowercase in scheme names
    // (e.g., allow "HTTP" as well as "http") for the sake of robustness. Quoth RFC3986  3.1
    return Ascii.equalsIgnoreCase(protocol, url.getProtocol());
}

From source file:org.sfs.util.NullSafeAscii.java

public static boolean equalsIgnoreCase(CharSequence s1, CharSequence s2) {
    if (s1 == s2) {
        return true;
    } else if (s1 == null || s2 == null) {
        return false;
    } else {/*from   www  .  j  a v  a2  s .  co  m*/
        return Ascii.equalsIgnoreCase(s1, s2);
    }
}

From source file:com.google.template.soy.basicfunctions.HtmlToText.java

public static String convert(String html) {
    StringBuilder text = new StringBuilder();
    int start = 0;
    String removingUntil = "";
    String wsPreservingUntil = "";
    Matcher matcher = TAG.matcher(html);
    while (matcher.find()) {
        int offset = matcher.start();
        String tag = matcher.group(1);
        if (removingUntil.isEmpty()) {
            String chunk = html.substring(start, offset);
            chunk = UnescapeUtils.unescapeHtml(chunk);
            if (wsPreservingUntil.isEmpty()) {
                chunk = WHITESPACE.matcher(chunk).replaceAll(" ");
                if (!TRAILING_NON_WHITESPACE.matcher(text).find()) {
                    chunk = LEADING_SPACE.matcher(chunk).replaceFirst("");
                }//from w w  w.  j  av  a2 s .  co m
            }
            text.append(chunk);
            if (tag != null) {
                if (REMOVING_TAGS.matcher(tag).matches()) {
                    removingUntil = '/' + tag;
                } else if (NEWLINE_TAGS.matcher(tag).matches()) {
                    text.append('\n');
                } else if (BLOCK_TAGS.matcher(tag).matches()) {
                    if (TRAILING_NON_NEWLINE.matcher(text).find()) {
                        text.append('\n');
                    }
                    if (WS_PRESERVING_TAGS.matcher(tag).matches()) {
                        wsPreservingUntil = '/' + tag;
                    } else if (Ascii.equalsIgnoreCase(tag, wsPreservingUntil)) {
                        wsPreservingUntil = "";
                    }
                } else if (TAB_TAGS.matcher(tag).matches()) {
                    text.append('\t');
                }
            }
        } else if (Ascii.equalsIgnoreCase(removingUntil, tag)) {
            removingUntil = "";
        }
        start = matcher.end();
    }
    return text.toString();
}

From source file:com.google.devtools.build.lib.bazel.repository.downloader.HttpConnector.java

URLConnection connect(URL originalUrl, ImmutableMap<String, String> requestHeaders) throws IOException {
    if (Thread.interrupted()) {
        throw new InterruptedIOException();
    }/*w w w .jav a  2  s.  c  o  m*/
    URL url = originalUrl;
    if (HttpUtils.isProtocol(url, "file")) {
        return url.openConnection();
    }
    List<Throwable> suppressions = new ArrayList<>();
    int retries = 0;
    int redirects = 0;
    int connectTimeout = MIN_CONNECT_TIMEOUT_MS;
    while (true) {
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) url.openConnection(proxyHelper.createProxyIfNeeded(url));
            boolean isAlreadyCompressed = COMPRESSED_EXTENSIONS.contains(HttpUtils.getExtension(url.getPath()))
                    || COMPRESSED_EXTENSIONS.contains(HttpUtils.getExtension(originalUrl.getPath()));
            for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
                if (isAlreadyCompressed && Ascii.equalsIgnoreCase(entry.getKey(), "Accept-Encoding")) {
                    // We're not going to ask for compression if we're downloading a file that already
                    // appears to be compressed.
                    continue;
                }
                connection.setRequestProperty(entry.getKey(), entry.getValue());
            }
            connection.setConnectTimeout(connectTimeout);
            // The read timeout is always large because it stays in effect after this method.
            connection.setReadTimeout(READ_TIMEOUT_MS);
            // Java tries to abstract HTTP error responses for us. We don't want that. So we're going
            // to try and undo any IOException that doesn't appepar to be a legitimate I/O exception.
            int code;
            try {
                connection.connect();
                code = connection.getResponseCode();
            } catch (FileNotFoundException ignored) {
                code = connection.getResponseCode();
            } catch (UnknownHostException e) {
                String message = "Unknown host: " + e.getMessage();
                eventHandler.handle(Event.progress(message));
                throw new UnrecoverableHttpException(message);
            } catch (IllegalArgumentException e) {
                // This will happen if the user does something like specify a port greater than 2^16-1.
                throw new UnrecoverableHttpException(e.getMessage());
            } catch (IOException e) {
                if (!e.getMessage().startsWith("Server returned")) {
                    throw e;
                }
                code = connection.getResponseCode();
            }
            // 206 means partial content and only happens if caller specified Range. See RFC7233  4.1.
            if (code == 200 || code == 206) {
                return connection;
            } else if (code == 301 || code == 302 || code == 307) {
                readAllBytesAndClose(connection.getInputStream());
                if (++redirects == MAX_REDIRECTS) {
                    eventHandler.handle(Event.progress("Redirect loop detected in " + originalUrl));
                    throw new UnrecoverableHttpException("Redirect loop detected");
                }
                url = HttpUtils.getLocation(connection);
                if (code == 301) {
                    originalUrl = url;
                }
            } else if (code == 403) {
                // jart@ has noticed BitBucket + Amazon AWS downloads frequently flake with this code.
                throw new IOException(describeHttpResponse(connection));
            } else if (code == 408) {
                // The 408 (Request Timeout) status code indicates that the server did not receive a
                // complete request message within the time that it was prepared to wait. Server SHOULD
                // send the "close" connection option (Section 6.1 of [RFC7230]) in the response, since
                // 408 implies that the server has decided to close the connection rather than continue
                // waiting.  If the client has an outstanding request in transit, the client MAY repeat
                // that request on a new connection. Quoth RFC7231  6.5.7
                throw new IOException(describeHttpResponse(connection));
            } else if (code < 500 // 4xx means client seems to have erred quoth RFC7231  6.5
                    || code == 501 // Server doesn't support function quoth RFC7231  6.6.2
                    || code == 502 // Host not configured on server cf. RFC7231  6.6.3
                    || code == 505) { // Server refuses to support version quoth RFC7231  6.6.6
                // This is a permanent error so we're not going to retry.
                readAllBytesAndClose(connection.getErrorStream());
                throw new UnrecoverableHttpException(describeHttpResponse(connection));
            } else {
                // However we will retry on some 5xx errors, particularly 500 and 503.
                throw new IOException(describeHttpResponse(connection));
            }
        } catch (UnrecoverableHttpException e) {
            throw e;
        } catch (IOException e) {
            if (connection != null) {
                // If we got here, it means we might not have consumed the entire payload of the
                // response, if any. So we're going to force this socket to disconnect and not be
                // reused. This is particularly important if multiple threads end up establishing
                // connections to multiple mirrors simultaneously for a large file. We don't want to
                // download that large file twice.
                connection.disconnect();
            }
            // We don't respect the Retry-After header (RFC7231  7.1.3) because it's rarely used and
            // tends to be too conservative when it is. We're already being good citizens by using
            // exponential backoff. Furthermore RFC law didn't use the magic word "MUST".
            int timeout = IntMath.pow(2, retries) * MIN_RETRY_DELAY_MS;
            if (e instanceof SocketTimeoutException) {
                eventHandler.handle(Event.progress("Timeout connecting to " + url));
                connectTimeout = Math.min(connectTimeout * 2, MAX_CONNECT_TIMEOUT_MS);
                // If we got connect timeout, we're already doing exponential backoff, so no point
                // in sleeping too.
                timeout = 1;
            } else if (e instanceof InterruptedIOException) {
                // Please note that SocketTimeoutException is a subtype of InterruptedIOException.
                throw e;
            }
            if (++retries == MAX_RETRIES) {
                if (!(e instanceof SocketTimeoutException)) {
                    eventHandler
                            .handle(Event.progress(format("Error connecting to %s: %s", url, e.getMessage())));
                }
                for (Throwable suppressed : suppressions) {
                    e.addSuppressed(suppressed);
                }
                throw e;
            }
            // Java 7 allows us to create a tree of all errors that led to the ultimate failure.
            suppressions.add(e);
            eventHandler.handle(
                    Event.progress(format("Failed to connect to %s trying again in %,dms", url, timeout)));
            url = originalUrl;
            try {
                sleeper.sleepMillis(timeout);
            } catch (InterruptedException translated) {
                throw new InterruptedIOException();
            }
        } catch (RuntimeException e) {
            if (connection != null) {
                connection.disconnect();
            }
            eventHandler.handle(Event.progress(format("Unknown error connecting to %s: %s", url, e)));
            throw e;
        }
    }
}

From source file:com.google.template.soy.passes.ContentSecurityPolicyNonceInjectionPass.java

private boolean isNonceableLink(HtmlOpenTagNode tag) {
    String relAttrValue = getStaticDirectAttributeValue(tag, "rel");
    if (relAttrValue == null) {
        return false;
    }//w ww . j a  va2  s.c o m
    if (Ascii.equalsIgnoreCase("import", relAttrValue)) {
        return true;
    }
    if (Ascii.equalsIgnoreCase("preload", relAttrValue)) {
        String asAttrValue = getStaticDirectAttributeValue(tag, "as");
        if (asAttrValue == null) {
            return false;
        }
        return Ascii.equalsIgnoreCase(asAttrValue, "script") || Ascii.equalsIgnoreCase(asAttrValue, "style");
    }
    return false;
}

From source file:com.linecorp.armeria.common.MediaTypeSet.java

private static boolean containsValue(String expectedValue, List<String> actualValues) {
    final int numActualValues = actualValues.size();
    for (int i = 0; i < numActualValues; i++) {
        if (Ascii.equalsIgnoreCase(expectedValue, actualValues.get(i))) {
            return true;
        }/*from   w  ww.j  a  v  a 2s.  c  o  m*/
    }
    return false;
}

From source file:com.google.template.soy.shared.internal.Sanitizers.java

/**
 * Given a snippet of HTML, returns a snippet that has the same text content but only whitelisted
 * tags./*from  w  w  w .  jav a  2s  .  co m*/
 *
 * @param safeTags the tags that are allowed in the output. A {@code null} white-list is the same
 *     as the empty white-list. If {@code null} or empty, then the output can be embedded in an
 *     attribute value. If the output is to be embedded in an attribute, {@code safeTags} should
 *     be {@code null}.
 * @param rawSpacesAllowed true if spaces are allowed in the output unescaped as is the case when
 *     the output is embedded in a regular text node, or in a quoted attribute.
 */
@VisibleForTesting
static String stripHtmlTags(String value, TagWhitelist safeTags, boolean rawSpacesAllowed) {
    EscapingConventions.CrossLanguageStringXform normalizer = rawSpacesAllowed
            ? EscapingConventions.NormalizeHtml.INSTANCE
            : EscapingConventions.NormalizeHtmlNospace.INSTANCE;

    Matcher matcher = EscapingConventions.HTML_TAG_CONTENT.matcher(value);
    if (!matcher.find()) {
        // Normalize so that the output can be embedded in an HTML attribute.
        return normalizer.escape(value);
    }

    StringBuilder out = new StringBuilder(value.length() - matcher.end() + matcher.start());
    Appendable normalizedOut = normalizer.escape(out);
    // We do some very simple tag balancing by dropping any close tags for unopened tags and at the
    // end emitting close tags for any still open tags.
    // This is sufficient (in HTML) to prevent embedded content with safe tags from breaking layout
    // when, for example, stripHtmlTags("</table>") is embedded in a page that uses tables for
    // formatting.
    List<String> openTags = null;
    int openListTagCount = 0;
    try {
        int pos = 0; // Such that value[:pos] has been sanitized onto out.
        do {
            int start = matcher.start();

            if (pos < start) {
                normalizedOut.append(value, pos, start);

                // More aggressively normalize ampersands at the end of a chunk so that
                //   "&<b>amp;</b>" -> "&amp;amp;" instead of "&amp;".
                if (value.charAt(start - 1) == '&') {
                    out.append("amp;");
                }
            }

            if (safeTags != null) {
                String tagName = matcher.group(1);
                if (tagName != null) {
                    // Use locale so that <I> works when the default locale is Turkish
                    tagName = Ascii.toLowerCase(tagName);
                    if (safeTags.isSafeTag(tagName)) {
                        boolean isClose = value.charAt(start + 1) == '/';
                        if (isClose) {
                            if (openTags != null) {
                                int lastIdx = openTags.lastIndexOf(tagName);
                                if (lastIdx >= 0) {
                                    // Close contained tags as well.
                                    // If we didn't, then we would convert "<ul><li></ul>" to "<ul><li></ul></li>"
                                    // which could lead to broken layout for embedding HTML that uses lists for
                                    // formatting.
                                    // This leads to observably different behavior for adoption-agency dependent
                                    // tag combinations like "<b><i>Foo</b> Bar</b>" but fails safe.
                                    // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#misnested-tags:-b-i-/b-/i
                                    List<String> tagsToClose = openTags.subList(lastIdx, openTags.size());
                                    for (String tagToClose : tagsToClose) {
                                        if (isListTag(tagToClose)) {
                                            openListTagCount--;
                                        }
                                    }
                                    closeTags(tagsToClose, out);
                                }
                            }
                        } else {
                            // Only allow whitelisted <li> through if it is nested in a parent <ol> or <ul>.
                            if (openListTagCount > 0 || !"li".equals(tagName)) {
                                if (isListTag(tagName)) {
                                    openListTagCount++;
                                }

                                // Emit beginning of the opening tag and tag name on the un-normalized channel.
                                out.append('<').append(tagName);

                                // Most attributes are dropped, but the dir attribute is preserved if it exists.
                                // The attribute matching could be made more generic if more attributes need to be
                                // whitelisted in the future. There are also probably other utilities in common to
                                // do such parsing of HTML, but this seemed simple enough and keeps with the
                                // current spirit of this function of doing custom parsing.
                                Matcher attributeMatcher = HTML_ATTRIBUTE_PATTERN.matcher(matcher.group());
                                while (attributeMatcher.find()) {
                                    String attributeName = attributeMatcher.group(1);
                                    if (!Strings.isNullOrEmpty(attributeName)
                                            && Ascii.equalsIgnoreCase(attributeName, "dir")) {
                                        String dir = attributeMatcher.group(2);
                                        if (!Strings.isNullOrEmpty(dir)) {
                                            // Strip quotes if the attribute value was quoted.
                                            if (dir.charAt(0) == '\'' || dir.charAt(0) == '"') {
                                                dir = dir.substring(1, dir.length() - 1);
                                            }
                                            dir = Ascii.toLowerCase(dir);
                                            if ("ltr".equals(dir) || "rtl".equals(dir) || "auto".equals(dir)) {
                                                out.append(" dir=\"").append(dir).append("\"");
                                            }
                                        }
                                        break;
                                    }
                                }

                                // Emit the end of the opening tag
                                out.append('>');

                                // Keep track of tags that need closing.
                                if (!HTML5_VOID_ELEMENTS.contains(tagName)) {
                                    if (openTags == null) {
                                        openTags = Lists.newArrayList();
                                    }
                                    openTags.add(tagName);
                                }
                            }
                        }
                    }
                }
            }
            pos = matcher.end();
        } while (matcher.find());
        normalizedOut.append(value, pos, value.length());
        // Emit close tags, so that safeTags("<table>") can't break the layout of embedding HTML that
        // uses tables for layout.
        if (openTags != null) {
            closeTags(openTags, out);
        }
    } catch (IOException ex) {
        // Writing to a StringBuilder should not throw.
        throw new AssertionError(ex);
    }
    return out.toString();
}