Java URL Build buildUrlAsString(String scheme, String host, int port, String path)

Here you can find the source of buildUrlAsString(String scheme, String host, int port, String path)

Description

Build a fully qualified URL from the pieces provided.

License

Open Source License

Parameter

Parameter Description
scheme server protocol.
host server name.
port server port.
path properly encoded relative URL.

Exception

Parameter Description
IllegalArgumentExceptionif scheme, host or port is null.

Declaration

public static String buildUrlAsString(String scheme, String host, int port, String path) 

Method Source Code

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

public class Main {
    /**
     * Build a fully qualified URL from the pieces provided. This method does not call any encoding methods, so the path
     * argument has to be properly encoded.
     *
     * @param   scheme  server protocol.
     * @param   host    server name.
     * @param   port    server port.
     * @param   path    properly encoded relative URL.
     *
     * @throws  IllegalArgumentException  if scheme, host or port is null.
     */
    public static String buildUrlAsString(String scheme, String host, int port, String path) {

        if ((scheme == null) || scheme.equals("") || (host == null) || host.equals("") || (port == 0)) {
            throw new IllegalArgumentException("Cannot build a URL using following scheme: " + scheme + " host: "
                    + host + " port: " + port + " path: " + path);
        }

        StringBuilder url = new StringBuilder(200);

        url.append(scheme).append("://").append(host);

        // check for protocol default port number
        if ((scheme.equalsIgnoreCase("http") && (port != 80))
                || (scheme.equalsIgnoreCase("https") && (port != 443))) {
            url.append(":").append(port);
        }

        if (path != null) {
            url.append(path);
        }

        return url.toString();
    }
}

Related

  1. buildUrl(String protocol, String host, int port, String context)
  2. buildUrl(String scheme, String base, String path, String query)
  3. buildUrl(String scheme, String host, int port)
  4. buildUrl(String uri)
  5. buildUrl(String[] indexNames, String[] types, String operationOrId)
  6. buildURLForParams(String... urlParams)
  7. buildURLForParamsWithPrefix(String prefix, String... urlParams)
  8. buildURLForPath(String[] _path)
  9. buildUrlFromParts(String... parts)