| | |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.api.smo.DefaultAbstractComponentSMO; |
| | | import com.java110.core.component.BaseComponentSMO; |
| | | import com.java110.api.smo.IApiServiceSMO; |
| | | import com.java110.api.smo.api.IApiSMO; |
| | | import com.java110.core.context.IPageData; |
| | | import com.java110.entity.component.ComponentValidateResult; |
| | | import com.java110.api.smo.api.IApiSMO; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.constant.ServiceConstant; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.utils.util.StringUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.HttpStatusCodeException; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | @Service("apiSMOImpl") |
| | | public class ApiSMOImpl extends DefaultAbstractComponentSMO implements IApiSMO { |
| | | |
| | | @Autowired |
| | | private IApiServiceSMO apiServiceSMOImpl; |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(ApiSMOImpl.class); |
| | | |
| | |
| | | |
| | | @Override |
| | | public ResponseEntity<String> doApi(String body, Map<String, String> headers, HttpServletRequest request) throws UnsupportedEncodingException { |
| | | HttpHeaders header = new HttpHeaders(); |
| | | |
| | | IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA); |
| | | |
| | | for (String key : headers.keySet() |
| | | ) { |
| | | header.add(key, headers.get(key)); |
| | | } |
| | | |
| | | ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate); |
| | | if (!StringUtil.isEmpty(result.getUserId())) { |
| | | header.remove("user-id"); |
| | | header.add("user-id", result.getUserId()); |
| | | headers.remove("user-id"); |
| | | headers.remove("user_id"); |
| | | headers.put("user-id", result.getUserId()); |
| | | headers.put("user_id", result.getUserId()); |
| | | if (!StringUtil.isEmpty(result.getUserName())) { |
| | | header.add("user-name", URLEncoder.encode(result.getUserName(), "UTF-8")); |
| | | headers.put("user-name", URLEncoder.encode(result.getUserName(), "UTF-8")); |
| | | } |
| | | } |
| | | |
| | | header.add("store-id", result.getStoreId()); |
| | | logger.debug("api请求头" + headers + ";请求内容:" + body); |
| | | HttpMethod method = null; |
| | | String url = headers.get(CommonConstant.HTTP_SERVICE); |
| | | if (CommonConstant.HTTP_METHOD_POST.equals(headers.get(CommonConstant.HTTP_METHOD))) { |
| | | method = HttpMethod.POST; |
| | | } else if (CommonConstant.HTTP_METHOD_GET.equals(headers.get(CommonConstant.HTTP_METHOD))) { |
| | | method = HttpMethod.GET; |
| | | url += super.mapToUrlParam(JSONObject.parseObject(body)); |
| | | } else if (CommonConstant.HTTP_METHOD_DELETE.equals(headers.get(CommonConstant.HTTP_METHOD))) { |
| | | method = HttpMethod.DELETE; |
| | | } else if (CommonConstant.HTTP_METHOD_PUT.equals(headers.get(CommonConstant.HTTP_METHOD))) { |
| | | method = HttpMethod.PUT; |
| | | |
| | | } else { |
| | | throw new IllegalArgumentException("不支持的请求方式" + headers.get(CommonConstant.HTTP_METHOD)); |
| | | if (!headers.containsKey("user_id")) { |
| | | headers.put("user_id", "-1"); |
| | | } |
| | | |
| | | |
| | | HttpEntity<String> httpEntity = new HttpEntity<String>(body, header); |
| | | logger.debug("请求后端url" + url); |
| | | ResponseEntity<String> responseEntity = null; |
| | | try { |
| | | responseEntity = restTemplate.exchange(url, method, httpEntity, String.class); |
| | | } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下 |
| | | responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode()); |
| | | } catch (Exception e) { |
| | | responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); |
| | | } finally { |
| | | logger.debug("api返回信息" + responseEntity); |
| | | if (!headers.containsKey("user-id")) { |
| | | headers.put("user-id", "-1"); |
| | | } |
| | | headers.put("store-id", result.getStoreId()); |
| | | ResponseEntity<String> responseEntity = apiServiceSMOImpl.service(body, headers); |
| | | return responseEntity; |
| | | } |
| | | |