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

/**
 * Performs an HTTP GET request to the specified url, and return the String
 * read from HttpResponse./*  www  .ja v  a2  s  .  c om*/
 * @param url The web address to post the request to
 * @return The result of the request
 * @throws Exception
 */
public static String executeHttpGet(String url) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * Convert a string url into a java.net.URI instance.
 *
 * @param url a url string./*from  w  w w  .j a  va2s  .  c o  m*/
 * @return an instance of URI.
 */
public static URI toURI(String url) {
    if (url == null) {
        throw new IllegalArgumentException("Null url cannot be converted to a URI");
    }
    try {
        return new URI(url);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

/**
 * Return true if the given string can be parsed as a URI and
 * begins with a URI scheme (e.g. "http://something"), false otherwise.
 *
 * @param uriString//from w ww .j  a  v a 2s  .  co m
 * @throws NullPointerException If uriString is null.
 * @return
 */
public static boolean isAbsoluteUri(String uriString) {
    try {
        URI uri = new URI(uriString);
        return uri.getScheme() != null;
    } catch (URISyntaxException ex) {
        return false;
    }
}

From source file:net.refractions.udig.document.source.ShpDocUtils.java

/**
 * Gets the absolute path with the url as the parent path and the relative path as the child
 * path./* ww w. j av a  2 s  .  co  m*/
 * 
 * @param url
 * @param relativePath
 * @return absolute path
 */
public static String getAbsolutePath(URL url, String relativePath) {
    if (relativePath != null && relativePath.trim().length() > 0) {
        try {
            final File parentFile = new File(new URI(url.toString()));
            final File childFile = new File(parentFile.getParent(), relativePath);
            return childFile.getAbsolutePath();
        } catch (URISyntaxException e) {
            // Should not happen as shapefile should be a valid file
            e.printStackTrace();
        }
    }
    return null;
}

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// w w w .java 2  s .  c om
 * @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:URIUtil.java

/**
 * Get the parent URI of the supplied URI
 * @param uri The input URI for which the parent URI is being requrested.
 * @return The parent URI.  Returns a URI instance equivalent to "../" if
 * the supplied URI path has no parent.//from  w w w.  java 2 s  .  c  o m
 * @throws URISyntaxException Failed to reconstruct the parent URI.
 */
public static URI getParent(URI uri) throws URISyntaxException {

    String parentPath = new File(uri.getPath()).getParent();

    if (parentPath == null) {
        return new URI("../");
    }

    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
            parentPath.replace('\\', '/'), uri.getQuery(), uri.getFragment());
}

From source file:com.openshift.internal.util.URIUtils.java

public static Map<String, String> splitFragment(String location) {
    if (StringUtils.isEmpty(location)) {
        return Collections.emptyMap();
    }/*from   w ww .  j a v a  2 s. c  o  m*/
    URI uri = null;
    try {
        uri = new URI(location);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    return splitFragment(uri);
}

From source file:Main.java

public static String httpStringGet(String url, String enc) throws Exception {
    // This method for HttpConnection
    String page = "";
    BufferedReader bufferedReader = null;
    try {/*  w  ww.  j  ava 2  s  .c om*/
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");

        HttpParams httpParams = client.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);

        HttpGet request = new HttpGet();
        request.setHeader("Content-Type", "text/plain; charset=utf-8");
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), enc));

        StringBuffer stringBuffer = new StringBuffer("");
        String line = "";

        String NL = System.getProperty("line.separator");// "\n"
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line + NL);
        }
        bufferedReader.close();
        page = stringBuffer.toString();
        Log.i("page", page);
        System.out.println(page + "page");
        return page;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                Log.d("BBB", e.toString());
            }
        }
    }
}

From source file:TestMapObjInterface.java

public static ArrayList<String> getChunks(String file, String fsName, String cnaddress) throws Exception {
    ArrayList<String> output = new ArrayList<String>();
    File target = new File(file);
    HttpClient client = new HttpClient();
    URI uri = new URI("http://" + cnaddress + ":14149/mproxy/map_obj");
    HTTPChunkLocator loc = new HTTPChunkLocator(client, uri);
    ChunkLocation[] chunks = loc.getChunkLocations(target, fsName);
    int i = 0;//  w w  w .j  a v a2 s . c o  m
    for (i = 0; i < chunks.length; i++) {
        StringBuilder b = new StringBuilder();
        b.append("[" + i + "] " + chunks[i].getChunkInfo().toString());
        b.append(": Filelength " + chunks[i].getFileLength());
        StorageNodeInfo[] nodes = chunks[i].getStorageNodeInfo();
        int j = 0;
        for (j = 0; j < nodes.length; j++) {
            b.append(" <" + j + ">" + nodes[j].toString());
        }
        output.add(b.toString());
    }
    return output;
}

From source file:com.flowlogix.util.PathUtil.java

/**
 * Returns full path URI for a relative path
 * //  ww  w  .ja  va  2  s .c  o m
 * @param request
 * @param relativePath
 * @return relative URI
 */
@SneakyThrows(URISyntaxException.class)
public static URI toAppURI(HttpServletRequest request, String relativePath) {
    String absolutePath = String.format("%s%s/%s", getServerPath(request), request.getContextPath(),
            relativePath);

    return new URI(absolutePath);
}