Java URI to Host Name getHost(URI uri)

Here you can find the source of getHost(URI uri)

Description

get Host

License

Open Source License

Declaration

public final static String getHost(URI uri) 

Method Source Code

//package com.java2s;
/*/*from  w w w.  ja va  2s  . c o  m*/
 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

import java.net.URI;

public class Main {
    public final static String getHost(URI uri) {
        String host = uri.getHost();
        if (host == null) {
            host = uri.getAuthority();
        }
        return host;
    }

    public final static String getAuthority(URI uri) {
        String url = uri.getAuthority();
        int port = uri.getPort();
        if (port == -1) {
            port = getPort(uri);
            url += ":" + port;
        }
        return url;
    }

    public final static int getPort(URI uri) {
        int port = uri.getPort();
        if (port == -1)
            port = uri.getScheme().equals("http") ? 80 : 443;
        return port;
    }
}

Related

  1. getHost(final URI uri)
  2. getHost(URI uri)
  3. getHost(URI uri)
  4. getHostAddress(final URI uri)
  5. getHostAddress(final URI uri)
  6. getHostAndPort(URI u)