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.epam.reportportal.apache.http.impl.client.DefaultRedirectStrategy.java

public URI getLocationURI(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    Args.notNull(request, "HTTP request");
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    //get the location header to find out where to redirect to
    final 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");
    }/*  w  w w .  j  av a2 s.  com*/
    final String location = locationHeader.getValue();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }

    final RequestConfig config = clientContext.getRequestConfig();

    URI uri = createLocationURI(location);

    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    try {
        if (!uri.isAbsolute()) {
            if (!config.isRelativeRedirectsAllowed()) {
                throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
            }
            // Adjust location URI
            final HttpHost target = clientContext.getTargetHost();
            Asserts.notNull(target, "Target host");
            final URI requestURI = new URI(request.getRequestLine().getUri());
            final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        }
    } catch (final URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
    }

    RedirectLocations redirectLocations = (RedirectLocations) clientContext
            .getAttribute(HttpClientContext.REDIRECT_LOCATIONS);
    if (redirectLocations == null) {
        redirectLocations = new RedirectLocations();
        context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
    }
    if (!config.isCircularRedirectsAllowed()) {
        if (redirectLocations.contains(uri)) {
            throw new CircularRedirectException("Circular redirect to '" + uri + "'");
        }
    }
    redirectLocations.add(uri);
    return uri;
}

From source file:ru.histone.resourceloaders.DefaultResourceLoader.java

public URI makeFullLocation(String location, String baseLocation) {
    if (location == null) {
        throw new ResourceLoadException("Resource location is undefined!");
    }//from  ww  w .  j a  v a  2s. c  om

    URI locationURI = URI.create(location);

    if (baseLocation == null && !locationURI.isAbsolute()) {
        throw new ResourceLoadException("Base HREF is empty and resource location is not absolute!");
    }

    if (baseLocation != null) {
        baseLocation = baseLocation.replace("\\", "/");
        baseLocation = baseLocation.replace("file://", "file:/");
    }
    URI baseLocationURI = (baseLocation != null) ? URI.create(baseLocation) : null;

    if (!locationURI.isAbsolute() && baseLocation != null) {
        locationURI = baseLocationURI.resolve(locationURI.normalize());
    }

    if (!locationURI.isAbsolute()) {
        throw new ResourceLoadException("Resource location is not absolute!");
    }

    return locationURI;
}

From source file:org.apache.openaz.xacml.pdp.std.StdPolicyFinder.java

/**
 * Looks up the given {@link org.apache.openaz.xacml.api.Identifier} in the map first. If not found, and
 * the <code>Identifier</code> contains a URL, then attempts to retrieve the document from the URL and
 * caches it./*www.j  a  va  2  s .c  o m*/
 *
 * @param idReferenceMatch the <code>IdReferenceMatch</code> to look up
 * @return a <code>PolicyFinderResult</code> with the requested <code>Policy</code> or an error status
 */
private PolicyFinderResult<Policy> lookupPolicyByIdentifier(IdReferenceMatch idReferenceMatch) {
    List<Policy> listCachedPolicies = this.getFromPolicyMap(idReferenceMatch, Policy.class);
    if (listCachedPolicies == null) {
        Identifier id = idReferenceMatch.getId();
        if (id != null) {
            URI uri = id.getUri();
            if (uri != null && uri.isAbsolute()) {
                PolicyDef policyDef = null;
                try {
                    policyDef = this.loadPolicyDefFromURI(uri);
                } catch (StdPolicyFinderException ex) {
                    return new StdPolicyFinderResult<Policy>(
                            new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, ex.getMessage()));
                }
                if (policyDef != null) {
                    if (policyDef instanceof Policy) {
                        List<PolicyDef> listPolicyDefs = new ArrayList<PolicyDef>();
                        listPolicyDefs.add(policyDef);
                        this.mapPolicies.put(id, listPolicyDefs);
                        this.mapPolicies.put(policyDef.getIdentifier(), listPolicyDefs);
                        return new StdPolicyFinderResult<Policy>((Policy) policyDef);
                    } else {
                        return PFR_NOT_A_POLICY;
                    }
                } else {
                    return PFR_POLICY_NOT_FOUND;
                }
            }
        }
    }
    if (listCachedPolicies != null) {
        return new StdPolicyFinderResult<Policy>(this.getBestMatch(listCachedPolicies));
    } else {
        return PFR_POLICY_NOT_FOUND;
    }
}

From source file:org.apache.openaz.xacml.pdp.std.StdPolicyFinder.java

/**
 * Looks up the given {@link org.apache.openaz.xacml.api.Identifier} in the map first. If not found, and
 * the <code>Identifier</code> contains a URL, then attempts to retrieve the document from the URL and
 * caches it.//from   w  w w .j  a  v  a2 s .  c o m
 *
 * @param idReferenceMatch the <code>IdReferenceMatch</code> to look up
 * @return a <code>PolicyFinderResult</code> with the requested <code>PolicySet</code> or an error status
 */
