Java URI to Base getBaseURI(final URI uri)

Here you can find the source of getBaseURI(final URI uri)

Description

Gets the base part (schema, host, port and path) of the specified URI.

License

Apache License

Parameter

Parameter Description
uri The URI. May be null .

Return

The base part of the URI, null if the original URI is null or doesn't specify a protocol.

Declaration

public static URI getBaseURI(final URI uri) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**// www  . j  a  v a2 s . c  o m
     * Gets the base part (schema, host, port and path) of the specified
     * URI.
     *
     * @param uri The URI. May be {@code null}.
     *
     * @return The base part of the URI, {@code null} if the original URI
     *         is {@code null} or doesn't specify a protocol.
     */
    public static URI getBaseURI(final URI uri) {

        if (uri == null)
            return null;

        try {
            return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);

        } catch (URISyntaxException e) {

            return null;
        }
    }
}

Related

  1. getBase(final URI uri)
  2. getBaseName(URI uri)
  3. getBaseURI()
  4. getBaseURI()