io.zulia.util.HttpHelper Maven / Gradle / Ivy
package io.zulia.util;
import io.zulia.ZuliaConstants;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
public class HttpHelper {
public static String createQuery(HashMap parameters) {
StringBuilder sb = new StringBuilder();
for (String key : parameters.keySet()) {
Object value = parameters.get(key);
if (value instanceof String) {
if (sb.length() > 0) {
sb.append('&');
}
sb.append(key);
sb.append('=');
try {
sb.append(URLEncoder.encode((String) value, ZuliaConstants.UTF8));
}
catch (UnsupportedEncodingException e) {
//should not be possible
throw new RuntimeException(e);
}
}
else if (value instanceof List) {
List stringList = (List) value;
for (String item : stringList) {
if (sb.length() > 0) {
sb.append('&');
}
sb.append(key);
sb.append('=');
try {
sb.append(URLEncoder.encode(item, ZuliaConstants.UTF8));
}
catch (UnsupportedEncodingException e) {
//should not be possible
throw new RuntimeException(e);
}
}
}
}
return sb.toString();
}
public static String createRequestUrl(String server, int restPort, String url, HashMap parameters) {
String fullUrl = ("http://" + server + ":" + restPort + url);
if (parameters == null || parameters.isEmpty()) {
return fullUrl;
}
return (fullUrl + "?" + createQuery(parameters));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy