
com.github.dadiyang.httpinvoker.util.ParamUtils Maven / Gradle / Ivy
package com.github.dadiyang.httpinvoker.util;
import com.github.dadiyang.httpinvoker.requestor.HttpRequest;
import com.github.dadiyang.httpinvoker.requestor.MultiPart;
import com.github.dadiyang.httpinvoker.serializer.JsonSerializerDecider;
import java.io.*;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.util.*;
/**
* utils for handling param
*
* @author dadiyang
* @since 1.1.2
*/
public class ParamUtils {
private static final char UPPER_A = 'A';
private static final char UPPER_Z = 'Z';
private static final char LOWER_A = 'a';
private static final char LOWER_Z = 'z';
private static final String FILE_NAME = "fileName";
private static final String DEFAULT_UPLOAD_FORM_KEY = "media";
private static final String FORM_KEY = "formKey";
private static final List> BASIC_TYPE = Arrays.asList(Byte.class, Short.class,
Integer.class, Long.class, Float.class, Double.class, Character.class,
Boolean.class, String.class, Void.class, Date.class);
/**
* for JDK6/7 compatibility
*/
private static final List BASIC_TYPE_NAME = Arrays.asList("java.time.LocalDate", "java.time.LocalDateTime");
private ParamUtils() {
throw new UnsupportedOperationException("utils should not be initialized!");
}
/**
* check if the clz is primary type, primary type's wrapper, String or Void
*
* @param clz the type
* @return check if the clz is basic type
*/
public static boolean isBasicType(Class> clz) {
if (clz == null) {
return false;
}
// for JDK6/7 compatibility
if (BASIC_TYPE_NAME.contains(clz.getName())) {
return true;
}
return clz.isPrimitive() || BASIC_TYPE.contains(clz);
}
/**
* check if the arg is a collection
*
* @param arg object to be checked
* @return if the arg is a array/collection
*/
public static boolean isCollection(Object arg) {
if (arg == null) {
return false;
}
return arg.getClass().isArray()
|| arg instanceof Collection;
}
/**
* convert an object to Map<String, String>
*
* @param value object to be converted
* @param prefix key's prefix
* @return Map<String, String> represent the value
*/
public static Map toMapStringString(Object value, String prefix) {
if (value == null) {
return Collections.emptyMap();
}
if (isBasicType(value.getClass())) {
return Collections.singletonMap(prefix, String.valueOf(value));
}
Map map = new HashMap();
if (value.getClass().isArray()) {
for (int i = 0; i < Array.getLength(value); i++) {
String key = prefix + "[" + i++ + "]";
Object item = Array.get(value, 0);
if (isBasicType(item.getClass())) {
map.put(prefix + "[" + i + "]", String.valueOf(item));
} else {
map.putAll(toMapStringString(item, key));
}
}
} else if (value instanceof Collection) {
Collection collection = (Collection) value;
Iterator it = collection.iterator();
int i = 0;
while (it.hasNext()) {
String key = prefix + "[" + i++ + "]";
Object item = it.next();
if (isBasicType(item.getClass())) {
map.put(prefix + "[" + i++ + "]", String.valueOf(item));
} else {
map.putAll(toMapStringString(item, key));
}
}
} else {
Map obj = JsonSerializerDecider.getJsonSerializer().toMap(JsonSerializerDecider.getJsonSerializer().serialize(value));
for (Map.Entry entry : obj.entrySet()) {
String key;
if (prefix == null || prefix.isEmpty()) {
key = entry.getKey();
} else {
key = prefix + "[" + entry.getKey() + "]";
}
if (isBasicType(value.getClass())) {
map.put(key, String.valueOf(value));
} else {
map.putAll(toMapStringString(entry.getValue(), key));
}
}
}
return map;
}
/**
* convert param object to query string
*
* collection fields will be convert to a form of duplicated key such as id=1&id=2&id=3
*
* @param arg the param args
* @return query string
*/
public static String toQueryString(Object arg) {
if (arg == null) {
return "";
}
StringBuilder qs = new StringBuilder("?");
Map obj = JsonSerializerDecider.getJsonSerializer().toMap(JsonSerializerDecider.getJsonSerializer().serialize(arg));
for (Map.Entry entry : obj.entrySet()) {
if (isCollection(entry.getValue())) {
qs.append(collectionToQueryString(obj, entry));
} else {
String value = entry.getValue() == null ? "" : entry.getValue().toString();
try {
value = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
}
qs.append(entry.getKey()).append("=").append(value).append("&");
}
}
return qs.substring(0, qs.length() - 1);
}
private static String collectionToQueryString(Map obj, Map.Entry entry) {
List