packageutil;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.Date;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.Map;importjava.util.Map.Entry;/***
*
*@authorYangChaoJie
**/
public classReqUtil {/***
* 将请求对象传入,进行对象转Map,排序,进行MD5加密;
*
*@paramreq
*@return
*/
private staticStringBuilder stringBuilder;private staticString currTime;/***
* hashCode加密字符串
*
*@return
*/
public static LinkedHashMapParameToMap(Object reqData) {
stringBuilder= newStringBuilder();
stringBuilder.append("he3yun.com-");
@SuppressWarnings("unchecked")
LinkedHashMap map =ZJ_BeanToMapUtils.toMap(reqData);
List> list = new ArrayList>(map.entrySet());
Collections.sort(list,new Comparator>() {public int compare(Entry o1, Entryo2) {returnString.valueOf(o1.getValue()).toLowerCase()
.compareTo(String.valueOf(o2.getValue()).toLowerCase());
}
});returnCheckData(reqData, list);
}/***
* 根据不同的构造方法参数生成对应Map对象,进行加密
*
*@paramreqData
*@paramlist
*@return
*/
private static LinkedHashMap CheckData(Object reqData, List>list) {
LinkedHashMap dataMap = new LinkedHashMap();returntraverseEntryToMap(reqData, dataMap, list);
}/***
* 提高复用写的方法
*
*@paramdataMap
*@paramlist
*@return
*/
private static LinkedHashMaptraverseEntryToMap(Object reqData,
LinkedHashMap dataMap, List>list) {for (Entrymapping : list) {if (mapping.getValue() != null) {
stringBuilder.append(String.valueOf(mapping.getValue())+ "-");
dataMap.put(mapping.getKey(), mapping.getValue());
}
}
getCurrTime();
stringBuilder.append(currTime);
dataMap.put("hashCode", MD5Util.stringToMD5(String.valueOf(stringBuilder)));
dataMap.put("timestamp", currTime);returndataMap;
}public static voidgetCurrTime() {
currTime= String.valueOf(newDate().getTime());
}/*** MAP排序
*@parammap*/
public static LinkedHashMap sortMap(LinkedHashMapmap) {
List> list = new ArrayList>(map.entrySet());
Collections.sort(list,new Comparator>() {public int compare(Entry o1, Entryo2) {returnString.valueOf(o1.getValue()).toLowerCase()
.compareTo(String.valueOf(o2.getValue()).toLowerCase());
}
});returnmap;
}
}