Java URL Create buildUrl(String host, int port, String path, Map parameters)

Here you can find the source of buildUrl(String host, int port, String path, Map parameters)

Description

Build a request URL.

License

Open Source License

Parameter

Parameter Description
host The host
port The port
path The path
parameters The parameters

Exception

Parameter Description
MalformedURLException an exception

Return

The URL

Declaration

@Deprecated
public static URL buildUrl(String host, int port, String path, Map<String, String> parameters)
        throws MalformedURLException 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class Main {
    public static final String UTF8 = "UTF-8";

    /**/*from w  w w.jav  a2 s  .  c  o  m*/
     * Build a request URL.
     * 
     * @param host
     *            The host
     * @param port
     *            The port
     * @param path
     *            The path
     * @param parameters
     *            The parameters
     * @return The URL
     * @throws MalformedURLException
     * @deprecated use {@link #buildSecureUrl(java.lang.String, int, java.lang.String, java.util.Map) }
     */
    @Deprecated
    public static URL buildUrl(String host, int port, String path, Map<String, String> parameters)
            throws MalformedURLException {
        return buildUrl("http", port, path, parameters);
    }

    /**
     * Build a request URL using a given scheme.
     * 
     * @param scheme the scheme, either {@code http} or {@code https}
     * @param host
     *            The host
     * @param port
     *            The port
     * @param path
     *            The path
     * @param parameters
     *            The parameters
     * @return The URL
     * @throws MalformedURLException
     */
    public static URL buildUrl(String scheme, String host, int port, String path, Map<String, String> parameters)
            throws MalformedURLException {
        checkSchemeAndPort(scheme, port);
        StringBuilder buffer = new StringBuilder();
        if (!host.startsWith(scheme + "://")) {
            buffer.append(scheme).append("://");
        }
        buffer.append(host);
        if (port > 0) {
            buffer.append(':');
            buffer.append(port);
        }
        if (path == null) {
            path = "/";
        }
        buffer.append(path);

        if (!parameters.isEmpty()) {
            buffer.append('?');
        }
        int size = parameters.size();
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            buffer.append(entry.getKey());
            buffer.append('=');
            Object value = entry.getValue();
            if (value != null) {
                String string = value.toString();
                try {
                    string = URLEncoder.encode(string, UTF8);
                } catch (UnsupportedEncodingException e) {
                    // Should never happen, but just in case
                }
                buffer.append(string);
            }
            if (--size != 0) {
                buffer.append('&');
            }
        }

        /*
         * RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = requestContext.getAuth(); if (auth != null &&
         * !ignoreMethod(getMethod(parameters))) { buffer.append("&api_sig="); buffer.append(AuthUtilities.getSignature(sharedSecret, parameters)); }
         */

        return new URL(buffer.toString());
    }

    private static void checkSchemeAndPort(String scheme, int port) {
        checkScheme(scheme);
        /*
         * Be liberal about accepting non-default ports, but strict
         * about mismatching scheme and default port.
         */
        if ("http".equals(scheme) && port == 443) {
            throw new IllegalArgumentException("port 443 is invalid with http scheme");
        }
        if ("https".equals(scheme) && port == 80) {
            throw new IllegalArgumentException("port 80 is invalid with https scheme");
        }
    }

    private static void checkScheme(String scheme) {
        if (scheme == null || !("http".equals(scheme) || "https".equals(scheme))) {
            throw new IllegalArgumentException("scheme must be http or https");
        }
    }
}

Related

  1. buildUrl(final String domain, final String path)
  2. buildUrl(String base, Map params)
  3. buildUrl(String baseUrl, Map queryParams, String anchor)
  4. buildUrl(String context, String... relocations)
  5. buildUrl(String context, String... relocations)
  6. buildUrl(String host, int port, String path, Map parameters)
  7. buildURL(String host, String path, Map params)
  8. buildURL(String id)
  9. buildUrl(String repositoryUrl, String resourceUrl)