private PolicyFinderResult<PolicySet> lookupPolicySetByIdentifier(IdReferenceMatch idReferenceMatch) {
    List<PolicySet> listCachedPolicySets = this.getFromPolicyMap(idReferenceMatch, PolicySet.class);
    if (listCachedPolicySets == null) {
        Identifier id = idReferenceMatch.getId();
        if (id != null) {
            URI uri = id.getUri();
            if (uri != null && uri.isAbsolute()) {
                PolicyDef policyDef = null;
                try {
                    policyDef = this.loadPolicyDefFromURI(uri);
                } catch (StdPolicyFinderException ex) {
                    return new StdPolicyFinderResult<PolicySet>(
                            new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, ex.getMessage()));
                }
                if (policyDef != null) {
                    if (policyDef instanceof PolicySet) {
                        List<PolicyDef> listPolicyDefs = new ArrayList<PolicyDef>();
                        listPolicyDefs.add(policyDef);
                        this.mapPolicies.put(id, listPolicyDefs);
                        this.mapPolicies.put(policyDef.getIdentifier(), listPolicyDefs);
                        return new StdPolicyFinderResult<PolicySet>((PolicySet) policyDef);
                    } else {
                        return PFR_NOT_A_POLICYSET;
                    }
                } else {
                    return PFR_POLICYSET_NOT_FOUND;
                }
            }
        }
    }
    if (listCachedPolicySets != null) {
        return new StdPolicyFinderResult<PolicySet>(this.getBestMatch(listCachedPolicySets));
    } else {
        return PFR_POLICYSET_NOT_FOUND;
    }
}

From source file:org.dita.dost.writer.SeparateChunkTopicParser.java

private URI resolve(final URI base, final String file) {
    assert base.isAbsolute();
    assert base.toString().startsWith(job.tempDirURI.toString());

    final FileInfo srcFi = job.getFileInfo(base);
    final URI dst;
    if (file != null) {
        dst = srcFi.result.resolve(file);
    } else {//w w  w . j a va 2s.  com
        dst = setPath(srcFi.result, srcFi.result.getPath() + FILE_EXTENSION_CHUNK);
    }
    final URI tmp = tempFileNameScheme.generateTempFileName(dst);

    if (job.getFileInfo(tmp) == null) {
        job.add(new FileInfo.Builder(srcFi).result(dst).uri(tmp).build());
    }

    return job.tempDirURI.resolve(tmp);
}

From source file:uk.co.flax.biosolr.ontology.core.owl.OWLOntologyHelper.java

/**
 * Construct a new ontology helper instance.
 *
 * @param ontologyUri the URI giving the location of the ontology.
 * @param config the ontology configuration, containing the property URIs
 * for labels, synonyms, etc./*from  w w  w .  j ava 2 s. c  o m*/
 * @throws URISyntaxException if the URI cannot be parsed.
 */
public OWLOntologyHelper(URI ontologyUri, OWLOntologyConfiguration config) throws URISyntaxException {
    this.config = config;

    if (!ontologyUri.isAbsolute()) {
        // Try to read as a file from the resource path
        LOGGER.debug("Ontology URI {} is not absolute - loading from classpath", ontologyUri);
        URL fileUrl = this.getClass().getClassLoader().getResource(ontologyUri.toString());
        if (fileUrl != null) {
            ontologyUri = fileUrl.toURI();
        } else {
            throw new URISyntaxException(ontologyUri.toString(), "Could not build URL for file");
        }
    }

    this.dataManager = new OWLDataManager(ontologyUri);
}

From source file:org.apache.ogt.http.impl.client.DefaultRedirectStrategy.java

public URI getLocationURI(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }// w w w .j a va 2 s  . com
    //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 = 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");
        }
        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.neo4j.server.helpers.CommunityServerBuilder.java

public CommunityServerBuilder withRelativeWebAdminUriPath(String webAdminUri) {
    try {//  ww w  .j  a v  a 2s .  c o m
        URI theUri = new URI(webAdminUri);
        if (theUri.isAbsolute()) {
            this.webAdminUri = theUri.getPath();
        } else {
            this.webAdminUri = theUri.toString();
        }
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    return this;
}

From source file:spaceRedirectStrategy.java

public URI getLocationURI(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }/*from   ww  w  .j av  a  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");
    }
    String location = locationHeader.getValue().replaceAll(" ", "%20");
    ;
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }

    URI uri = createLocationURI(location);

    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");
        }
        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:com.amos.tool.SelfRedirectStrategy.java

public URI getLocationURI(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    Args.notNull(request, "HTTP request");
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    //get the location header to find out where to redirect to
    final 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");
    }/*ww  w .  j  ava2  s  .c  o m*/
    final String location = locationHeader.getValue();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }

    final RequestConfig config = clientContext.getRequestConfig();

    URI uri = createLocationURI(location.replaceAll(" ", "%20"));

    // rfc2616 demands the location value be a complete URI
    // Location       = "Location" ":" absoluteURI
    try {
        if (!uri.isAbsolute()) {
            if (!config.isRelativeRedirectsAllowed()) {
                throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
            }
            // Adjust location URI
            final HttpHost target = clientContext.getTargetHost();
            Asserts.notNull(target, "Target host");
            final URI requestURI = new URI(request.getRequestLine().getUri());
            final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        }
    } catch (final URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
    }

    RedirectLocations redirectLocations = (RedirectLocations) clientContext
            .getAttribute(HttpClientContext.REDIRECT_LOCATIONS);
    if (redirectLocations == null) {
        redirectLocations = new RedirectLocations();
        context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
    }
    if (!config.isCircularRedirectsAllowed()) {
        if (redirectLocations.contains(uri)) {
            throw new CircularRedirectException("Circular redirect to '" + uri + "'");
        }
    }
    redirectLocations.add(uri);
    return uri;
}