Java URI Create createAbsoluteURI(URI base, String uri)

Here you can find the source of createAbsoluteURI(URI base, String uri)

Description

Guarantees that the returned URI is absolute, no matter what the argument is.

License

CDDL license

Parameter

Parameter Description
base An absolute base URI, can be null!
uri A string that either represents a relative or an already absolute URI

Exception

Parameter Description
IllegalArgumentException If the base URI is null and the given URI string is not absolute

Return

An absolute URI

Declaration

public static URI createAbsoluteURI(URI base, String uri) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**//from  w  w  w  . j ava2  s  . c  o  m
 * Copyright (C) 2014 4th Line GmbH, Switzerland and others
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License Version 1 or later
 * ("CDDL") (collectively, the "License"). You may not use this file
 * except in compliance with the License. See LICENSE.txt for more
 * information.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.net.*;

public class Main {
    /**
     * Guarantees that the returned URI is absolute, no matter what the argument is.
     *
     * @param base An absolute base URI, can be null!
     * @param uri  A string that either represents a relative or an already absolute URI
     * @return An absolute URI
     * @throws IllegalArgumentException If the base URI is null and the given URI string is not absolute
     */
    public static URI createAbsoluteURI(URI base, String uri) throws IllegalArgumentException {
        return createAbsoluteURI(base, URI.create(uri));
    }

    public static URI createAbsoluteURI(URI base, URI relativeOrNot) throws IllegalArgumentException {
        if (base == null && !relativeOrNot.isAbsolute()) {
            throw new IllegalArgumentException("Base URI is null and given URI is not absolute");
        } else if (base == null && relativeOrNot.isAbsolute()) {
            return relativeOrNot;
        } else {
            assert base != null;
            // If the given base URI has no path we give it a root path
            if (base.getPath().length() == 0) {
                try {
                    base = new URI(base.getScheme(), base.getAuthority(), "/", base.getQuery(), base.getFragment());
                } catch (Exception ex) {
                    throw new IllegalArgumentException(ex);
                }
            }
            return base.resolve(relativeOrNot);
        }
    }
}

Related

  1. addQueryString(final URI uri, final String queryString)
  2. convertToURI(String uriOrPath)
  3. convertToURI(String uriString, Map aliasMap)
  4. convertToURI(String value)
  5. create(final String uri)
  6. createConnection(final String uri, final String authValue)
  7. createDatastream(String registrationUri, String APIKey, String xmlDescription)
  8. createDefaultName(final String uri)
  9. createExtensionUri(final String ext)