All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.espertech.esperio.http.core.URIUtil Maven / Gradle / Ivy

There is a newer version: 9.0.0
Show newest version
package com.espertech.esperio.http.core;

import java.net.URI;
import java.net.URLEncoder;
import java.util.Map;
import java.util.LinkedHashMap;
import java.io.UnsupportedEncodingException;

public class URIUtil {

    public static URI withQuery(URI uri, String... params) {
        Map map = new LinkedHashMap();
        for (int i = 0; i < params.length; i += 2) {
            String key = params[i];
            String val = i + 1 < params.length ? params[i + 1] : "";
            map.put(key, val);
        }
        return withQuery(uri, map);
    }

    public static URI withQuery(URI uri, Map params) {
        StringBuilder query = new StringBuilder();
        char separator = '?';
        for (Map.Entry param : params.entrySet()) {
            query.append(separator);
            separator = '&';
            try {
                query.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                if (param.getValue().length() != 0) {
                    query.append('=');
                    query.append(URLEncoder.encode(param.getValue(), "UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return URI.create(uri.toString() + query.toString());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy