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:org.sonatype.nexus.internal.httpclient.HttpClientManagerImpl.java

/**
 * Creates absolute request URI with full path from passed in context.
 *///from  www .ja  v  a 2  s.c o m
@Nonnull
private URI getRequestURI(final HttpContext context) {
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final HttpRequest httpRequest = clientContext.getRequest();
    final HttpHost target = clientContext.getTargetHost();
    try {
        URI uri;
        if (httpRequest instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) httpRequest).getURI();
        } else {
            uri = URI.create(httpRequest.getRequestLine().getUri());
        }
        return uri.isAbsolute() ? uri : URIUtils.resolve(URI.create(target.toURI()), uri);
    } catch (Exception e) {
        log.warn("Could not create absolute request URI", e);
        return URI.create(target.toURI());
    }
}

From source file:org.apache.streams.util.schema.SchemaStoreImpl.java

@Override
public synchronized Schema create(URI uri) {
    if (!getByUri(uri).isPresent()) {
        URI baseUri = UriUtil.removeFragment(uri);
        JsonNode baseNode = this.contentResolver.resolve(baseUri);
        if (uri.toString().contains("#") && !uri.toString().endsWith("#")) {
            Schema newSchema = new Schema(baseUri, baseNode, null, true);
            this.schemas.put(baseUri, newSchema);
            JsonNode childContent = this.fragmentResolver.resolve(baseNode,
                    '#' + StringUtils.substringAfter(uri.toString(), "#"));
            this.schemas.put(uri, new Schema(uri, childContent, newSchema, false));
        } else {/* w  ww  . j  a  v a  2 s. c  o  m*/
            if (baseNode.has("extends") && baseNode.get("extends").isObject()) {
                URI ref = URI.create((baseNode.get("extends")).get("$ref").asText());
                URI absoluteUri;
                if (ref.isAbsolute()) {
                    absoluteUri = ref;
                } else {
                    absoluteUri = baseUri.resolve(ref);
                }
                JsonNode parentNode = this.contentResolver.resolve(absoluteUri);
                Schema parentSchema;
                if (this.schemas.get(absoluteUri) != null) {
                    parentSchema = this.schemas.get(absoluteUri);
                } else {
                    parentSchema = create(absoluteUri);
                }
                this.schemas.put(uri, new Schema(uri, baseNode, parentSchema, true));
            } else {
                this.schemas.put(uri, new Schema(uri, baseNode, null, true));
            }
        }
        List<JsonNode> refs = baseNode.findValues("$ref");
        for (JsonNode ref : refs) {
            if (ref.isValueNode()) {
                String refVal = ref.asText();
                URI refUri = null;
                try {
                    refUri = URI.create(refVal);
                } catch (Exception ex) {
                    LOGGER.info("Exception: {}", ex.getMessage());
                }
                if (refUri != null && !getByUri(refUri).isPresent()) {
                    if (refUri.isAbsolute()) {
                        create(refUri);
                    } else {
                        create(baseUri.resolve(refUri));
                    }
                }
            }
        }
    }

    return this.schemas.get(uri);
}

From source file:com.amytech.android.library.utils.asynchttp.MyRedirectHandler.java

@Override
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }/*from w  w w  . ja  v a  2s  . c  om*/
    // get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
        // got a redirect response, but no location header
        throw new ProtocolException(
                "Received redirect response " + response.getStatusLine() + " but no location header");
    }
    // HERE IS THE MODIFIED LINE OF CODE
    String location = locationHeader.getValue().replaceAll(" ", "%20");

    URI uri;
    try {
        uri = new URI(location);
    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }

    HttpParams params = response.getParams();
    // rfc2616 demands the location value be a complete URI
    // Location = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
        if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
            throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
        }
        // Adjust location URI
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            throw new IllegalStateException("Target host not available " + "in the HTTP context");
        }

        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

        try {
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        } catch (URISyntaxException ex) {
            throw new ProtocolException(ex.getMessage(), ex);
        }
    }

    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {

        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);

        if (redirectLocations == null) {
            redirectLocations = new RedirectLocations();
            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
        }

        URI redirectURI;
        if (uri.getFragment() != null) {
            try {
                HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                redirectURI = URIUtils.rewriteURI(uri, target, true);
            } catch (URISyntaxException ex) {
                throw new ProtocolException(ex.getMessage(), ex);
            }
        } else {
            redirectURI = uri;
        }

        if (redirectLocations.contains(redirectURI)) {
            throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
        } else {
            redirectLocations.add(redirectURI);
        }
    }

    return uri;
}

