Java Code Examples for com.networknt.utility.StringUtils#replaceOnce()

The following examples show how to use com.networknt.utility.StringUtils#replaceOnce() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Adds a trailing slash (/) right after the domain for URLs with no
 * path, before any fragment (#) or query string (?).</p>
 *
 * <p><b>Please Note:</b> Adding a trailing slash to URLs could
 * potentially break its semantic equivalence.</p>
 * <code>http://www.example.com &rarr;
 *       http://www.example.com/</code>
 * @return this instance
 * @since 1.12.0
 */
public URLNormalizer addDomainTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    if (StringUtils.isNotBlank(path)) {
        // there is a path so do nothing
        return this;
    }
    String urlRootAndPath = urlRoot + "/";
    url = StringUtils.replaceOnce(url, urlRoot, urlRootAndPath);
    return this;
}
 
Example 2
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Removes any trailing slash (/) from a URL, before fragment
 * (#) or query string (?).</p>
 *
 * <p><b>Please Note:</b> Removing trailing slashes form URLs
 * could potentially break their semantic equivalence.</p>
 * <code>http://www.example.com/alice/ &rarr;
 *       http://www.example.com/alice</code>
 * @return this instance
 * @since 1.11.0
 */
public URLNormalizer removeTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    String urlRootAndPath = urlRoot + path;

    if (path.endsWith("/")) {
        String newPath = StringUtils.removeEnd(path, "/");
        String newUrlRootAndPath = urlRoot + newPath;
        url = StringUtils.replaceOnce(
                url, urlRootAndPath, newUrlRootAndPath);
    }
    return this;
}
 
Example 3
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Removes duplicate slashes.  Two or more adjacent slash ("/")
 * characters will be converted into one.</p>
 * <code>http://www.example.com/foo//bar.html
 *       &rarr; http://www.example.com/foo/bar.html </code>
 * @return this instance
 */
public URLNormalizer removeDuplicateSlashes() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    String urlRootAndPath = urlRoot + path;
    String newPath = path.replaceAll("/{2,}", "/");
    String newUrlRootAndPath = urlRoot + newPath;
    url = StringUtils.replaceOnce(url, urlRootAndPath, newUrlRootAndPath);
    return this;
}
 
Example 4
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Removes "www." domain name prefix.</p>
 * <code>http://www.example.com/ &rarr; http://example.com/</code>
 * @return this instance
 */
public URLNormalizer removeWWW() {
    String host = toURL().getHost();
    String newHost = StringUtils.removeStartIgnoreCase(host, "www.");
    url = StringUtils.replaceOnce(url, host, newHost);
    return this;
}
 
Example 5
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Adds "www." domain name prefix.</p>
 * <code>http://example.com/ &rarr; http://www.example.com/</code>
 * @return this instance
 */
public URLNormalizer addWWW() {
    String host = toURL().getHost();
    if (!host.toLowerCase().startsWith("www.")) {
        url = StringUtils.replaceOnce(url, host, "www." + host);
    }
    return this;
}
 
Example 6
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Removes the unnecessary "." and ".." segments from the URL path.
 * </p>
 * <p><b>As of 2.3.0</b>, the algorithm used to remove the dot segments
 * is the one prescribed by
 * <a href="http://tools.ietf.org/html/rfc3986#section-5.2.4">RFC3986</a>.
 * </p>
 * <code>http://www.example.com/../a/b/../c/./d.html &rarr;
 *       http://www.example.com/a/c/d.html</code>
 * <p><b>Please Note:</b> URLs do not always represent a clean hierarchy
 * structure and the dots/double-dots may have a different signification
 * on some sites.  Removing them from a URL could potentially break
 * its semantic equivalence.</p>
 * @return this instance
 * @see URI#normalize()
 */
