ApiBaseBMO.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. JSONObject paramInObj = restToCenterProtocol(businesses, context.getRequestCurrentHeaders());
  44. return callService(context,serviceCode,paramInObj);
  45. }
  46. /**
  47. * 调用下游服务
  48. *
  49. * @param context
  50. * @param serviceCode 下游服务
  51. * @return
  52. */
  53. public ResponseEntity<String> callService(DataFlowContext context, String serviceCode,JSONObject paramInObj) {
  54. //将 rest header 信息传递到下层服务中去
  55. HttpHeaders header = new HttpHeaders();
  56. freshHttpHeader(header, context.getRequestCurrentHeaders());
  57. ResponseEntity responseEntity = null;
  58. AppService appService = DataFlowFactory.getService(context.getAppId(), serviceCode);
  59. if (appService == null) {
  60. responseEntity = new ResponseEntity<String>("当前没有权限访问" + ServiceCodeConstant.SERVICE_CODE_QUERY_STORE_USERS, HttpStatus.UNAUTHORIZED);
  61. context.setResponseEntity(responseEntity);
  62. return responseEntity;
  63. }
  64. return callService(context, appService, paramInObj);
  65. }
  66. /**
  67. * 调用下游服务
  68. *
  69. * @param context
  70. * @param appService 下游服务
  71. * @return
  72. */
  73. public ResponseEntity<String> callService(DataFlowContext context, AppService appService, Map paramIn) {
  74. context.getRequestCurrentHeaders().put(CommonConstant.HTTP_ORDER_TYPE_CD, "D");
  75. ResponseEntity responseEntity = null;
  76. if (paramIn == null || paramIn.isEmpty()) {
  77. paramIn = context.getReqJson();
  78. }
  79. RestTemplate tmpRestTemplate = appService.getServiceCode().startsWith("out.") ? restTemplateNoLoadBalanced : restTemplate;
  80. String serviceUrl = appService.getUrl();
  81. HttpEntity<String> httpEntity = null;
  82. HttpHeaders header = new HttpHeaders();
  83. for (String key : context.getRequestCurrentHeaders().keySet()) {
  84. if (CommonConstant.HTTP_SERVICE.toLowerCase().equals(key.toLowerCase())) {
  85. continue;
  86. }
  87. header.add(key, context.getRequestCurrentHeaders().get(key));
  88. }
  89. header.add(CommonConstant.HTTP_SERVICE.toLowerCase(), appService.getServiceCode());
  90. try {
  91. if (CommonConstant.HTTP_METHOD_GET.equals(appService.getMethod())) {
  92. serviceUrl += "?";
  93. for (Object key : paramIn.keySet()) {
  94. serviceUrl += (key + "=" + paramIn.get(key) + "&");
  95. }
  96. if (serviceUrl.endsWith("&")) {
  97. serviceUrl = serviceUrl.substring(0, serviceUrl.lastIndexOf("&"));
  98. }
  99. httpEntity = new HttpEntity<String>("", header);
  100. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.GET, httpEntity, String.class);
  101. } else if (CommonConstant.HTTP_METHOD_PUT.equals(appService.getMethod())) {
  102. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  103. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.PUT, httpEntity, String.class);
  104. } else if (CommonConstant.HTTP_METHOD_DELETE.equals(appService.getMethod())) {
  105. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  106. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.DELETE, httpEntity, String.class);
  107. } else {
  108. httpEntity = new HttpEntity<String>(JSONObject.toJSONString(paramIn), header);
  109. responseEntity = tmpRestTemplate.exchange(serviceUrl, HttpMethod.POST, httpEntity, String.class);
  110. }
  111. } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
  112. responseEntity = new ResponseEntity<String>( e.getResponseBodyAsString(), e.getStatusCode());
  113. }
  114. return responseEntity;
  115. }
  116. /**
  117. * 将rest 协议转为 订单协议
  118. *
  119. * @param businesses 多个业务
  120. * @param headers 订单头信息
  121. * @return
  122. */
  123. public JSONObject restToCenterProtocol(JSONArray businesses, Map<String, String> headers) {
  124. JSONObject centerProtocol = JSONObject.parseObject("{\"orders\":{},\"business\":[]}");
  125. freshOrderProtocol(centerProtocol.getJSONObject("orders"), headers);
  126. centerProtocol.put("business", businesses);
  127. return centerProtocol;
  128. }
  129. /**
  130. * 将rest 协议转为 订单协议
  131. *
  132. * @param business
  133. * @return
  134. */
  135. public JSONObject restToCenterProtocol(JSONObject business, Map<String, String> headers) {
  136. JSONObject centerProtocol = JSONObject.parseObject("{\"orders\":{},\"business\":[]}");
  137. freshOrderProtocol(centerProtocol.getJSONObject("orders"), headers);
  138. centerProtocol.getJSONArray("business").add(business);
  139. return centerProtocol;
  140. }
  141. /**
  142. * 刷入order信息
  143. *
  144. * @param orders 订单信息
  145. * @param headers 头部信息
  146. */
  147. public void freshOrderProtocol(JSONObject orders, Map<String, String> headers) {
  148. for (String key : headers.keySet()) {
  149. if (CommonConstant.HTTP_APP_ID.equals(key)) {
  150. orders.put("appId", headers.get(key));
  151. }
  152. if (CommonConstant.HTTP_TRANSACTION_ID.equals(key)) {
  153. orders.put("transactionId", headers.get(key));
  154. }
  155. if (CommonConstant.HTTP_SIGN.equals(key)) {
  156. orders.put("sign", headers.get(key));
  157. }
  158. if (CommonConstant.HTTP_REQ_TIME.equals(key)) {
  159. orders.put("requestTime", headers.get(key));
  160. }
  161. if (CommonConstant.HTTP_ORDER_TYPE_CD.equals(key)) {
  162. orders.put("orderTypeCd", headers.get(key));
  163. }
  164. if (CommonConstant.HTTP_USER_ID.equals(key)) {
  165. orders.put("userId", headers.get(key));
  166. }
  167. if(CommonConstant.ORDER_PROCESS.equals(key)){
  168. orders.put("orderProcess", headers.get(CommonConstant.ORDER_PROCESS));
  169. }
  170. }
  171. }
  172. /**
  173. * 刷入order信息
  174. *
  175. * @param httpHeaders http 头信息
  176. * @param headers 头部信息
  177. */
  178. public void freshHttpHeader(HttpHeaders httpHeaders, Map<String, String> headers) {
  179. for (String key : headers.keySet()) {
  180. if (CommonConstant.HTTP_APP_ID.equals(key)) {
  181. httpHeaders.add("app_id", headers.get(key));
  182. }
  183. if (CommonConstant.HTTP_TRANSACTION_ID.equals(key)) {
  184. httpHeaders.add("transaction_id", headers.get(key));
  185. }
  186. if (CommonConstant.HTTP_REQ_TIME.equals(key)) {
  187. httpHeaders.add("req_time", headers.get(key));
  188. }
  189. if (CommonConstant.HTTP_USER_ID.equals(key)) {
  190. httpHeaders.add("user_id", headers.get(key));
  191. }
  192. }
  193. }
  194. }