Example usage for java.net URI getScheme

List of usage examples for java.net URI getScheme

Introduction

In this page you can find the example usage for java.net URI getScheme.

Prototype

public String getScheme() 

Source Link

Document

Returns the scheme component of this URI.

Usage

From source file:org.projecthdata.social.api.connect.HDataServiceProvider.java

private static StringBuilder getBaseUrl(String ehrUrl) {
    URI uri = URIBuilder.fromUri(ehrUrl).build();
    StringBuilder builder = new StringBuilder();
    builder.append(uri.getScheme()).append("://");
    builder.append(uri.getHost());// w  ww.  j ava2  s.c o  m
    if (uri.getPort() >= 0) {
        builder.append(":").append(uri.getPort());
    }
    if (uri.getPath() != null) {
        StringTokenizer tokenizer = new StringTokenizer(uri.getPath(), "/");
        // if there is more than one path element, then the first one should
        // be the webapp name
        if (tokenizer.countTokens() > 1) {
            builder.append("/").append(tokenizer.nextToken());
        }
    }
    return builder;
}

From source file:Main.java

/**
 * Makes an absolute URI from the relative id. If the given
 * id is already an URI, it will be returned unchanged.
 * //from w  w  w  .j a  v a2  s  .c o m
 * @param id A relative id
 * @param resType For example <em>track</em> (without namespace!)
 * 
 * @return An absolute URI
 */
public static String convertIdToURI(String id, String resType) {
    URI absolute;
    try {
        absolute = new URI(id);
    } catch (URISyntaxException e) {
        return "http://musicbrainz.org/" + resType.toLowerCase() + "/" + id;
    }
    if (absolute.getScheme() == null) {
        return "http://musicbrainz.org/" + resType.toLowerCase() + "/" + id;
    }

    // may be already an absolute URI
    return id;
}

From source file:UriUtils.java

/**
 * Resolves the specified URI, and returns the file represented by the URI.
 *
 * @param uri The URI for which to return an absolute path.
 * @return The {@link File} instance represented by the specified URI.
 * @throws IllegalArgumentException <ul><li>The URI cannot be null.</li><li>Wrong URI scheme for path resolution;
 *                                  only file:// URIs are supported.</li></ul>
 *///w w w. j  av  a  2 s .c  om
public static File getFile(URI uri) throws IllegalArgumentException {
    if (uri == null)
        throw new IllegalArgumentException("The URI cannot be null.");
    if (!"file".equals(uri.getScheme()))
        throw new IllegalArgumentException("Wrong URI scheme for path resolution, expected \"file\" "
                + "and got \"" + uri.getScheme() + "\"");

    // Workaround for the following bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5086147
    // Remove (or add, take your pick...) extra slashes after the scheme part.
    if (uri.getAuthority() != null)
        try {
            uri = new URI(uri.toString().replace("file://", "file:/"));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(
                    "The specified URI contains an authority, but could not be " + "normalized.", e);
        }

    return new File(uri);
}

From source file:Main.java

/** Crea una URI a partir de un nombre de fichero local o una URL.
 * @param file Nombre del fichero local o URL
 * @return URI (<code>file://</code>) del fichero local o URL
 * @throws URISyntaxException Si no se puede crear una URI soportada a partir de la cadena de entrada */
public static URI createURI(final String file) throws URISyntaxException {

    if (file == null || file.isEmpty()) {
        throw new IllegalArgumentException("No se puede crear una URI a partir de un nulo"); //$NON-NLS-1$
    }/*ww w .  j a  v a 2  s  . com*/

    String filename = file.trim();

    if (filename.isEmpty()) {
        throw new IllegalArgumentException("La URI no puede ser una cadena vacia"); //$NON-NLS-1$
    }

    // Cambiamos los caracteres Windows
    filename = filename.replace('\\', '/');

    // Realizamos los cambios necesarios para proteger los caracteres no
    // seguros
    // de la URL
    filename = filename.replace(" ", "%20") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("<", "%3C") //$NON-NLS-1$ //$NON-NLS-2$
            .replace(">", "%3E") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("\"", "%22") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("{", "%7B") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("}", "%7D") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("|", "%7C") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("^", "%5E") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("[", "%5B") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("]", "%5D") //$NON-NLS-1$ //$NON-NLS-2$
            .replace("`", "%60"); //$NON-NLS-1$ //$NON-NLS-2$

    final URI uri = new URI(filename);

    // Comprobamos si es un esquema soportado
    final String scheme = uri.getScheme();
    for (final String element : SUPPORTED_URI_SCHEMES) {
        if (element.equals(scheme)) {
            return uri;
        }
    }

    // Si el esquema es nulo, aun puede ser un nombre de fichero valido
    // El caracter '#' debe protegerse en rutas locales
    if (scheme == null) {
        filename = filename.replace("#", "%23"); //$NON-NLS-1$ //$NON-NLS-2$
        return createURI("file://" + filename); //$NON-NLS-1$
    }

    // Miramos si el esquema es una letra, en cuyo caso seguro que es una
    // unidad de Windows ("C:", "D:", etc.), y le anado el file://
    // El caracter '#' debe protegerse en rutas locales
    if (scheme.length() == 1 && Character.isLetter((char) scheme.getBytes()[0])) {
        filename = filename.replace("#", "%23"); //$NON-NLS-1$ //$NON-NLS-2$
        return createURI("file://" + filename); //$NON-NLS-1$
    }

    throw new URISyntaxException(filename, "Tipo de URI no soportado"); //$NON-NLS-1$

}

