Example usage for java.net URI isAbsolute

List of usage examples for java.net URI isAbsolute

Introduction

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

Prototype

public boolean isAbsolute() 

Source Link

Document

Tells whether or not this URI is absolute.

Usage

From source file:com.msopentech.odatajclient.engine.utils.URIUtils.java

/**
 * Build URI starting from the given base and href.
 * <br/>// w  w w.  jav  a2 s  .  c  o m
 * If href is absolute or base is null then base will be ignored.
 *
 * @param base URI prefix.
 * @param href URI suffix.
 * @return built URI.
 */
public static URI getURI(final String base, final String href) {
    if (href == null) {
        throw new IllegalArgumentException("Null link provided");
    }

    URI uri = URI.create(href);

    if (!uri.isAbsolute() && base != null) {
        uri = URI.create(base + "/" + href);
    }

    return uri.normalize();
}

From source file:com.msopentech.odatajclient.engine.utils.URIUtils.java

/**
 * Build URI starting from the given base and href.
 * <br/>/*from   w w  w  .j a  va 2 s  .com*/
 * If href is absolute or base is null then base will be ignored.
 *
 * @param base URI prefix.
 * @param href URI suffix.
 * @return built URI.
 */
public static URI getURI(final URI base, final String href) {
    if (href == null) {
        throw new IllegalArgumentException("Null link provided");
    }

    URI uri = URI.create(href);

    if (!uri.isAbsolute() && base != null) {
        uri = URI.create(base.toASCIIString() + "/" + href);
    }

    return uri.normalize();
}

From source file:com.vaadin.sass.testcases.scss.W3ConformanceTests.java

public static void extractCSS(final URI url, File targetdir) throws Exception {
    /*/*from   w ww.j  a va2 s . c  o  m*/
     * For each test URL: 1) extract <style> tag contents 2) extract from
     * <link rel="stylesheet"> files 3) extract inline style attributes from
     * all elements and wrap the result in .style {}
     */

    Document doc = Jsoup.connect(url.toString()).timeout(20000).get();

    List<String> tests = new ArrayList<String>();

    for (Element e : doc.select("style[type=text/css]")) {
        tests.add(e.data());
    }

    for (Element e : doc.select("link[rel=stylesheet][href][type=text/css]")) {
        URI cssUri = new URI(e.attr("href"));
        if (!cssUri.isAbsolute()) {
            cssUri = url.resolve(cssUri);
        }
        String encoding = doc.outputSettings().charset().name();
        tests.add(IOUtils.toString(cssUri, encoding));
    }

    for (Element e : doc.select("*[style]")) {
        tests.add(String.format(".style { %s }", e.attr("style")));
    }

    for (final String test : tests) {
        targetdir.mkdirs();
        String logfile = String.format("%s.%d.scss", FilenameUtils.getBaseName(url.toString()),
                tests.indexOf(test));
        PrintStream dataLogger = new PrintStream(new File(targetdir, logfile));

        dataLogger.println("/* Source: " + url + " */");
        dataLogger.println(test);

    }
}

From source file:edu.usc.goffish.gofs.namenode.DataNode.java

private static Path convertLocalURIToPath(URI localURI) throws IOException {
    if (localURI == null || localURI.isOpaque() || !localURI.isAbsolute()
            || !"file".equalsIgnoreCase(localURI.getScheme())) {
        throw new IllegalArgumentException();
    }//from   w w w. j  a v a  2  s  .  com
    if (!URIHelper.isLocalURI(localURI)) {
        throw new IOException("uri host " + localURI.getHost() + " is not local");
    }

    String path = localURI.getPath();
    if (path == null) {
        path = "/";
    }
    return Paths.get(URI.create("file://" + path));
}

From source file:at.bitfire.davdroid.webdav.DavRedirectStrategy.java

/**
 * Gets the destination of a redirection 
 * @return absolute URL of new location; null if not available
 *//*w w  w . j a  va2s .c om*/
static URI getLocation(HttpRequest request, HttpResponse response, HttpContext context) {
    Header locationHdr = response.getFirstHeader("Location");
    if (locationHdr == null) {
        Log.e(TAG, "Received redirection without Location header, ignoring");
        return null;
    }
    try {
        URI location = URIUtils.parseURI(locationHdr.getValue(), false);

        // some servers don't return absolute URLs as required by RFC 2616
        if (!location.isAbsolute()) {
            Log.w(TAG, "Received invalid redirection to relative URL, repairing");
            URI originalURI = URIUtils.parseURI(request.getRequestLine().getUri(), false);
            if (!originalURI.isAbsolute()) {
                final HttpHost target = HttpClientContext.adapt(context).getTargetHost();
                if (target != null)
                    originalURI = org.apache.http.client.utils.URIUtilsHC4.rewriteURI(originalURI, target);
                else
                    return null;
            }
            return originalURI.resolve(location);
        }
        return location;
    } catch (URISyntaxException e) {
        Log.e(TAG, "Received redirection from/to invalid URI, ignoring", e);
    }
    return null;
}

