f4a5f376794b9d762bcebde47262de90d00b8bf7.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.chinacreator.common.pipe;
  2. import com.chinacreator.common.exception.BusinessException;
  3. import com.chinacreator.common.util.AESUtil;
  4. import com.chinacreator.common.util.RSAUtil;
  5. import java.text.MessageFormat;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.commons.lang.math.NumberUtils;
  9. import net.sf.json.JSONSerializer;
  10. import net.sf.json.JsonConfig;
  11. public class DataOutPipe
  12. {
  13. private Map<String, Object> dataMap = new HashMap();
  14. private static JsonConfig jsonConfig = new JsonConfig();
  15. private String resultCode = "0";
  16. private String errorInfo = "";
  17. public static final String RSA = "RSA";
  18. public static final String AES = "AES";
  19. public void add(String key, Object value)
  20. {
  21. this.dataMap.put(key, value);
  22. }
  23. public String getResultCode() {
  24. return this.resultCode;
  25. }
  26. public String getErrorInfo() {
  27. return this.errorInfo;
  28. }
  29. public void setResultCode(String resultCode) {
  30. this.resultCode = resultCode;
  31. }
  32. public void setErrorInfo(String errorInfo) {
  33. this.errorInfo = errorInfo;
  34. }
  35. public void setException(Throwable e) {
  36. this.dataMap.clear();
  37. if ((e instanceof BusinessException)) {
  38. BusinessException be = (BusinessException)e;
  39. this.resultCode = be.getCode();
  40. this.errorInfo = getMessage(be.getMessage(), be.getParams());
  41. } else {
  42. e.printStackTrace();
  43. this.resultCode = "8000";
  44. this.errorInfo = e.getMessage();
  45. }
  46. }
  47. private static String getMessage(String message, String[] params) {
  48. String formatedMessage ="";
  49. if(message.contains("{") && message.contains("}") ){
  50. String value = message.substring(message.indexOf("{")+1, message.lastIndexOf("}"));
  51. if(NumberUtils.isNumber(value)){
  52. formatedMessage = MessageFormat.format(message, params);
  53. }else{
  54. formatedMessage = message;
  55. }
  56. }else{
  57. formatedMessage = MessageFormat.format(message, params);
  58. }
  59. return formatedMessage.replaceAll("\\{\\d\\}", "");
  60. }
  61. private String getJSON(Object value) {
  62. if (value == null)
  63. return null;
  64. if ((value instanceof String)) {
  65. return value.toString();
  66. }
  67. return JSONSerializer.toJSON(value, jsonConfig).toString();
  68. }
  69. public String toJSON() {
  70. this.dataMap.put("resultcode", this.resultCode);
  71. this.dataMap.put("errorinfo", this.errorInfo);
  72. return JSONSerializer.toJSON(this.dataMap, jsonConfig).toString();
  73. }
  74. public String toJSON(String password) {
  75. return toJSON(password, "AES");
  76. }
  77. public String toJSON(String password, String type) {
  78. try {
  79. for (String key : this.dataMap.keySet())
  80. this.dataMap.put(key, encode(password, getJSON(this.dataMap.get(key)), type));
  81. }
  82. catch (Throwable e) {
  83. setException(e);
  84. }
  85. this.dataMap.put("resultcode", this.resultCode);
  86. this.dataMap.put("errorinfo", this.errorInfo);
  87. return JSONSerializer.toJSON(this.dataMap, jsonConfig).toString();
  88. }
  89. private String encode(String password, String data, String type) throws Exception {
  90. if ("RSA".equals(type)) {
  91. return RSAUtil.encryptByPrivateKey(data, password);
  92. }
  93. return AESUtil.encrypt(data, password);
  94. }
  95. public Map<String, Object> getDataMap()
  96. {
  97. this.dataMap.put("resultcode", this.resultCode);
  98. this.dataMap.put("errorinfo", this.errorInfo);
  99. return this.dataMap;
  100. }
  101. public static void main(String[] args) {
  102. System.out.println(getMessage("用户订购鉴权产品查询失败:Value Not Found!{0}", null));
  103. }
  104. }