From source file:org.kitodo.filemanagement.FileManagement.java

@Override
public Long getSizeOfDirectory(URI directory) throws IOException {
    if (!directory.isAbsolute()) {
        directory = fileMapper.mapUriToKitodoDataDirectoryUri(directory);
    }//from   w  w  w. j  av  a2 s. com
    if (isDirectory(directory)) {
        return FileUtils.sizeOfDirectory(new File(directory));
    } else {
        throw new IOException("Given URI doesn't point to the directory!");
    }
}

From source file:cn.com.loopj.android.http.MyRedirectHandler.java

@Override
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }/*from   w ww . ja  va  2 s .  c  o m*/
    //get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
        // got a redirect response, but no location header
        throw new ProtocolException(
                "Received redirect response " + response.getStatusLine() + " but no location header");
    }
    //HERE IS THE MODIFIED LINE OF CODE
    String location = locationHeader.getValue().replaceAll(" ", "%20");

    URI uri;
    try {
        uri = new URI(location);
    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }

    HttpParams params = response.getParams();
    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
        if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
            throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
        }
        // Adjust location URI
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            throw new IllegalStateException("Target host not available " + "in the HTTP context");
        }

        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

        try {
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        } catch (URISyntaxException ex) {
            throw new ProtocolException(ex.getMessage(), ex);
        }
    }

    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {

        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);

        if (redirectLocations == null) {
            redirectLocations = new RedirectLocations();
            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
        }

        URI redirectURI;
        if (uri.getFragment() != null) {
            try {
                HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                redirectURI = URIUtils.rewriteURI(uri, target, true);
            } catch (URISyntaxException ex) {
                throw new ProtocolException(ex.getMessage(), ex);
            }
        } else {
            redirectURI = uri;
        }

        if (redirectLocations.contains(redirectURI)) {
            throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
        } else {
            redirectLocations.add(redirectURI);
        }
    }

    return uri;
}

From source file:org.apache.http2.impl.client.DefaultRedirectStrategy.java

public URI getLocationURI(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }//from  ww  w .  j  a va 2s . com
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    //get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
        // got a redirect response, but no location header
        throw new ProtocolException(
                "Received redirect response " + response.getStatusLine() + " but no location header");
    }
    String location = locationHeader.getValue();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }

    URI uri = createLocationURI(location);

    HttpParams params = request.getParams();
    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    try {
        // Drop fragment
        uri = URIUtils.rewriteURI(uri);
        if (!uri.isAbsolute()) {
            if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
                throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
            }
            // Adjust location URI
            HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (target == null) {
                throw new IllegalStateException("Target host not available " + "in the HTTP context");
            }
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        }
    } catch (URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
    }

    RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
    if (redirectLocations == null) {
        redirectLocations = new RedirectLocations();
        context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
    }
    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
        if (redirectLocations.contains(uri)) {
            throw new CircularRedirectException("Circular redirect to '" + uri + "'");
        }
    }
    redirectLocations.add(uri);
    return uri;
}

From source file:org.eclipse.orion.server.tests.servlets.files.FileSystemTest.java

/**
 * Makes a URI absolute. If the provided URI is relative, it is assumed to be relative to the workspace location (file servlet location).
 * If the provided URI is already absolute it is returned as-is.
 * The provided URI must be correctly encoded.
 *///from  ww  w.j  a  v a2s .  c  o m
