InsurePdfAddressManager.java 4.05 KB
package cn.com.glsx.manager;

import cn.com.glsx.dj.insure.model.InsureOrder;
import com.rop.utils.RopUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;


/**
 * @author zhoudan
 */
@Service
public class InsurePdfAddressManager {

    @Autowired
    private Environment env;

    private final static Logger LOGGER = LoggerFactory.getLogger(InsurePdfAddressManager.class);

//    final String appKey = "48e5e13229b82c1b4e6e8c96151f0637";

//    final String secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";

    public String getInsurePdfAddressManager(InsureOrder insureOrder){
        try{
            if(insureOrder.getSeqNo() == null){
                throw new NullPointerException("param seqNo is not null");
            }
            MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
            String url = env.getProperty("rop_url");
            params.add("method", "dj.api.insure.get.pdf.address");
            params.add("seqNo",insureOrder.getSeqNo());
            params.add("v", "1.0.0");
            params.add("appKey", env.getProperty("rop_appKey"));
            params.add("format", "json");
            params.add("locale", "zh_CN");
            String sign = RopUtils.sign(params.toSingleValueMap(), env.getProperty("rop_secret"));
            params.add("sign", sign);
            RestTemplate rest = new RestTemplate();
            RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler();
            rest.setUriTemplateHandler(uriTemplateHandler);
            String response = rest.getForObject(url, String.class, params.toSingleValueMap());
            System.out.println(response);
            return response;
        }catch(Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return "";
    }

    public static void main(String[] args) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(16);
        String url = "http://localhost:8343/rop-dj/router";
        params.add("seqNo", "e44e5a1c-698b-4c00-a20b-3340de068abb");
        params.add("v", "1.0.0");
        params.add("method", "dj.api.insure.get.pdf.address");
        params.add("appKey", "48e5e13229b82c1b4e6e8c96151f0637");
        params.add("format", "json");
        params.add("locale", "zh_CN");
        String sign = RopUtils.sign(params.toSingleValueMap(), "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b");
        params.add("sign", sign);
        try{
            RestTemplate rest = new RestTemplate();
            RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler();
            rest.setUriTemplateHandler(uriTemplateHandler);
            String response = rest.getForObject(url, String.class, params.toSingleValueMap());
            System.out.println(response);
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
    }

    static class RopUriTemplateHandler extends DefaultUriTemplateHandler {
        @Override
        protected UriComponents expandAndEncode(UriComponentsBuilder builder,
                                                Map<String, ?> uriVariables) {
            if(uriVariables != null && !uriVariables.isEmpty()){
                MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
                for(Map.Entry<String, ?> entry:uriVariables.entrySet()){
                    params.add(entry.getKey(), entry.getValue().toString());
                }
                builder.queryParams(params);
            }
            return super.expandAndEncode(builder, uriVariables);
        }
    }
}