HttpApi.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.java110.center.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.center.smo.ICenterServiceSMO;
  4. import com.java110.common.constant.ResponseConstant;
  5. import com.java110.common.exception.BusinessException;
  6. import com.java110.common.util.Assert;
  7. import com.java110.core.factory.DataTransactionFactory;
  8. import com.java110.core.base.controller.BaseController;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * 中心http服务 统一服务类
  17. * 1、只提供service方法
  18. * 2、提供 透传机制
  19. * Created by wuxw on 2018/4/13.
  20. */
  21. @RestController
  22. public class HttpApi extends BaseController {
  23. @Autowired
  24. private ICenterServiceSMO centerServiceSMOImpl;
  25. @RequestMapping(path = "/httpApi/service",method= RequestMethod.GET)
  26. public String serviceGet(HttpServletRequest request) {
  27. return DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
  28. ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求").toJSONString();
  29. }
  30. @RequestMapping(path = "/httpApi/service",method= RequestMethod.POST)
  31. public String servicePost(@RequestBody String orderInfo, HttpServletRequest request) {
  32. try {
  33. Map<String, String> headers = new HashMap<String, String>();
  34. getRequestInfo(request, headers);
  35. //预校验
  36. preValiateOrderInfo(orderInfo);
  37. return centerServiceSMOImpl.service(orderInfo, headers);
  38. }catch (Exception e){
  39. logger.error("请求订单异常",e);
  40. return DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
  41. ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
  42. }
  43. }
  44. /**
  45. * 对协议不遵循的 接口进行透传
  46. * @param orderInfo
  47. * @param request
  48. * @return
  49. */
  50. @RequestMapping(path = "/httpApi/service/{serviceCode}",method= RequestMethod.POST)
  51. public String servicePostTransfer(@PathVariable String serviceCode, @RequestBody String orderInfo, HttpServletRequest request,
  52. HttpServletResponse response) {
  53. String resData = "";
  54. Map<String, String> headers = new HashMap<String, String>();
  55. try {
  56. headers.put("serviceCode",serviceCode);
  57. getRequestInfo(request, headers);
  58. //预校验
  59. preValiateOrderInfo(orderInfo,headers);
  60. resData = centerServiceSMOImpl.serviceTransfer(orderInfo, headers);
  61. }catch (Exception e){
  62. logger.error("请求订单异常",e);
  63. resData = DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
  64. ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
  65. }finally {
  66. for(String key : headers.keySet()) {
  67. response.addHeader(key,headers.get(key));
  68. }
  69. return resData;
  70. }
  71. }
  72. /**
  73. * 这里预校验,请求报文中不能有 dataFlowId
  74. * @param orderInfo
  75. */
  76. private void preValiateOrderInfo(String orderInfo) {
  77. if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){
  78. throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点");
  79. }
  80. }
  81. /**
  82. * 这里预校验,请求报文中不能有 dataFlowId
  83. * @param orderInfo
  84. */
  85. private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
  86. Assert.hasKey(headers,"serviceCode","没有包含serviceCode");
  87. Assert.hasLength(headers.get("serviceCode"),"serviceCode 不能为空");
  88. Assert.hasKey(headers,"appId","没有包含appId");
  89. Assert.hasLength(headers.get("appId"),"appId 不能为空");
  90. }
  91. /**
  92. * 获取请求信息
  93. * @param request
  94. * @param headers
  95. * @throws RuntimeException
  96. */
  97. private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
  98. try{
  99. super.initHeadParam(request,headers);
  100. super.initUrlParam(request,headers);
  101. }catch (Exception e){
  102. logger.error("加载头信息失败",e);
  103. throw e;
  104. }
  105. }
  106. public ICenterServiceSMO getCenterServiceSMOImpl() {
  107. return centerServiceSMOImpl;
  108. }
  109. public void setCenterServiceSMOImpl(ICenterServiceSMO centerServiceSMOImpl) {
  110. this.centerServiceSMOImpl = centerServiceSMOImpl;
  111. }
  112. }