ApiBaseBMO.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.java110.api.bmo;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.java110.core.context.DataFlowContext;
  5. import com.java110.core.factory.DataFlowFactory;
  6. import com.java110.entity.center.AppService;
  7. import com.java110.entity.order.Orders;
  8. import com.java110.event.service.api.ServiceDataFlowEvent;
  9. import com.java110.utils.constant.CommonConstant;
  10. import com.java110.utils.constant.ServiceCodeConstant;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.http.*;
  13. import org.springframework.web.client.HttpStatusCodeException;
  14. import org.springframework.web.client.RestTemplate;
  15. import java.util.Map;
  16. public class ApiBaseBMO implements IApiBaseBMO{
  17. protected static final int DEFAULT_ORDER = 1;
  18. //默认序列
  19. protected static final int DEFAULT_SEQ = 1;
  20. @Autowired
  21. private RestTemplate restTemplate;
  22. @Autowired
  23. private RestTemplate restTemplateNoLoadBalanced;
  24. /**
  25. * 调用下游服务
  26. *
  27. * @param event
  28. * @return
  29. */
  30. public ResponseEntity<String> callService(ServiceDataFlowEvent event) {
  31. DataFlowContext dataFlowContext = event.getDataFlowContext();
  32. AppService service = event.getAppService();
  33. return callService(dataFlowContext, service, dataFlowContext.getReqJson());
  34. }
  35. /**
  36. * 调用下游服务
  37. *
  38. * @param context
  39. * @param serviceCode 下游服务
  40. * @return
  41. */
  42. public ResponseEntity<String> callService(DataFlowContext context, String serviceCode,JSONArray businesses) {
  43. context.getRequestCurrentHeaders().put(CommonConstant.HTTP_ORDER_TYPE_CD, "D");
  44. JSONObject paramInObj = restToCenterProtocol(businesses, context.getRequestCurrentHeaders());
  45. return callService(context,serviceCode,paramInObj);
  46. }
  47. /**
  48. * 调用下游服务
  49. *
  50. * @param context
  51. * @param serviceCode 下游服务
  52. * @return
  53. */
  54. public ResponseEntity<String> callService(DataFlowContext context, String serviceCode,JSONObject paramInObj) {
  55. //将 rest header 信息传递到下层服务中去
  56. HttpHeaders header = new HttpHeaders();
  57. freshHttpHeader(header, context.getRequestCurrentHeaders());
  58. ResponseEntity responseEntity = null;
  59. AppService appService = DataFlowFactory.getService(context.getAppId(), serviceCode);
  60. if (appService == null) {
  61. responseEntity = new ResponseEntity<String>("当前没有权限访问" + ServiceCodeConstant.SERVICE_CODE_QUERY_STORE_USERS, HttpStatus.UNAUTHORIZED);
  62. context.setResponseEntity(responseEntity);
  63. return responseEntity;
  64. }
  65. return callService(context, appService, paramInObj);
  66. }
  67. /**
  68. * 调用下游服务
  69. *
  70. * @param context
  71. * @param appService 下游服务
  72. * @return
  73. */
  74. public ResponseEntity<String> callService(DataFlowContext context, AppService appService, Map paramIn) {
  75. context.getRequestCurrentHeaders().put(CommonConstant.HTTP_ORDER_TYPE_CD, "D");
  76. ResponseEntity responseEntity = null;
  77. if (paramIn == null || paramIn.isEmpty()) {
  78. paramIn = context.getReqJson();
  79. }
  80. RestTemplate tmpRestTemplate = appService.getServiceCode().startsWith("out.") ? restTemplateNoLoadBalanced : restTemplate;
  81. String serviceUrl = appService.getUrl();
  82. HttpEntity<String> httpEntity = null;
  83. HttpHeaders header = new HttpHeaders();
  84. for (String key : context.getRequestCurrentHeaders().keySet()) {
  85. if (CommonConstant.HTTP_SERVICE.toLowerCase().equals(key.toLowerCase())) {
  86. continue;
  87. }
  88. header.add(key, context.getRequestCurrentHeaders().get(key));
  89. }
  90. header.add(CommonConstant.HTTP_SERVICE.toLowerCase(), appService.getServiceCode());
  91. try {
  92. if (CommonConstant.HTTP_METHOD_GET.equals(appService.getMethod())) {
  93. serviceUrl += "?";
  94. for (Object key : paramIn.keySet()) {
  95. serviceUrl += (key + "=" + paramIn.get(key) + "&");
  96. }
  97. if (serviceUrl.endsWith("&")) {
  98. serviceUrl = serviceUrl.substring(0, serviceUrl.lastIndexOf("&"));
  99. }
  100. httpEntity = new HttpEntity<String>("", header);
  101. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.GET, httpEntity, String.class);
  102. } else if (CommonConstant.HTTP_METHOD_PUT.equals(appService.getMethod())) {
  103. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  104. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.PUT, httpEntity, String.class);
  105. } else if (CommonConstant.HTTP_METHOD_DELETE.equals(appService.getMethod())) {
  106. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  107. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.DELETE, httpEntity, String.class);
  108. } else {
  109. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  110. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.POST, httpEntity, String.class);
  111. }
  112. } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
  113. responseEntity = new ResponseEntity<String>( e.getResponseBodyAsString(), e.getStatusCode());
  114. }
  115. return responseEntity;
  116. }
  117. /**
  118. * 将rest 协议转为 订单协议
  119. *
  120. * @param businesses 多个业务
  121. * @param headers 订单头信息
  122. * @return
  123. */
  124. public JSONObject restToCenterProtocol(JSONArray businesses, Map<String, String> headers) {
  125. JSONObject centerProtocol = JSONObject.parseObject("{\"orders\":{},\"business\":[]}");
  126. freshOrderProtocol(centerProtocol.getJSONObject("orders"), headers);
  127. centerProtocol.put("business", businesses);
  128. return centerProtocol;
  129. }
  130. /**
  131. * 将rest 协议转为 订单协议
  132. *
  133. * @param business
  134. * @return
  135. */
  136. public JSONObject restToCenterProtocol(JSONObject business, Map<String, String> headers) {
  137. JSONObject centerProtocol = JSONObject.parseObject("{\"orders\":{},\"business\":[]}");
  138. freshOrderProtocol(centerProtocol.getJSONObject("orders"), headers);
  139. centerProtocol.getJSONArray("business").add(business);
  140. return centerProtocol;
  141. }
  142. /**
  143. * 刷入order信息
  144. *
  145. * @param orders 订单信息
  146. * @param headers 头部信息
  147. */
  148. public void freshOrderProtocol(JSONObject orders, Map<String, String> headers) {
  149. for (String key : headers.keySet()) {
  150. if (CommonConstant.HTTP_APP_ID.equals(key)) {
  151. orders.put("appId", headers.get(key));
  152. }
  153. if (CommonConstant.HTTP_TRANSACTION_ID.equals(key)) {
  154. orders.put("transactionId", headers.get(key));
  155. }
  156. if (CommonConstant.HTTP_SIGN.equals(key)) {
  157. orders.put("sign", headers.get(key));
  158. }
  159. if (CommonConstant.HTTP_REQ_TIME.equals(key)) {
  160. orders.put("requestTime", headers.get(key));
  161. }
  162. if (CommonConstant.HTTP_ORDER_TYPE_CD.equals(key)) {
  163. orders.put("orderTypeCd", headers.get(key));
  164. }
  165. if (CommonConstant.HTTP_USER_ID.equals(key)) {
  166. orders.put("userId", headers.get(key));
  167. }
  168. if(CommonConstant.ORDER_PROCESS.equals(key)){
  169. orders.put("orderProcess", headers.get(CommonConstant.ORDER_PROCESS));
  170. }
  171. }
  172. }
  173. /**
  174. * 刷入order信息
  175. *
  176. * @param httpHeaders http 头信息
  177. * @param headers 头部信息
  178. */
  179. public void freshHttpHeader(HttpHeaders httpHeaders, Map<String, String> headers) {
  180. for (String key : headers.keySet()) {
  181. if (CommonConstant.HTTP_APP_ID.equals(key)) {
  182. httpHeaders.add("app_id", headers.get(key));
  183. }
  184. if (CommonConstant.HTTP_TRANSACTION_ID.equals(key)) {
  185. httpHeaders.add("transaction_id", headers.get(key));
  186. }
  187. if (CommonConstant.HTTP_REQ_TIME.equals(key)) {
  188. httpHeaders.add("req_time", headers.get(key));
  189. }
  190. if (CommonConstant.HTTP_USER_ID.equals(key)) {
  191. httpHeaders.add("user_id", headers.get(key));
  192. }
  193. }
  194. }
  195. }