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

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

Description

build Url

License

Apache License

Declaration

public static String buildUrl(final String baseUrl, final Map<String, String> params, final String... parts)
            throws MalformedURLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 John Casey//  w w w . java  2  s.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;
import java.util.Map;

public class Main {
    public static String buildUrl(final String baseUrl, final String... parts) throws MalformedURLException {
        return buildUrl(baseUrl, null, parts);
    }

    public static String buildUrl(final String baseUrl, final Map<String, String> params, final String... parts)
            throws MalformedURLException {
        if (parts == null || parts.length < 1) {
            return baseUrl;
        }

        final StringBuilder urlBuilder = new StringBuilder();

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

        for (String part : parts) {
            if (part.startsWith("/")) {
                part = part.substring(1);
            }

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

            urlBuilder.append(part);
        }

        if (params != null && !params.isEmpty()) {
            urlBuilder.append("?");
            boolean first = true;
            for (final Map.Entry<String, String> param : params.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    urlBuilder.append("&");
                }

                urlBuilder.append(param.getKey()).append("=").append(param.getValue());
            }
        }

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

Related

  1. buildUrl(final String baseUrl, final String basePath, final String[] parts)
  2. buildUrl(final String baseUrl, final String... parts)
  3. buildUrl(final String domain, final String path)
  4. buildUrl(String base, Map params)