欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ClientServiceRestTemplateImpl

程序员文章站 2022-07-10 17:56:36
...
package com.pingan.ff.btoam.demo.service;

import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.pingan.ff.btoam.demo.dto.ParamDTO;
import com.pingan.ff.btoam.demo.dto.RequestDTO;
import com.pingan.ff.btoam.demo.dto.ResponseDTO;
import com.pingan.ff.btoam.demo.util.Constant;
import com.pingan.ff.btoam.demo.util.SecurityUtils;

@Service
public class ClientServiceRestTemplateImpl implements ClientService, InitializingBean {

public static final Log logger = LogFactory.getLog(ClientServiceRestTemplateImpl.class);

@Autowired
private ClientConfig clientConfig;

private RestTemplate restTemplate;

@Override
public void afterPropertiesSet() throws Exception {
HttpsClientRequestFactory httpsClientRequestFactory = new HttpsClientRequestFactory();
httpsClientRequestFactory.setConnectTimeout(Integer.parseInt(clientConfig.getConnectTimeout()));
httpsClientRequestFactory.setReadTimeout(Integer.parseInt(clientConfig.getReadTimeout()));
this.restTemplate = new RestTemplate(httpsClientRequestFactory);
}

@Override
public JSONObject request(Map<String, Object> parameterMap, String isEncrypted) {
RequestDTO requestDto = SecurityUtils.signAndEncryptForRequest(parameterMap, clientConfig.getGateWayUrl(), clientConfig.getPrivateKey(), isEncrypted, clientConfig.getAesKey());
HttpEntity<MultiValueMap<String, String>> requestEntity = getRequestEntity(requestDto);
ResponseEntity<String> response = null;
long startTime = System.currentTimeMillis();
logger.info("request begin");
try {
response = restTemplate.postForEntity(requestDto.getRequestUrl(), requestEntity, String.class);
} catch (Exception e) {
logger.error("request failed exception:", e);
} finally {
logger.info("request end cost:" + (System.currentTimeMillis() - startTime) + "ms");
}
if (response == null || response.getStatusCode() != HttpStatus.OK) {
logger.error("request failed response is null or httpStatus="+ response==null? "": response.getStatusCode());
return getResult("","请求失败");
}
JSONObject responseJSON = JSONObject.parseObject(response.getBody());
boolean isverfiy = SecurityUtils.decryptAndVerfiyForResponse(responseJSON, clientConfig.getAesKey(), clientConfig.getPublicKey());
if (!isverfiy) {
return getResult("","返回数据验签失败");
}
JSONObject dataJson = (JSONObject) responseJSON.get(Constant.DATA);
dataJson.remove(Constant.REQUESTID);
dataJson.remove(Constant.COMPONENTCODE);
dataJson.remove(Constant.ISENCRYPTED);
return dataJson;
}

private JSONObject getResult(String code, String msg){
JSONObject result = new JSONObject();
result.put(Constant.RESPONSECODE, code);
result.put(Constant.RESPONSEMSG, msg);
return result;
}

private HttpEntity<MultiValueMap<String, String>> getRequestEntity(RequestDTO requestDto) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> bodyParams = new LinkedMultiValueMap<String, String>();
bodyParams.add(Constant.SIGN, requestDto.getSign());
bodyParams.add(Constant.REQUESTDATA, requestDto.getRequestData());
bodyParams.add(Constant.ISENCRYPTED, requestDto.getIsEncrypted());
logger.info("request URL: " + requestDto.getRequestUrl());
logger.info("request params sign:" + requestDto.getSign() + ",isEncrypted:" + requestDto.getIsEncrypted() + ",requestData:" + requestDto.getRequestData());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(bodyParams, headers);
return requestEntity;
}

@Override
public ResponseDTO request(ParamDTO params, String isEncrypted) {
Map<String, Object> paramDataMap = (JSONObject) JSONObject.toJSON(params);
JSONObject resultJSON = request(paramDataMap, isEncrypted);
return JSONObject.toJavaObject(resultJSON, ResponseDTO.class);
}

}