/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.google.gson.Gson
 *  net.sf.json.JSONObject
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.apache.http.HttpEntity
 *  org.apache.http.auth.AuthScope
 *  org.apache.http.auth.Credentials
 *  org.apache.http.auth.UsernamePasswordCredentials
 *  org.apache.http.client.ClientProtocolException
 *  org.apache.http.client.CredentialsProvider
 *  org.apache.http.client.config.RequestConfig
 *  org.apache.http.client.entity.UrlEncodedFormEntity
 *  org.apache.http.client.methods.CloseableHttpResponse
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.impl.client.BasicCredentialsProvider
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.message.BasicNameValuePair
 *  org.apache.http.util.EntityUtils
 */
package com.hst.common.util;

import com.google.gson.Gson;
import com.hst.common.domain.BaseAppServiceDomain;
import com.hst.common.domain.TaxCommonReturnDomain;
import com.hst.common.util.HstJsonUtil;
import com.hst.framework.constants.WebConstants;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HstWxServiceUtil {
    static Log _logger = LogFactory.getLog(HstWxServiceUtil.class);

    public static Map<String, String> sendMess(BaseAppServiceDomain domain, String ywId) {
        HashMap<String, String> sendResMap = new HashMap<String, String>();
        String url = WebConstants.HST_PROP_MAP.get("hst.WX_SERVICE_URL");
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            httppost.addHeader("Content-type", "application/x-www-form-urlencoded;charset=gbk");
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            par.add(new BasicNameValuePair("domain.ywId", ywId));
            par.add(new BasicNameValuePair("domain.parmJson", HstJsonUtil.beanCtreatJson(domain)));
            httppost.setEntity((HttpEntity)new UrlEncodedFormEntity(par));
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                sendResMap.put("code", "00");
                sendResMap.put("mess", strResult);
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                sendResMap.put("code", "01");
                sendResMap.put("mess", strResult);
            }
        }
        catch (ClientProtocolException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        catch (IOException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        return sendResMap;
    }

    public static Map<String, String> sendMess(String url, BaseAppServiceDomain domain, String ywId) {
        HashMap<String, String> sendResMap = new HashMap<String, String>();
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            httppost.addHeader("Content-type", "application/x-www-form-urlencoded;charset=gbk");
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            par.add(new BasicNameValuePair("domain.ywId", ywId));
            par.add(new BasicNameValuePair("domain.parmJson", HstJsonUtil.beanCtreatJson(domain)));
            httppost.setEntity((HttpEntity)new UrlEncodedFormEntity(par));
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                sendResMap.put("code", "00");
                sendResMap.put("mess", strResult);
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                sendResMap.put("code", "01");
                sendResMap.put("mess", strResult);
            }
        }
        catch (ClientProtocolException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        catch (IOException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        return sendResMap;
    }

    public static <T extends TaxCommonReturnDomain> T sendMess(String url, BaseAppServiceDomain domain, String ywId, Class<T> classT) throws InstantiationException, IllegalAccessException {
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        TaxCommonReturnDomain returnDomain = (TaxCommonReturnDomain)classT.newInstance();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            httppost.addHeader("Content-type", "application/x-www-form-urlencoded;charset=gbk");
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
            httppost.setConfig(requestConfig);
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            par.add(new BasicNameValuePair("domain.ywId", ywId));
            par.add(new BasicNameValuePair("domain.parmJson", HstJsonUtil.beanCtreatJson(domain)));
            httppost.setEntity((HttpEntity)new UrlEncodedFormEntity(par, "GBK"));
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                JSONObject jsonObject = JSONObject.fromObject((Object)strResult);
                JSONObject retJson = JSONObject.fromObject((Object)jsonObject.getJSONObject("domain").get("retJson"));
                String resultJson = retJson.getString("result");
                Gson gson = new Gson();
                returnDomain = (TaxCommonReturnDomain)gson.fromJson(resultJson, classT);
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                returnDomain.setCode("01");
                returnDomain.setMess(strResult);
            }
        }
        catch (ClientProtocolException e) {
            returnDomain.setCode("01");
            returnDomain.setMess(e.toString());
        }
        catch (IOException e) {
            returnDomain.setCode("01");
            returnDomain.setMess(e.toString());
        }
        _logger.debug((Object)("\u8c03\u7528\u63a5\u53e3\uff0cURL=" + url + ",YWID=" + ywId + ",\u8fd4\u56de\u7ed3\u679c\uff1aCODE:" + returnDomain.getCode() + ",MESS=" + returnDomain.getMess()));
        return (T)returnDomain;
    }

    public static Map<String, String> sendMess(String url, Map<String, String> params, String charSet) {
        HashMap<String, String> sendResMap = new HashMap<String, String>();
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            if (params != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    par.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(par, charSet);
            httppost.setEntity((HttpEntity)entity);
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                sendResMap.put("code", "00");
                sendResMap.put("mess", strResult);
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                sendResMap.put("code", "01");
                sendResMap.put("mess", strResult);
            }
        }
        catch (ClientProtocolException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        catch (IOException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        return sendResMap;
    }

    public static Map<String, String> sendMessForBasicAuth(String url, Map<String, String> params, String charSet, String busName, String pwssword) {
        HashMap<String, String> sendResMap = new HashMap<String, String>();
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        CloseableHttpClient httpClient = HstWxServiceUtil.getHttpClient(busName, pwssword);
        try {
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            if (params != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    par.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(par, charSet);
            httppost.setEntity((HttpEntity)entity);
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                sendResMap.put("code", "00");
                sendResMap.put("mess", strResult);
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                sendResMap.put("code", "01");
                sendResMap.put("mess", strResult);
            }
        }
        catch (ClientProtocolException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        catch (IOException e) {
            sendResMap.put("code", "01");
            sendResMap.put("mess", e.toString());
        }
        return sendResMap;
    }

    private static CloseableHttpClient getHttpClient(String busName, String password) {
        BasicCredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(busName, password);
        provider.setCredentials(AuthScope.ANY, (Credentials)credentials);
        return HttpClients.custom().setDefaultCredentialsProvider((CredentialsProvider)provider).build();
    }
}
