Support configuring multiple values in http header/query parameter/form-data #246
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package we.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -36,6 +37,10 @@ import org.springframework.util.MultiValueMap;
|
||||
*/
|
||||
public class MapUtil {
|
||||
|
||||
private static final String KEY = "key";
|
||||
|
||||
private static final String VALUE = "value";
|
||||
|
||||
public static HttpHeaders toHttpHeaders(Map<String, Object> params) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -65,7 +70,6 @@ public class MapUtil {
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
public static MultiValueMap<String, String> toMultiValueMap(Map<String, Object> params) {
|
||||
MultiValueMap<String, String> mvmap = new LinkedMultiValueMap<>();
|
||||
@@ -96,7 +100,7 @@ public class MapUtil {
|
||||
|
||||
return mvmap;
|
||||
}
|
||||
|
||||
|
||||
public static MultiValueMap<String, Object> toMultipartDataMap(Map<String, Object> params) {
|
||||
MultiValueMap<String, Object> mvmap = new LinkedMultiValueMap<>();
|
||||
if (params == null || params.isEmpty()) {
|
||||
@@ -124,15 +128,16 @@ public class MapUtil {
|
||||
return mvmap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract form data from multipart map exclude file
|
||||
*
|
||||
* @param params
|
||||
* @param fileKeyPrefix
|
||||
* @param filePartMap Map
|
||||
* @param filePartMap Map
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> extractFormData(MultiValueMap<String, Part> params, String fileKeyPrefix, Map<String, FilePart> filePartMap) {
|
||||
public static Map<String, Object> extractFormData(MultiValueMap<String, Part> params, String fileKeyPrefix,
|
||||
Map<String, FilePart> filePartMap) {
|
||||
HashMap<String, Object> m = new HashMap<>();
|
||||
if (params == null || params.isEmpty()) {
|
||||
return m;
|
||||
@@ -148,7 +153,7 @@ public class MapUtil {
|
||||
formFieldValues.add(p.value());
|
||||
} else if (part instanceof FilePart) {
|
||||
FilePart fp = (FilePart) part;
|
||||
String k = fileKeyPrefix + UUIDUtil.getUUID() + "-" + fp.filename();
|
||||
String k = fileKeyPrefix + UUIDUtil.getUUID() + "-" + fp.filename();
|
||||
formFieldValues.add(k);
|
||||
filePartMap.put(k, fp);
|
||||
}
|
||||
@@ -171,26 +176,28 @@ public class MapUtil {
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replace file field with FilePart object
|
||||
*
|
||||
* @param params
|
||||
* @param fileKeyPrefix
|
||||
* @param filePartMap
|
||||
*/
|
||||
public static void replaceWithFilePart(MultiValueMap<String, Object> params, String fileKeyPrefix, Map<String, FilePart> filePartMap) {
|
||||
public static void replaceWithFilePart(MultiValueMap<String, Object> params, String fileKeyPrefix,
|
||||
Map<String, FilePart> filePartMap) {
|
||||
if (params == null || params.isEmpty() || filePartMap == null || filePartMap.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
for (Entry<String, List<Object>> entry : params.entrySet()) {
|
||||
List<Object> list = entry.getValue();
|
||||
if (list != null && list.size() > 0) {
|
||||
List<Object> newlist = new ArrayList<>();
|
||||
List<Object> newlist = new ArrayList<>();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i).toString().startsWith(fileKeyPrefix)) {
|
||||
newlist.add(filePartMap.get(list.get(i).toString()));
|
||||
}else {
|
||||
} else {
|
||||
newlist.add(list.get(i));
|
||||
}
|
||||
}
|
||||
@@ -198,7 +205,7 @@ public class MapUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Object> toHashMap(MultiValueMap<String, String> params) {
|
||||
HashMap<String, Object> m = new HashMap<>();
|
||||
|
||||
@@ -219,7 +226,7 @@ public class MapUtil {
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Object> headerToHashMap(HttpHeaders headers) {
|
||||
HashMap<String, Object> m = new HashMap<>();
|
||||
|
||||
@@ -240,7 +247,7 @@ public class MapUtil {
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Object> upperCaseKey(Map<String, Object> m) {
|
||||
HashMap<String, Object> rs = new HashMap<>();
|
||||
|
||||
@@ -254,79 +261,96 @@ public class MapUtil {
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
public static MultiValueMap<String, Object> upperCaseKey(MultiValueMap<String, Object> m) {
|
||||
MultiValueMap<String, Object> rs = new LinkedMultiValueMap<>();
|
||||
|
||||
if (m == null || m.isEmpty()) {
|
||||
return rs;
|
||||
}
|
||||
|
||||
for (Entry<String, List<Object>> entry : m.entrySet()) {
|
||||
rs.put(entry.getKey().toUpperCase(), entry.getValue());
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value by path,support multiple levels,eg:a.b.c <br>
|
||||
* Do NOT use this method if field name contains a dot <br>
|
||||
* @param data
|
||||
*
|
||||
* @param data
|
||||
* @param path
|
||||
* @param value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void set(Map<String, Object> data, String path, Object value) {
|
||||
String[] fields = path.split("\\.");
|
||||
if(fields.length < 2) {
|
||||
if (fields.length < 2) {
|
||||
data.put(path, value);
|
||||
}else {
|
||||
} else {
|
||||
Map<String, Object> next = data;
|
||||
for (int i = 0; i < fields.length - 1; i++) {
|
||||
Map<String, Object> val = (Map<String, Object>) next.get(fields[i]);
|
||||
if(val == null) {
|
||||
if (val == null) {
|
||||
val = new HashMap<>();
|
||||
next.put(fields[i], val);
|
||||
}
|
||||
if(i == fields.length - 2) {
|
||||
val.put(fields[i+1], value);
|
||||
if (i == fields.length - 2) {
|
||||
val.put(fields[i + 1], value);
|
||||
break;
|
||||
}
|
||||
next = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get value by path, support multiple levels,eg:a.b.c <br>
|
||||
* Do NOT use this method if field name contains a dot <br>
|
||||
*
|
||||
* @param data
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
public static Object get(Map<String, Object> data, String path) {
|
||||
String[] fields = path.split("\\.");
|
||||
if(fields.length < 2) {
|
||||
if (fields.length < 2) {
|
||||
return data.get(path);
|
||||
}else {
|
||||
} else {
|
||||
Map<String, Object> next = data;
|
||||
for (int i = 0; i < fields.length - 1; i++) {
|
||||
if(!(next.get(fields[i]) instanceof Map)) {
|
||||
if (!(next.get(fields[i]) instanceof Map)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> val = (Map<String, Object>) next.get(fields[i]);
|
||||
if(val == null) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
}
|
||||
if(i == fields.length - 2) {
|
||||
return val.get(fields[i+1]);
|
||||
if (i == fields.length - 2) {
|
||||
return val.get(fields[i + 1]);
|
||||
}
|
||||
next = val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Merge maps, merge src to target
|
||||
*
|
||||
* @param target
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> merge(Map<String, Object> target, Map<String, Object> src) {
|
||||
if(src == null || src.isEmpty()) {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return target;
|
||||
}
|
||||
src.forEach((key, value) -> {
|
||||
if(value != null) {
|
||||
if (value != null) {
|
||||
target.merge(key, value, (oldValue, newValue) -> {
|
||||
if (oldValue instanceof Map && newValue instanceof Map) {
|
||||
oldValue = merge((Map<String, Object>) oldValue, (Map<String, Object>) newValue);
|
||||
@@ -338,5 +362,71 @@ public class MapUtil {
|
||||
});
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert list to map and merge multiple values<br>
|
||||
* Example: <br>
|
||||
* List as:<br>
|
||||
* [{"key": "abc", "value": "aaa"},{"key": "abc", "value": "xyz"},{"key":
|
||||
* "a123", "value": 666}]<br>
|
||||
* Merge Result:<br>
|
||||
* {"abc": ["aaa","xyz"], "a123": 666} <br>
|
||||
*
|
||||
* @param obj
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> list2Map(Object obj) {
|
||||
// Compatible with older version configuration
|
||||
if (obj instanceof Map) {
|
||||
return (Map<String, Object>) obj;
|
||||
}
|
||||
if (obj instanceof List) {
|
||||
Map<String, Object> rs = new HashMap<>();
|
||||
List<Map<String, Object>> list = (List<Map<String, Object>>) obj;
|
||||
if (list == null || list.size() == 0) {
|
||||
return rs;
|
||||
}
|
||||
for (Map<String, Object> m : list) {
|
||||
String k = m.get(KEY).toString();
|
||||
Object v = m.get(VALUE);
|
||||
if (rs.containsKey(k)) {
|
||||
List<Object> vals = null;
|
||||
if (rs.get(k) instanceof List) {
|
||||
vals = (List<Object>) rs.get(k);
|
||||
} else {
|
||||
vals = new ArrayList<>();
|
||||
vals.add(rs.get(k));
|
||||
}
|
||||
vals.add(v);
|
||||
rs.put(k, vals);
|
||||
} else {
|
||||
rs.put(k, v);
|
||||
}
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert list to MultiValueMap<br>
|
||||
* List format example:<br>
|
||||
* [{"key": "abc", "value": "aaa"},{"key": "abc", "value": "xyz"},,{"key":
|
||||
* "a123", "value": 666}]<br>
|
||||
*
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
public static MultiValueMap<String, Object> listToMultiValueMap(List<Map<String, Object>> list) {
|
||||
MultiValueMap<String, Object> mvmap = new LinkedMultiValueMap<>();
|
||||
if (list == null || list.size() == 0) {
|
||||
return mvmap;
|
||||
}
|
||||
for (Map<String, Object> m : list) {
|
||||
mvmap.add(m.get(KEY).toString(), m.get(VALUE));
|
||||
}
|
||||
return mvmap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user