OpenApiV1.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.iamberry.nuonuo.service;
  2. import com.iamberry.nuonuo.bean.RequestMode;
  3. import com.iamberry.nuonuo.exception.ExConstants;
  4. import com.iamberry.nuonuo.exception.OpensnsException;
  5. import com.iamberry.nuonuo.util.SecurityUtil;
  6. import com.iamberry.nuonuo.util.StateData;
  7. import com.iamberry.nuonuo.util.ValidataUtil;
  8. import net.sf.json.xml.XMLSerializer;
  9. import org.json.JSONObject;
  10. import java.util.Map;
  11. /**
  12. * 提供访问爱信诺开放平台 OpenApiV1 的接口
  13. * @author sdk.jss.com.cn
  14. * @version 2.0
  15. * @since jdk1.6
  16. */
  17. public class OpenApiV1{
  18. /**
  19. * 执行API调用
  20. * @param serverName 服务器的地址
  21. * @param headers 请求头参数
  22. * @param requestMode 请求体model
  23. * @return 返回服务器响应内容
  24. * @throws OpensnsException
  25. */
  26. public String handle(String serverName, Map<String, String> headers, RequestMode requestMode) throws OpensnsException {
  27. // 客户端请求的数据进行基本校验
  28. requestDataValidata(serverName, headers, requestMode);
  29. String dataType = (String) headers.get("dataType");
  30. String signMethod = (String) headers.get("signMethod");
  31. String clientReqParam = "";
  32. String aesParams = "";
  33. try {
  34. if ("json".equalsIgnoreCase(dataType)) {
  35. clientReqParam = installJsonStr(requestMode, clientReqParam);
  36. } else if ("xml".equalsIgnoreCase(dataType)) {
  37. clientReqParam = installXmlStr(requestMode);
  38. }
  39. } catch (Exception e) {
  40. throw new OpensnsException(ExConstants.input_Data_Type, ExConstants.input_Data_Type_Msg);
  41. }
  42. // System.out.println("客户端组装的参数格式-->:" + clientReqParam);
  43. try {
  44. // 每个APP密钥不同,根据当前APP赋值相应密钥值
  45. aesParams = (String) SecurityUtil.AESEncrypt(clientReqParam, StateData.app_secret);
  46. } catch (Exception e) {
  47. throw new OpensnsException(ExConstants.security_Decryption, ExConstants.security_Encryption_Msg);
  48. }
  49. // 发送请求(客户端可以自定义自己的请求)
  50. HttpClientService httpService = new HttpClientService();
  51. String result = httpService.sendSyncSingleHttp(serverName, headers, aesParams);
  52. // 如果返回值为密文,此处进行AES解密操作
  53. if ("AES/AES".equalsIgnoreCase(signMethod)) { //{"result":"","code":"E9301","describe":"企业信息备案错误,请联系诺诺网"}
  54. result = String.valueOf(SecurityUtil.AESDecrypt(result, StateData.app_secret));
  55. }
  56. return result;
  57. }
  58. /**
  59. * 客户端请求数据校验
  60. * @param headers
  61. * @param requestMode
  62. * @throws OpensnsException
  63. */
  64. private void requestDataValidata(String serverName, Map<String, String> headers, RequestMode requestMode) throws OpensnsException {
  65. if (ValidataUtil.isEmpty(serverName) || (headers == null || headers.isEmpty()) || requestMode == null
  66. || requestMode.getPublic() == null || requestMode.getPrivate() == null) {
  67. throw new OpensnsException(ExConstants.input_Data_Null, ExConstants.input_Data_Null_Msg);
  68. }
  69. // 检查header参数不为空
  70. if (ValidataUtil.isEmpty(headers.get("appKey")) // appkey
  71. || ValidataUtil.isEmpty(headers.get("dataType"))// 数据格式<xml/json>
  72. || ValidataUtil.isEmpty(headers.get("accessToken"))// 授权码
  73. || ValidataUtil.isEmpty(headers.get("appRate"))) {
  74. throw new OpensnsException(ExConstants.input_Data_Type, ExConstants.input_Data_Type_Msg1);
  75. }
  76. // 检查加密算法
  77. if (!valiSignMethod(String.valueOf(headers.get("signMethod")))) {
  78. throw new OpensnsException(ExConstants.security_Decryption_arithmeticNULL,
  79. ExConstants.security_Decryption_arithmeticNULL_Msg);
  80. }
  81. // 检查public参数不为空
  82. if (ValidataUtil.isEmpty(requestMode.getPublic().getMethod())
  83. || ValidataUtil.isEmpty(requestMode.getPublic().getTimestamp())
  84. || ValidataUtil.isEmpty(requestMode.getPublic().getVersion())) {
  85. throw new OpensnsException(ExConstants.input_Data_Type, ExConstants.input_Data_Null_Msg);
  86. }
  87. String dataType = (String) headers.get("dataType");
  88. // 检查public参数不为空
  89. if ((!"json".equalsIgnoreCase(dataType)) && (!"xml".equalsIgnoreCase(dataType))) {
  90. throw new OpensnsException(ExConstants.input_Data_Type, ExConstants.input_Data_Type_Msg);
  91. }
  92. }
  93. /**
  94. * 判断加密方法是否正确
  95. * true 正确
  96. * @param signMethod
  97. * @return
  98. */
  99. private boolean valiSignMethod(String signMethod) {
  100. boolean isFlag = false;
  101. if ("AES".equalsIgnoreCase(signMethod) || "AES/AES".equalsIgnoreCase(signMethod)) {
  102. isFlag = true;
  103. }
  104. return isFlag;
  105. }
  106. /**
  107. * 组装成JSON格式字符串
  108. * @param requestMode
  109. * @param clientReqParam
  110. * @return
  111. */
  112. private String installJsonStr(RequestMode requestMode, String clientReqParam) throws Exception {
  113. JSONObject jSONObj = new JSONObject();
  114. JSONObject publicObj = new JSONObject();
  115. JSONObject privateObj = new JSONObject();
  116. publicObj.put("method", requestMode.getPublic().getMethod());
  117. publicObj.put("version", requestMode.getPublic().getVersion());
  118. publicObj.put("timestamp", requestMode.getPublic().getTimestamp());
  119. privateObj.put("servicedata", requestMode.getPrivate().getServicedata());
  120. jSONObj.put("public", publicObj);
  121. jSONObj.put("private", privateObj);
  122. clientReqParam = jSONObj.toString();
  123. return clientReqParam;
  124. }
  125. /**
  126. * 组装成XML格式字符串
  127. * @param requestMode
  128. * @return
  129. */
  130. private String installXmlStr(RequestMode requestMode) throws Exception {
  131. net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(requestMode);
  132. XMLSerializer xml = new XMLSerializer();
  133. String xmlStr = xml.write(jsonObject, "UTF-8");
  134. return xmlStr;
  135. }
  136. }