QueryString(parameter, formdata) 관련 Util 클래스 추가
This commit is contained in:
@@ -0,0 +1,108 @@
|
|||||||
|
package com.eactive.eai.util;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
|
public class QueryStringUtils {
|
||||||
|
|
||||||
|
private QueryStringUtils() {
|
||||||
|
throw new IllegalStateException("Utility class");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String[]> parseQueryString(String queryString, String charset) throws UnsupportedEncodingException {
|
||||||
|
return parseQueryString(queryString, charset, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String[]> parseQueryString(String queryString) throws UnsupportedEncodingException {
|
||||||
|
return parseQueryString(queryString, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String[]> parseQueryString(String queryString, String charset, boolean urlDecode) throws UnsupportedEncodingException {
|
||||||
|
Map<String, String[]> paramMap = new HashMap<>();
|
||||||
|
Map<String, List<String>> tempMap = new HashMap<>();
|
||||||
|
|
||||||
|
if(StringUtils.isEmpty(queryString))
|
||||||
|
return paramMap;
|
||||||
|
|
||||||
|
String[] pairs = queryString.split("&");
|
||||||
|
for(String pair : pairs) {
|
||||||
|
|
||||||
|
if(StringUtils.isEmpty(pair))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int idx = pair.indexOf("=");
|
||||||
|
String key = null;
|
||||||
|
String value = null;
|
||||||
|
if(idx > 0) {
|
||||||
|
if(urlDecode)
|
||||||
|
key = URLDecoder.decode(pair.substring(0, idx), charset);
|
||||||
|
else
|
||||||
|
if(charset != null)
|
||||||
|
key = new String((pair.substring(0, idx)).getBytes(charset));
|
||||||
|
else
|
||||||
|
key = pair.substring(0, idx);
|
||||||
|
|
||||||
|
|
||||||
|
if(urlDecode)
|
||||||
|
value = URLDecoder.decode(pair.substring(idx + 1), charset);
|
||||||
|
else
|
||||||
|
if(charset != null)
|
||||||
|
value = new String((pair.substring(idx + 1)).getBytes(charset));
|
||||||
|
else
|
||||||
|
value = pair.substring(idx + 1);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
key = urlDecode ? URLDecoder.decode(pair, charset) : new String(pair.getBytes(charset));
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
tempMap.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Map.Entry<String, List<String>> entry : tempMap.entrySet()) {
|
||||||
|
paramMap.put(entry.getKey(), entry.getValue().toArray(new String[0]));
|
||||||
|
}
|
||||||
|
return paramMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String makeJson(Map<String, String[]> paramMap, boolean bUrlDecode) throws UnsupportedEncodingException {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("{");
|
||||||
|
int i = 0;
|
||||||
|
for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
|
||||||
|
if (i > 0) {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
sb.append("\"").append(entry.getKey()).append("\":");
|
||||||
|
String[] values = entry.getValue();
|
||||||
|
if (values.length > 1) {
|
||||||
|
// ["111", "222"]
|
||||||
|
sb.append("[");
|
||||||
|
for (int j = 0; j < values.length; j++) {
|
||||||
|
if (j > 0) {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
String value = bUrlDecode ? URLDecoder.decode(values[j], StandardCharsets.UTF_8.name()) : values[j];
|
||||||
|
sb.append("\"").append(JSONValue.escape(value)).append("\"");
|
||||||
|
}
|
||||||
|
sb.append("]");
|
||||||
|
} else {
|
||||||
|
String value = bUrlDecode ? URLDecoder.decode(values[0], StandardCharsets.UTF_8.name()) : values[0];
|
||||||
|
sb.append("\"").append(JSONValue.escape(value)).append("\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append("}");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user