Java Code Examples for java.net.URI.normalize()

The following are Jave code examples for showing how to use normalize() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mobile-store   File: ManageReposActivity.java   View Source Code Vote up 6 votes
/**
 * Some basic sanitization of URLs, so that two URLs which have the same semantic meaning
 * are represented by the exact same string by F-Droid. This will help to make sure that,
 * e.g. "http://10.0.1.50" and "http://10.0.1.50/" are not two different repositories.
 * <p>
 * Currently it normalizes the path so that "/./" are removed and "test/../" is collapsed.
 * This is done using {@link URI#normalize()}. It also removes multiple consecutive forward
 * slashes in the path and replaces them with one. Finally, it removes trailing slashes.
 */
private String normalizeUrl(String urlString) throws URISyntaxException {
    if (urlString == null) {
        return null;
    }
    URI uri = new URI(urlString);
    if (!uri.isAbsolute()) {
        throw new URISyntaxException(urlString, "Must provide an absolute URI for repositories");
    }

    uri = uri.normalize();
    String path = uri.getPath();
    if (path != null) {
        path = path.replaceAll("//*/", "/"); // Collapse multiple forward slashes into 1.
        if (path.length() > 0 && path.charAt(path.length() - 1) == '/') {
            path = path.substring(0, path.length() - 1);
        }
    }

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
            path, uri.getQuery(), uri.getFragment()).toString();
}
 
Example 2
Project: letv   File: jr.java   View Source Code Vote up 6 votes
public static String b(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    URI i = i(str);
    if (i == null) {
        return str;
    }
    i = i.normalize();
    if (i.isOpaque()) {
        return str;
    }
    i = a(i.getScheme(), i.getAuthority(), "/", null, null);
    if (i != null) {
        return i.toString();
    }
    return str;
}
 
Example 3
Project: letv   File: jr.java   View Source Code Vote up 6 votes
public static String c(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    URI i = i(str);
    if (i == null) {
        return str;
    }
    i = i.normalize();
    if (i.isOpaque()) {
        return str;
    }
    i = URIUtils.resolve(i, "./");
    if (i != null) {
        return i.toString();
    }
    return str;
}
 
Example 4
Project: rs-aggregator   File: NormURI.java   View Source Code Vote up 6 votes
public static Optional<URI> normalize(@Nonnull URI uri) {
  URI n = uri.normalize();
  String host = stripWWW(n);
  host = host == null ? null : host.toLowerCase();
  String path = normalizePath(n).getPath();
  try {
    if (n.isOpaque()) {
      return Optional.of(new URI(n.getScheme(), n.getSchemeSpecificPart(), n.getFragment()));
    } else {
      return Optional.of(new URI(n.getScheme(), null, host, n.getPort(), path, null, null));
    }
  } catch (URISyntaxException e) {
    // unlikely deviation
    throw new RuntimeException(e);
  }
}
 
Example 5
Project: incubator-netbeans   File: FileBasedURLMapperTest.java   View Source Code Vote up 5 votes
public void testSlashifyUNCPath() throws Exception {
    String unc = "\\\\192.168.0.201\\data\\services\\web\\com_resource\\";
    URI uri = FileBasedURLMapper.toURI(unc, true, '\\');
    final URI norm = uri.normalize();

    assertTrue("Is normalized: " + uri + " == " + norm, uri.equals(norm));
    assertEquals("192.168.0.201", uri.getHost());
    assertEquals("/data/services/web/com_resource/", uri.getPath());
}
 
Example 6
Project: elasticsearch_my   File: URIPattern.java   View Source Code Vote up 5 votes
public static boolean match(URIPattern[] patterns, URI uri) {
    URI normalized = uri.normalize();
    for (URIPattern pattern : patterns) {
        if (pattern.matchNormalized(normalized)) {
            return true;
        }
    }
    return false;
}
 
Example 7
Project: Elasticsearch   File: URIPattern.java   View Source Code Vote up 5 votes
public static boolean match(URIPattern[] patterns, URI uri) {
    URI normalized = uri.normalize();
    for (URIPattern pattern : patterns) {
        if (pattern.matchNormalized(normalized)) {
            return true;
        }
    }
    return false;
}
 
Example 8
Project: hadoop-oss   File: Path.java   View Source Code Vote up 4 votes
/**
 * Construct a path from a URI
 */
public Path(URI aUri) {
  uri = aUri.normalize();
}
 
Example 9
Project: hadoop   File: Path.java   View Source Code Vote up 4 votes
/**
 * Construct a path from a URI
 */
public Path(URI aUri) {
  uri = aUri.normalize();
}
 
Example 10
Project: webmate-sdk-java   File: WebmateEnvironment.java   View Source Code Vote up 4 votes
public static WebmateEnvironment create(URI baseUri) {
    return new WebmateEnvironment(baseUri.normalize());
}