package com.chinacreator.common.pipe; import com.chinacreator.common.exception.BusinessException; import com.chinacreator.common.util.AESUtil; import com.chinacreator.common.util.RSAUtil; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.math.NumberUtils; import net.sf.json.JSONSerializer; import net.sf.json.JsonConfig; public class DataOutPipe { private Map dataMap = new HashMap(); private static JsonConfig jsonConfig = new JsonConfig(); private String resultCode = "0"; private String errorInfo = ""; public static final String RSA = "RSA"; public static final String AES = "AES"; public void add(String key, Object value) { this.dataMap.put(key, value); } public String getResultCode() { return this.resultCode; } public String getErrorInfo() { return this.errorInfo; } public void setResultCode(String resultCode) { this.resultCode = resultCode; } public void setErrorInfo(String errorInfo) { this.errorInfo = errorInfo; } public void setException(Throwable e) { this.dataMap.clear(); if ((e instanceof BusinessException)) { BusinessException be = (BusinessException)e; this.resultCode = be.getCode(); this.errorInfo = getMessage(be.getMessage(), be.getParams()); } else { e.printStackTrace(); this.resultCode = "8000"; this.errorInfo = e.getMessage(); } } private static String getMessage(String message, String[] params) { String formatedMessage =""; if(message.contains("{") && message.contains("}") ){ String value = message.substring(message.indexOf("{")+1, message.lastIndexOf("}")); if(NumberUtils.isNumber(value)){ formatedMessage = MessageFormat.format(message, params); }else{ formatedMessage = message; } }else{ formatedMessage = MessageFormat.format(message, params); } return formatedMessage.replaceAll("\\{\\d\\}", ""); } private String getJSON(Object value) { if (value == null) return null; if ((value instanceof String)) { return value.toString(); } return JSONSerializer.toJSON(value, jsonConfig).toString(); } public String toJSON() { this.dataMap.put("resultcode", this.resultCode); this.dataMap.put("errorinfo", this.errorInfo); return JSONSerializer.toJSON(this.dataMap, jsonConfig).toString(); } public String toJSON(String password) { return toJSON(password, "AES"); } public String toJSON(String password, String type) { try { for (String key : this.dataMap.keySet()) this.dataMap.put(key, encode(password, getJSON(this.dataMap.get(key)), type)); } catch (Throwable e) { setException(e); } this.dataMap.put("resultcode", this.resultCode); this.dataMap.put("errorinfo", this.errorInfo); return JSONSerializer.toJSON(this.dataMap, jsonConfig).toString(); } private String encode(String password, String data, String type) throws Exception { if ("RSA".equals(type)) { return RSAUtil.encryptByPrivateKey(data, password); } return AESUtil.encrypt(data, password); } public Map getDataMap() { this.dataMap.put("resultcode", this.resultCode); this.dataMap.put("errorinfo", this.errorInfo); return this.dataMap; } public static void main(String[] args) { System.out.println(getMessage("用户订购鉴权产品查询失败:Value Not Found!{0}", null)); } }