Java URL Create buildUrl(final String baseUrl, final String... parts)

Here you can find the source of buildUrl(final String baseUrl, final String... parts)

Description

Given a base URL and some path fragments, build a complete, valid URL

License

Apache License

Parameter

Parameter Description
baseUrl The base URL
parts the part of the path to append, with path separator characters inserted as appropriate

Exception

Parameter Description
MalformedURLException In case the inputs don't form a valid URL

Return

The validate, concatenated URL

Declaration

public static String buildUrl(final String baseUrl, final String... parts) throws MalformedURLException 

Method Source Code

//package com.java2s;
/**//from w ww . j  a v  a 2  s  .com
 * Copyright (C) 2015 Red Hat, Inc. (jcasey@redhat.com)
 *
 * 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.
 */

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Given a base URL and some path fragments, build a complete, valid URL
     * @param baseUrl The base URL
     * @param parts the part of the path to append, with path separator characters inserted as appropriate
     * @return The validate, concatenated URL
     * @throws MalformedURLException In case the inputs don't form a valid URL
     */
    public static String buildUrl(final String baseUrl, final String... parts) throws MalformedURLException {
        if (parts == null || parts.length < 1) {
            return baseUrl;
        }

        final StringBuilder urlBuilder = new StringBuilder();

        if (parts[0] == null || !parts[0].startsWith(baseUrl)) {
            urlBuilder.append(baseUrl);
        }

        for (String part : parts) {
            if (part == null || part.trim().length() < 1) {
                continue;
            }

            if (part.startsWith("/")) {
                part = part.substring(1);
            }

            if (urlBuilder.length() > 0 && urlBuilder.charAt(urlBuilder.length() - 1) != '/') {
                urlBuilder.append("/");
            }

            urlBuilder.append(part);
        }

        return new URL(urlBuilder.toString()).toExternalForm();
    }
}

Related

  1. buildUrl(final String baseUrl, final Map params, final String... parts)
  2. buildUrl(final String baseUrl, final String basePath, final String[] parts)
  3. buildUrl(final String domain, final String path)
  4. buildUrl(String base, Map params)
  5. buildUrl(String baseUrl, Map queryParams, String anchor)
  6. buildUrl(String context, String... relocations)