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

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";

    /**/*www .  j a  v  a2 s  . com*/
     * 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
     */
    public static URL buildUrl(String host, int port, String path,
            Map<String, String> parameters) throws MalformedURLException {
        // see: AuthUtilities.getSignature()
        // AuthUtilities.addAuthToken(parameters);

        StringBuffer buffer = new StringBuffer();
        if (!host.startsWith("http://")) {
            buffer.append("http://");
        }
        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());
    }
}

Related

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