Gb1400RestTemplate.java 3.96 KB
package com.viontech.config;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author vion
 **/
@Component
@Slf4j
public class Gb1400RestTemplate {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Gb1400Config gb1400Config;

    public String doPost(String userIdentify, String uri, String content) {
        HttpHeaders httpHeaders = setHeader(userIdentify);
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        String url = "http://" + gb1400Config.getIpPort() + uri;
        HttpEntity<String> entity = new HttpEntity<>(content, httpHeaders);
        if (content.length() > 500) {
//            log.info("1400:{} data docking,params:{}", uri, content.substring(0, 500));
            log.info("1400:{} data docking,params:{}", uri, content);
        } else {
            log.info("1400:{} data docking,params:{}", uri, content);
        }
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
        String result = responseEntity.getBody();
        log.info("1400:{} data docking,result:{}", uri, result);
        return result;
    }

    public String doGet(String userIdentify, String uri, Map params) {
        String paramsStr = JSONObject.toJSONString(params);
        HttpHeaders httpHeaders = setHeader(userIdentify);
        HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        String url = "http://" + gb1400Config.getIpPort() + uri;
        if (params.containsKey("NotificationID")) {
            url = url + "?NotificationID=" + params.get("NotificationID");
        }
        if (params.containsKey("SubscribeID")) {
            if (url.indexOf("?") > 0) {
                url = url + "&SubscribeID=" + params.get("SubscribeID");
            } else {
                url = url + "?SubscribeID=" + params.get("SubscribeID");
            }
        }
        log.info("1400:{} data doGet,params:{}", url, paramsStr);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new HashMap<>());
        String result = responseEntity.getBody();
        log.info("1400:{} data doGet,result:{}", uri, result);
        return result;
    }

    public String doDelete(String userIdentify, String uri, Map params) {
        String paramsStr = JSONObject.toJSONString(params);
        HttpHeaders httpHeaders = setHeader(userIdentify);
        HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        String url = "http://" + gb1400Config.getIpPort() + uri;
        log.info("1400:{} data doDelete,params:{}", uri, paramsStr);
//        restTemplate.delete(url, params);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class, new HashMap<>());
        String result = responseEntity.getBody();
        log.info("1400:{} data doDelete,result:{}", uri, result);
        return result;
    }

    private HttpHeaders setHeader(String userIdentify) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("User-Identify", userIdentify);
        headers.add("Content-Type", "application/VIID+JSON;charset=utf-8");
//        String currentTime = DateUtil.getCurrentTime(DateUtil.TIMESTAMP_NO_DELIMITER);
//        headers.add("User-SendTime", currentTime);
        return headers;
    }
}