From source file:org.gradle.caching.http.internal.HttpBuildCache.java

/**
 * Create a safe URI from the given one by stripping out user info.
 *
 * @param uri Original URI/*w ww .  ja v a 2  s. com*/
 * @return a new URI with no user info
 */
private static URI safeUri(URI uri) {
    try {
        return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(),
                uri.getFragment());
    } catch (URISyntaxException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}

From source file:org.ovirt.engine.sdk4.internal.SsoUtils.java

/**
 * Construct SSO URL to obtain token from username and password.
 *
 * @param url oVirt engine URL//from   www . j a  va2  s . co  m
 * @return URI to be used to obtain token
 */
public static URI buildSsoUrlBasic(String url) {
    try {
        URI uri = new URI(url);
        URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/sso/oauth/%3$s",
                uri.getScheme(), uri.getAuthority(), ENTRY_POINT_TOKEN));
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new Error("Failed to build SSO authentication URL", ex);
    }
}

From source file:org.ovirt.engine.sdk4.internal.SsoUtils.java

/**
 * Construct SSO URL to obtain token from kerberos authentication.
 *
 * @param url oVirt engine URL/*from w ww . jav  a 2s .c om*/
 * @return URI to be used to obtain token
 */
public static URI buildSsoUrlKerberos(String url) {
    try {
        URI uri = new URI(url);
        URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/sso/oauth/%3$s",
                uri.getScheme(), uri.getAuthority(), ENTRY_POINT_HTTP));
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new Error("Failed to build SSO authentication URL", ex);
    }
}

From source file:com.google.cloud.hadoop.util.HttpTransportFactory.java

/**
 * Parse an HTTP proxy from a String address.
 * @param proxyAddress The address of the proxy of the form (https?://)HOST:PORT.
 * @return The URI of the proxy.//from   w w w.  java 2s .  c  om
 * @throws IllegalArgumentException If the address is invalid.
 */
@VisibleForTesting
static URI parseProxyAddress(@Nullable String proxyAddress) {
    if (Strings.isNullOrEmpty(proxyAddress)) {
        return null;
    }
    String uriString = proxyAddress;
    if (!uriString.contains("//")) {
        uriString = "//" + uriString;
    }
    try {
        URI uri = new URI(uriString);
        String scheme = uri.getScheme();
        String host = uri.getHost();
        int port = uri.getPort();
        if (!Strings.isNullOrEmpty(scheme) && !scheme.matches("https?")) {
            throw new IllegalArgumentException(
                    String.format("HTTP proxy address '%s' has invalid scheme '%s'.", proxyAddress, scheme));
        } else if (Strings.isNullOrEmpty(host)) {
            throw new IllegalArgumentException(String.format("Proxy address '%s' has no host.", proxyAddress));
        } else if (port == -1) {
            throw new IllegalArgumentException(String.format("Proxy address '%s' has no port.", proxyAddress));
        } else if (!uri.equals(new URI(scheme, null, host, port, null, null, null))) {
            throw new IllegalArgumentException(String.format("Invalid proxy address '%s'.", proxyAddress));
        }
        return uri;
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(String.format("Invalid proxy address '%s'.", proxyAddress), e);
    }
}

From source file:com.ibm.jaggr.core.util.PathUtil.java

public static URI appendToPath(URI uri, String append) throws URISyntaxException {
    return new URI(uri.getScheme(), uri.getAuthority(), uri.getPath() + append, uri.getQuery(),
            uri.getFragment());/*ww w  .j  a  v  a  2s  . c  o  m*/
}

From source file:org.ovirt.engine.sdk4.internal.SsoUtils.java

/**
 * Construct SSO URL to revoke SSO token
 *
 * @param url oVirt engine URL/*w ww .ja v a2s  . co m*/
 * @return URI to be used to revoke token
 */
public static URI buildSsoRevokeUrl(String url) {
    try {
        URI uri = new URI(url);
        URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/services/sso-logout",
                uri.getScheme(), uri.getAuthority()));
        return uriBuilder.build();
    } catch (URISyntaxException ex) {
        throw new Error("Failed to build SSO revoke URL", ex);
    }
}