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 getDomainName(String url) {

    String domain = "";

    URI uri;/* w  w  w .ja  v  a2s.  c  om*/

    try {
        uri = new URI(url);
        domain = uri.getHost();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    return domain.startsWith("www.") ? domain.substring(4) : domain;
}

From source file:Main.java

public static String resolvePath(final String path) {
    try {/* w  w  w.java 2 s  . c om*/
        final URI uri = new URI(path);
        return removeExtraSlashes(URLDecoder.decode(uri.getPath(), "UTF-8"));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static File getFile(URL url) {

    String dir = url.toString();/*from   w w w . j  a v  a 2s  . com*/
    File md;

    try {
        md = new File(new URI(dir));
    } catch (IllegalArgumentException e) {

        // this is an attempt at a crazy workaround that should not really work
        if (dir.startsWith("file://")) {
            md = new File(dir.substring(5, dir.length()).replaceAll("\\%20", " "));
        } else {
            System.err.println("this should never happen! bad file: " + dir);
            md = new File("maps/");
        }
    } catch (Exception e) {
        throw new RuntimeException("Cant create file: " + dir, e);
    }

    return md;

}

From source file:Main.java

public static URI getURIAttribute(Element el, String attr, URI defaultValue) {
    if (el.hasAttribute(attr)) {
        try {//  ww  w  . j  a v  a2 s  . c  om
            return new URI(el.getAttribute(attr));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return defaultValue;
}

From source file:Main.java

public static String responseContent(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;/*from ww w .  j ava2s . c o m*/
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

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  ww  w  . j a v  a 2s  .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:Main.java

public static String getDomain(String target) {
    Pattern p = Pattern.compile(".*?([^.]+\\.[^.]+)");
    URI uri;/*www .  ja  v a2s  .  c  o m*/

    try {
        uri = new URI(target);

    } catch (Exception e) {
        return "http";
    }

    String host = uri.getHost();
    Matcher m = p.matcher(host);

    if (m.matches())
        return m.group(1);
    else
        return host;
}

From source file:Main.java

/**
 * This method returns an absolute URI using the given type
 * and namespace. If the type is already an absolute URI, it
 * is returned unchanged./*from  w  ww.j a v a  2s. c o  m*/
 * 
 * @param type The type
 * @param namespace The namespace
 *  
 * @return An absolute URI
 */
public static String convertTypeToURI(String type, String namespace) {
    if (type == null || type.equals(""))
        return null;

    URI absolute;
    try {
        absolute = new URI(type);
    } catch (URISyntaxException e) {
        return namespace + type;
    }
    if (absolute.getScheme() == null) {
        return namespace + type;
    }

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

From source file:Main.java

public static HttpResponse getData(String url) {
    HttpResponse response = null;/*from   w  w w.  jav a 2  s.c o  m*/
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

From source file:Main.java

/**
 * Given an endpoint, calculate the corresponding BrowserID audience.
 * <p>/*from w w w.  ja va  2s . co m*/
 * This is the domain, in web parlance.
 *
 * @param serverURI endpoint.
 * @return BrowserID audience.
 * @throws URISyntaxException
 */
public static String getAudienceForURL(String serverURI) throws URISyntaxException {
    URI uri = new URI(serverURI);
    return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), null, null, null).toString();
}