ClientServiceRestTemplateImpl

阅读更多
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 parameterMap, String isEncrypted) {
RequestDTO requestDto = SecurityUtils.signAndEncryptForRequest(parameterMap, clientConfig.getGateWayUrl(), clientConfig.getPrivateKey(), isEncrypted, clientConfig.getAesKey());
HttpEntity> requestEntity = getRequestEntity(requestDto);
ResponseEntity 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> getRequestEntity(RequestDTO requestDto) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap bodyParams = new LinkedMultiValueMap();
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> requestEntity = new HttpEntity>(bodyParams, headers);
return requestEntity;
}

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

}

你可能感兴趣的:(ClientServiceRestTemplateImpl)