public URLNormalizer removeDotSegments() {
    String path = toURL().getPath().trim();

    // (Bulleted comments are from RFC3986, section-5.2.4)

    // 1.  The input buffer is initialized with the now-appended path
    //     components and the output buffer is initialized to the empty
    //     string.
    StringBuilder in = new StringBuilder(path);
    StringBuilder out = new StringBuilder();

    // 2.  While the input buffer is not empty, loop as follows:
    while (in.length() > 0) {

        // A.  If the input buffer begins with a prefix of "../" or "./",
        //     then remove that prefix from the input buffer; otherwise,
        if (startsWith(in, "../")) {
            deleteStart(in, "../");
        } else if (startsWith(in, "./")) {
            deleteStart(in, "./");
        }

        // B.  if the input buffer begins with a prefix of "/./" or "/.",
        //     where "." is a complete path segment, then replace that
        //     prefix with "/" in the input buffer; otherwise,
        else if (startsWith(in, "/./")) {
            replaceStart(in, "/./", "/");
        } else if (equalStrings(in, "/.")) {
            replaceStart(in, "/.", "/");
        }

        // C.  if the input buffer begins with a prefix of "/../" or "/..",
        //     where ".." is a complete path segment, then replace that
        //     prefix with "/" in the input buffer and remove the last
        //     segment and its preceding "/" (if any) from the output
        //     buffer; otherwise,
        else if (startsWith(in, "/../")) {
            replaceStart(in, "/../", "/");
            removeLastSegment(out);
        } else if (equalStrings(in, "/..")) {
            replaceStart(in, "/..", "/");
            removeLastSegment(out);
        }

        // D.  if the input buffer consists only of "." or "..", then remove
        //      that from the input buffer; otherwise,
        else if (equalStrings(in, "..")) {
            deleteStart(in, "..");
        } else if (equalStrings(in, ".")) {
            deleteStart(in, ".");
        }

        // E.  move the first path segment in the input buffer to the end of
        //     the output buffer, including the initial "/" character (if
        //     any) and any subsequent characters up to, but not including,
        //     the next "/" character or the end of the input buffer.
        else {
            int nextSlashIndex = in.indexOf("/", 1);
            if (nextSlashIndex > -1) {
                out.append(in.substring(0, nextSlashIndex));
                in.delete(0, nextSlashIndex);
            } else {
                out.append(in);
                in.setLength(0);
            }
        }
    }

    // 3.  Finally, the output buffer is returned as the result of
    //     remove_dot_segments.
    url = StringUtils.replaceOnce(url, path, out.toString());
    return this;
}
 
Example 7
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Adds a trailing slash (/) to a URL ending with a directory.  A URL is
 * considered to end with a directory if the last path segment,
 * before fragment (#) or query string (?), does not contain a dot,
 * typically representing an extension.</p>
 *
 * <p><b>Please Note:</b> URLs do not always denote a directory structure
 * and many URLs can qualify to this method without truly representing a
 * directory. Adding a trailing slash to these URLs could potentially break
 * its semantic equivalence.</p>
 * <code>http://www.example.com/alice &rarr;
 *       http://www.example.com/alice/</code>
 * @return this instance
 * @since 1.11.0 (renamed from "addTrailingSlash")
 */
public URLNormalizer addDirectoryTrailingSlash() {
    String urlRoot = HttpURL.getRoot(url);
    String path = toURL().getPath();
    String urlRootAndPath = urlRoot + path;

    String name = StringUtils.substringAfterLast(path, "/");
    if (StringUtils.isNotBlank(name) && !name.contains(".")) {
        String newPath = path + "/";
        String newUrlRootAndPath = urlRoot + newPath;
        url = StringUtils.replaceOnce(
                url, urlRootAndPath, newUrlRootAndPath);
    }
    return this;
}
 
Example 8
Source File: URLNormalizer.java    From light-4j with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Removes directory index files.  They are often not needed in URLs.</p>
 * <code>http://www.example.com/a/index.html &rarr;
 *       http://www.example.com/a/</code>
 * <p>Index files must be the last URL path segment to be considered.
 * The following are considered index files:</p>
 * <ul>
 *   <li>index.html</li>
 *   <li>index.htm</li>
 *   <li>index.shtml</li>
 *   <li>index.php</li>
 *   <li>default.html</li>
 *   <li>default.htm</li>
 *   <li>home.html</li>
 *   <li>home.htm</li>
 *   <li>index.php5</li>
 *   <li>index.php4</li>
 *   <li>index.php3</li>
 *   <li>index.cgi</li>
 *   <li>placeholder.html</li>
 *   <li>default.asp</li>
 * </ul>
 * <p><b>Please Note:</b> There are no guarantees a URL without its
 * index files will be semantically equivalent, or even be valid.</p>
 * @return this instance
 */
public URLNormalizer removeDirectoryIndex() {
    String path = toURL().getPath();
    if (PATTERN_PATH_LAST_SEGMENT.matcher(path).matches()) {
        url = StringUtils.replaceOnce(
                url, path, StringUtils.substringBeforeLast(path, "/") + "/");
    }
    return this;
}