Example usage for java.net URI URI

List of usage examples for java.net URI URI

Introduction

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

Prototype

public URI(String str) throws URISyntaxException 

Source Link

Document

Constructs a URI by parsing the given string.

Usage

From source file:Main.java

public static String getFileName2(String url) {
    String file = null;/* w ww.  ja  v a  2 s .  com*/
    try {
        file = new File(new URI(url).getPath()).getName();
        System.out.println("File name: " + file);
    } catch (Exception e) {
    }
    if (file == null || file.length() < 1)
        file = "FILE";
    return file;
}

From source file:Main.java

public static URI createServiceUri(String gridId, String serviceId) {
    try {//from  w  ww  .j  a  v  a 2 s  .c om
        return new URI("servicegrid:service:" + gridId + ":" + serviceId);
    } catch (URISyntaxException e) {
        // must be unreachable
        throw new RuntimeException(e);
    }
}

From source file:com.cloudera.MultiplexedTest.java

public static void main(String[] args) throws Exception {
    options = new Options();
    final Configuration conf = new Configuration();
    final FileSystem fs = FileSystem.get(new URI(options.uri), conf);

    long nanoStart = System.nanoTime();
    BaseThread threads[] = new BaseThread[options.nThreads];
    if (options.isWriteOperation) {
        for (int i = 0; i < options.nThreads; i++) {
            threads[i] = WriterThread.create(i, fs);
        }/* ww w.  j  a  va2  s .c  o m*/
    } else {
        for (int i = 0; i < options.nThreads; i++) {
            threads[i] = ReaderThread.create(i, fs);
        }
    }
    for (int i = 0; i < options.nThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < options.nThreads; i++) {
        threads[i].join();
    }
    for (int i = 0; i < options.nThreads; i++) {
        Throwable t = threads[i].getException();
        if (t != null) {
            System.err.println("there were exceptions.  Aborting.");
            System.exit(1);
        }
    }
    long nanoEnd = System.nanoTime();
    fs.close();
    long totalIo = options.nThreads;
    totalIo *= options.nBytesPerThread;
    float nanoDiff = nanoEnd - nanoStart;
    float seconds = nanoDiff / 1000000000;
    float rate = totalIo / seconds;
    System.out.println(String.format("Using %d threads, average rate was %s/s\n" + "Total time was %f seconds",
            options.nThreads, prettyPrintByteSize(rate), seconds));
}

From source file:Main.java

public static URI createServiceTypeUri(String domainId, String serviceTypeId) {
    try {/*from   ww  w . j a va2s.c o  m*/
        return new URI("servicegrid:servicetype:" + domainId + ":" + serviceTypeId);
    } catch (URISyntaxException e) {
        // must be unreachable
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Convert an URL to a valid systemId according to RFC 2396.
 *///from   w  w  w  .  ja  v a2  s. c  om
public static String toSystemId(URL url) {
    try {
        return new URI(url.toExternalForm()).toASCIIString();
    } catch (URISyntaxException e) {
        return url.toExternalForm();
    }
}

From source file:Main.java

public static URI createURI(final String scheme, final String host, int port, final String path,
        final String query, final String fragment) throws URISyntaxException {

    StringBuilder buffer = new StringBuilder();
    if (host != null) {
        if (scheme != null) {
            buffer.append(scheme);//ww  w.j  a  va 2s  . c om
            buffer.append("://");
        } else {
            buffer.append("//");
        }
        buffer.append(host);
        if (port > 0) {
            buffer.append(':');
            buffer.append(port);
        }
    }
    if (path == null || !path.startsWith("/")) {
        buffer.append('/');
    }
    if (path != null) {
        buffer.append(path);
    }
    if (query != null) {
        buffer.append('?');
        buffer.append(query);
    }
    if (fragment != null) {
        buffer.append('#');
        buffer.append(fragment);
    }
    return new URI(buffer.toString());
}

From source file:de.micromata.jira.rest.core.util.URIParser.java

public static URI parseStringToURI(String uri) {
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    if (urlValidator.isValid(uri)) {
        try {//w  ww .  j av  a  2  s . co m
            return new URI(uri);
        } catch (URISyntaxException e) {
            return null;
        }
    }
    return null;
}

From source file:net.desgrange.pwad.service.UrlUtils.java

public static String getParameter(final String url, final String parameterName) throws BadUrlException {
    try {/*from  w  w  w  . jav  a  2  s .  c om*/
        final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(new URI(url), "UTF-8");
        for (final NameValuePair pair : nameValuePairs) {
            if (pair.getName().equalsIgnoreCase(parameterName)) {
                return pair.getValue();
            }
        }
        return null;
    } catch (final URISyntaxException e) {
        throw new BadUrlException(e);
    } catch (final IllegalArgumentException e) {
        throw new BadUrlException(e);
    } catch (final NullPointerException e) {
        throw new BadUrlException(e);
    }
}

From source file:com.widowcrawler.core.util.DomainUtils.java

public static boolean isBaseDomain(String baseDomain, String url) {
    try {/*from w  w  w .  ja  v a 2  s.  com*/
        baseDomain = StringUtils.lowerCase(StringUtils.trim(baseDomain));
        String domain = StringUtils.lowerCase(StringUtils.trimToNull(new URI(url).getHost()));

        if (domain == null) {
            logger.info("Returning false because url's domain is null");
            return false;
        }

        while (StringUtils.isNotBlank(domain)) {
            if (StringUtils.equals(baseDomain, domain)) {
                return true;
            }

            domain = StringUtils.substringAfter(domain, ".");
        }

        return false;

    } catch (URISyntaxException ex) {
        logger.error("Could not determine base domain relationship. Returning default of false.", ex);
        return false;
    }
}

From source file:com.jayway.restassured.internal.UriValidator.java

/**
 * Checks if the <code>potentialUri</code> is a URI.
 *
 * @param potentialUri The URI to check.
 * @return <code>true</code> if it is a URI, <code>false</code> otherwise.
 *//*from  w  ww.ja  va2 s  . c o m*/
public static boolean isUri(String potentialUri) {
    if (StringUtils.isBlank(potentialUri)) {
        return false;
    }

    try {
        URI uri = new URI(potentialUri);
        return uri.getScheme() != null && uri.getHost() != null;
    } catch (URISyntaxException e) {
        return false;
    }
}