Java URI Parse parseURI(String connectionString, URI defaultURI)

Here you can find the source of parseURI(String connectionString, URI defaultURI)

Description

Parses the URL provided by the user and

License

Open Source License

Declaration

public static URI parseURI(String connectionString, URI defaultURI) 

Method Source Code


//package com.java2s;
/*/*from   ww w .j  a va 2s.c  o m*/
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

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

public class Main {
    /**
     * Parses the URL provided by the user and
     */
    public static URI parseURI(String connectionString, URI defaultURI) {
        final URI uri = parseWithNoScheme(connectionString);
        final String path = "".equals(uri.getPath()) ? defaultURI.getPath() : uri.getPath();
        final String query = uri.getQuery() == null ? defaultURI.getQuery() : uri.getQuery();
        final int port = uri.getPort() < 0 ? defaultURI.getPort() : uri.getPort();
        try {
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, path, query,
                    defaultURI.getFragment());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(
                    "Invalid connection configuration [" + connectionString + "]: " + e.getMessage(), e);
        }
    }

    private static URI parseWithNoScheme(String connectionString) {
        URI uri;
        // check if URI can be parsed correctly without adding scheme
        // if the connection string is in format host:port or just host, the host is going to be null
        // if the connection string contains IPv6 localhost [::1] the parsing will fail
        URISyntaxException firstException = null;
        try {
            uri = new URI(connectionString);
            if (uri.getHost() == null || uri.getScheme() == null) {
                uri = null;
            }
        } catch (URISyntaxException e) {
            firstException = e;
            uri = null;
        }

        if (uri == null) {
            // We couldn't parse URI without adding scheme, let's try again with scheme this time
            try {
                return new URI("http://" + connectionString);
            } catch (URISyntaxException e) {
                IllegalArgumentException ie = new IllegalArgumentException(
                        "Invalid connection configuration [" + connectionString + "]: " + e.getMessage(), e);
                if (firstException != null) {
                    ie.addSuppressed(firstException);
                }
                throw ie;
            }
        } else {
            // We managed to parse URI and all necessary pieces are present, let's make sure the scheme is correct
            if ("http".equals(uri.getScheme()) == false && "https".equals(uri.getScheme()) == false) {
                throw new IllegalArgumentException("Invalid connection configuration [" + connectionString
                        + "]: Only http and https protocols are supported");
            }
            return uri;
        }
    }
}

Related

  1. parseRepositoryItemUri(URI uri)
  2. parseServiceName(URI endpoint)
  3. parseServiceName(URI endpoint)
  4. parseURI(final String string)
  5. parseURI(final String value)
  6. parseUri(String s)
  7. parseURI(String target)
  8. parseURI(String uriStr)
  9. parseUriParameters(URI uri)