protected String makeResourceURIAbsolute(String uriString) {
    try {
        URI uri = new URI(uriString);
        if (uri.isAbsolute())
            return uriString;
        if (uriString.startsWith("/")) {
            return toAbsoluteURI(uriString);
        }
    } catch (URISyntaxException e) {
        //unencoded string - fall through
    }
    try {
        if (uriString.startsWith(FILE_SERVLET_LOCATION))
            return URIUtil.fromString(SERVER_LOCATION + uriString).toString();
        //avoid double slash
        if (uriString.startsWith("/"))
            uriString = uriString.substring(1);
        String path = new Path(FILE_SERVLET_LOCATION).append(getTestBaseResourceURILocation()).append(uriString)
                .toString();
        return new URI(SERVER_LOCATION + path).toString();
    } catch (URISyntaxException e) {
        fail(e.getMessage());
        return null;
    }
}

From source file:com.bincode.util.DefaultRedirectHandler.java

public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }/*from  ww  w. j a v  a  2  s .c om*/
    //get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
        // got a redirect response, but no location header
        throw new ProtocolException(
                "Received redirect response " + response.getStatusLine() + " but no location header");
    }
    String location = locationHeader.getValue();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }

    URI uri;
    try {
        uri = new URI(location);
    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }

    HttpParams params = response.getParams();
    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
        if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
            throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
        }
        // Adjust location URI
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            throw new IllegalStateException("Target host not available " + "in the HTTP context");
        }

        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

        try {
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        } catch (URISyntaxException ex) {
            throw new ProtocolException(ex.getMessage(), ex);
        }
    }

    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {

        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);

        if (redirectLocations == null) {
            redirectLocations = new RedirectLocations();
            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
        }

        URI redirectURI;
        if (uri.getFragment() != null) {
            try {
                HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                redirectURI = URIUtils.rewriteURI(uri, target, true);
            } catch (URISyntaxException ex) {
                throw new ProtocolException(ex.getMessage(), ex);
            }
        } else {
            redirectURI = uri;
        }

        if (redirectLocations.contains(redirectURI)) {
            throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
        } else {
            redirectLocations.add(redirectURI);
        }
    }

    return uri;
}

From source file:org.apache.http.impl.nio.client.AbstractHttpAsyncClient.java

private 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;/*from   w  w  w.  j a  v  a  2  s  . 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:net.xy.jcms.shared.adapter.HttpRequestDataAccessContext.java

@Override
public String buildUriWithParams(final String requestString, final Map<Object, Object> parameters) {
    // 1. should not alter external links
    // 2. should convert absolute uris to relatives if possible
    try {/*from   w  w  w.j  ava 2s.c o m*/
        URI build; // the initial request
        try {
            // first asume its already an proper url
            build = new URI(requestString);
        } catch (final URISyntaxException ex) {
            // second encode to an proper url
            final String requestUri = URLEncoder.encode(requestString, "UTF-8");
            build = new URI(requestUri);
        }
        if (!build.isAbsolute()) {
            build = rootUrl.resolve(build); // make it absolute
        }
        final String path = StringUtils.isNotBlank(build.getPath()) ? build.getPath() : "/";
        build = new URI(build.getScheme(), build.getUserInfo(), build.getHost(), build.getPort(), path,
                buildQuery(parameters), build.getFragment());
        final URI relBuild = rootUrl.relativize(build);
        final String ret;
        if (!relBuild.isAbsolute()) {
            // because we relativate it always to docroot, which is the
            // servlet container in JEE
            ret = contextPath + relBuild.toASCIIString().replace("+", "%20");
        } else {
            ret = relBuild.toASCIIString();
        }
        return ret;
    } catch (final URISyntaxException e) {
        throw new IllegalArgumentException("URL couldn't be build from given parameters. "
                + DebugUtils.printFields(requestString, parameters), e);
    } catch (final UnsupportedEncodingException e) {
        throw new IllegalArgumentException(
                " Encoding is not supported " + DebugUtils.printFields(requestString, parameters), e);
    }
}