From source file:com.epam.reportportal.apache.http.impl.client.CloseableHttpClient.java

private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException {
    // A null target may be acceptable if there is a default target.
    // Otherwise, the null target is detected in the director.
    HttpHost target = null;/* www.  j  a v a 2s  .c  o m*/

    final URI requestURI = request.getURI();
    if (requestURI.isAbsolute()) {
        target = URIUtils.extractHost(requestURI);
        if (target == null) {
            throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI);
        }
    }
    return target;
}

From source file:eu.eubrazilcc.lvl.core.util.UrlUtils.java

/**
 * Gets the path part of an URI./*from  w w w. j a  v  a  2  s.co m*/
 * @param uri - input URI
 * @return the path part of the specified URI or an empty string if one does not exist.
 * @throws IllegalArgumentException If a malformed URI occurs.
 */
public static String getPath(final URI uri) {
    checkArgument(uri != null, "Uninitialized URI");
    try {
        final URI nUri = uri.normalize();
        final URL url = nUri.isAbsolute() ? nUri.toURL()
                : new URL(new URL("http://example.org/"), nUri.getPath());
        return url.getPath();
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Malformed URI", e);
    }
}

From source file:org.apache.pig.parser.QueryParserUtils.java

static Set<String> getRemoteHosts(String absolutePath, String defaultHost) {
    String HAR_PREFIX = "hdfs-";
    Set<String> result = new HashSet<String>();
    String[] fnames = absolutePath.split(",");
    for (String fname : fnames) {
        // remove leading/trailing whitespace(s)
        fname = fname.trim();/*w  ww .  j  av  a 2s  .c o  m*/
        Path p = new Path(fname);
        URI uri = p.toUri();
        if (uri.isAbsolute()) {
            String scheme = uri.getScheme();
            if (scheme != null && scheme.toLowerCase().equals("hdfs") || scheme.toLowerCase().equals("har")) {
                if (uri.getHost() == null)
                    continue;
                String thisHost = uri.getHost().toLowerCase();
                if (scheme.toLowerCase().equals("har")) {
                    if (thisHost.startsWith(HAR_PREFIX)) {
                        thisHost = thisHost.substring(HAR_PREFIX.length());
                    }
                }
                if (!uri.getHost().isEmpty() && !thisHost.equals(defaultHost)) {
                    if (uri.getPort() != -1)
                        result.add("hdfs://" + thisHost + ":" + uri.getPort());
                    else
                        result.add("hdfs://" + thisHost);
                }
            }
        }
    }
    return result;
}

From source file:eu.eubrazilcc.lvl.core.util.UrlUtils.java

/**
 * Returns a list of name-value pairs as built from the URI's query portion.
 * @param uri - input URI//from  ww w . ja v  a 2  s . co  m
 * @return a list of name-value pairs as built from the URI's query portion.
 * @throws IllegalArgumentException If a malformed URI occurs.
 */
public static Map<String, String> getQueryParams(final URI uri) {
    checkArgument(uri != null, "Uninitialized URI");
    try {
        final URI nUri = uri.normalize();
        final URL url = nUri.isAbsolute() ? nUri.toURL()
                : new URL(new URL("http://example.org/"), nUri.toString());
        final String query = new URL(URLDecoder.decode(url.toString(), defaultCharset().name())).getQuery();
        final Map<String, String> map = newHashMap();
        final List<NameValuePair> pairs = URLEncodedUtils.parse(query, defaultCharset());
        final Iterator<NameValuePair> iterator = pairs.iterator();
        while (iterator.hasNext()) {
            final NameValuePair pair = iterator.next();
            map.put(pair.getName(), pair.getValue());
        }
        return map;
    } catch (MalformedURLException | UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Malformed URI", e);
    }
}

From source file:com.netflix.http4.NFHttpClient.java

private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException {
    // A null target may be acceptable if there is a default target.
    // Otherwise, the null target is detected in the director.
    HttpHost target = null;/* w  w  w .j  ava 2  s.c om*/
    URI requestURI = request.getURI();
    if (requestURI.isAbsolute()) {
        target = URIUtils.extractHost(requestURI);
        if (target == null) {
            throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI);
        }
    }
    return target;
}