/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.google.gson.Gson
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.apache.http.HttpEntity
 *  org.apache.http.HttpResponse
 *  org.apache.http.client.config.RequestConfig
 *  org.apache.http.client.entity.EntityBuilder
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClientBuilder
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.ssl.SSLContextBuilder
 *  org.apache.http.ssl.TrustStrategy
 *  org.apache.http.util.EntityUtils
 */
package com.ifugle.util.export;

import com.google.gson.Gson;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.domain.BaseDomain;
import com.ifugle.util.export.AESUtil;
import com.ifugle.util.export.URLSignUtil;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

public class DsbServiceUtil {
    static Log _logger = LogFactory.getLog(DsbServiceUtil.class);
    private static SSLContext sslContext;
    private static RequestConfig commonRequestConfig;

    public static <T> T sendMess(String path, BaseDomain domain, Class<T> classT) throws Exception {
        Object returnDomain = classT.newInstance();
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("client_id", WebConstants.DSB_CLIENT_ID);
        Gson gson = new Gson();
        String dataJson = new Gson().toJson((Object)domain);
        _logger.debug((Object)("\u8bf7\u6c42\u5730\u5740" + WebConstants.DSB_SERVICE_URL + path + ",\u53c2\u6570" + dataJson));
        String clientSecret = WebConstants.DSB_CLIENT_SECRET;
        try {
            String result = DsbServiceUtil.doPost(WebConstants.DSB_SERVICE_URL, path, params, AESUtil.encryptWithZipToBase64(dataJson, clientSecret), "utf-8", WebConstants.DSB_CLIENT_ID, WebConstants.DSB_CLIENT_SECRET);
            returnDomain = gson.fromJson(result, classT);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return returnDomain;
    }

    public static String doPost(String url, String path, Map<String, String> params, String json, String charset, String clientId, String clientSecret) throws Exception {
        String paramsStr = URLSignUtil.getSortedQuery(params);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(date);
        String sign = URLSignUtil.sign("POST", dateStr, path, paramsStr, clientSecret);
        CloseableHttpClient httpClient = DsbServiceUtil.getCommonBuilder().build();
        HttpPost httpPost = new HttpPost(String.valueOf(url) + path + "?" + paramsStr);
        httpPost.setHeader("Date", dateStr);
        httpPost.setHeader("Authorization", String.valueOf(clientId) + ":" + sign);
        httpPost.setHeader("Content-Type", "application/json");
        EntityBuilder entity = EntityBuilder.create();
        entity.setText(json);
        httpPost.setEntity(entity.build());
        HttpResponse response = httpClient.execute((HttpUriRequest)httpPost);
        HttpEntity entity2 = response.getEntity();
        String result = EntityUtils.toString((HttpEntity)entity2, (String)charset);
        _logger.debug((Object)("\u8bf7\u6c42\u8fd4\u56de\u7ed3\u679c" + response.getStatusLine().getStatusCode()));
        if (response.getStatusLine().getStatusCode() == 200) {
            result = AESUtil.decryptWithUnzipFromBase64(result, WebConstants.DSB_CLIENT_SECRET);
        }
        _logger.debug((Object)result);
        return result;
    }

    private static HttpClientBuilder getCommonBuilder() {
        HttpClientBuilder builder = HttpClients.custom();
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
        try {
            sslContext = sslContextBuilder.loadTrustMaterial(null, new TrustStrategy(){

                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        if (sslContext != null) {
            builder.setSSLContext(sslContext);
        }
        if (commonRequestConfig != null) {
            builder.setDefaultRequestConfig(commonRequestConfig);
        }
        return builder;
    }
}
