/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.sf.json.JSONObject
 *  org.apache.http.HttpEntity
 *  org.apache.http.client.ClientProtocolException
 *  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.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.message.BasicNameValuePair
 *  org.apache.http.util.EntityUtils
 */
package com.hst.webService.service;

import com.hst.common.domain.TaxCommonReturnDomain;
import com.hst.common.util.HstJsonUtil;
import com.hst.framework.constants.AppServiceConstants;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.util.HstToolsUtil;
import java.io.IOException;
import java.util.ArrayList;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class BaseWebService {
    public byte[] doService(byte[] ywId, byte[] parmJson) throws Exception {
        int index;
        String ywIdStr = new String(ywId);
        String parmJsonStr = new String(parmJson);
        String actionUrl = "";
        String key = "";
        TaxCommonReturnDomain domain = null;
        if (HstToolsUtil.isNullOrEmpty(ywIdStr)) {
            domain = new TaxCommonReturnDomain();
            domain.setCode("01");
            domain.setMess("\u975e\u6cd5\u8bf7\u6c42\uff01");
            return HstJsonUtil.beanToJson(domain).getBytes();
        }
        key = AppServiceConstants.HST_ACTION_MAP.containsKey(ywIdStr) ? ywIdStr : ((index = ywIdStr.indexOf(".")) >= 0 ? ywIdStr.substring(0, index + 1) : ywIdStr);
        if (!AppServiceConstants.HST_ACTION_MAP.containsKey(key)) {
            domain = new TaxCommonReturnDomain();
            domain.setCode("01");
            domain.setMess("\u975e\u6cd5\u8bf7\u6c42\uff01");
            return HstJsonUtil.beanToJson(domain).getBytes();
        }
        actionUrl = AppServiceConstants.HST_ACTION_MAP.get(key);
        String url = "http://" + WebConstants.LOCAL_SERVICE_IP + ":" + WebConstants.LOCAL_SERVICE_PORT + "/" + actionUrl;
        String resStr = this.sendMess(ywIdStr, parmJsonStr, url);
        return resStr == null ? null : resStr.getBytes();
    }

    public String sendMess(String ywId, String parmJson, String 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("dsyyqh.ywId", ywId));
            par.add(new BasicNameValuePair("dsyyqh.parmJson", parmJson));
            httppost.setEntity((HttpEntity)new UrlEncodedFormEntity(par, "gbk"));
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                strResult = EntityUtils.toString((HttpEntity)response.getEntity());
            } else {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
            }
        }
        catch (ClientProtocolException e) {
            strResult = e.toString();
        }
        catch (IOException e) {
            strResult = e.toString();
        }
        return strResult;
    }

    public String doService(String ywId, String parmJson) {
        int index;
        String actionUrl = "";
        String key = "";
        TaxCommonReturnDomain domain = null;
        if (HstToolsUtil.isNullOrEmpty(ywId)) {
            domain = new TaxCommonReturnDomain();
            domain.setCode("01");
            domain.setMess("\u975e\u6cd5\u8bf7\u6c42\uff01");
            return HstJsonUtil.beanToJson(domain);
        }
        key = AppServiceConstants.HST_ACTION_MAP.containsKey(ywId) ? ywId : ((index = ywId.indexOf(".")) >= 0 ? ywId.substring(0, index + 1) : ywId);
        if (!AppServiceConstants.HST_ACTION_MAP.containsKey(key)) {
            domain = new TaxCommonReturnDomain();
            domain.setCode("01");
            domain.setMess("\u975e\u6cd5\u8bf7\u6c42\uff01");
            return HstJsonUtil.beanToJson(domain);
        }
        actionUrl = AppServiceConstants.HST_ACTION_MAP.get(key);
        String url = "http://" + WebConstants.LOCAL_SERVICE_IP + ":" + WebConstants.LOCAL_SERVICE_PORT + "/" + actionUrl;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        String strResult = "";
        TaxCommonReturnDomain returnDomian = null;
        String err = "";
        try {
            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("dsyyqh.ywId", ywId));
                par.add(new BasicNameValuePair("dsyyqh.parmJson", parmJson));
                httpPost.setEntity((HttpEntity)new UrlEncodedFormEntity(par, "gbk"));
                response = httpClient.execute((HttpUriRequest)httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    strResult = EntityUtils.toString((HttpEntity)response.getEntity());
                    if (!HstToolsUtil.isNullOrEmpty(strResult)) {
                        JSONObject jsonObject = JSONObject.fromObject((Object)strResult);
                        JSONObject retJson = JSONObject.fromObject((Object)jsonObject.getJSONObject("dsyyqh"));
                        strResult = retJson.getString("retJson");
                    }
                } else {
                    returnDomian = new TaxCommonReturnDomain();
                    returnDomian.setCode("01");
                    err = String.valueOf(response.getStatusLine().getStatusCode());
                    err = "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                    returnDomian.setMess(err);
                    strResult = HstJsonUtil.beanToJson(returnDomian);
                }
            }
            catch (ClientProtocolException e) {
                returnDomian = new TaxCommonReturnDomain();
                returnDomian.setCode("01");
                err = e.toString().length() > 1000 ? e.toString().substring(0, 999) : e.toString();
                returnDomian.setMess(err);
                strResult = HstJsonUtil.beanToJson(returnDomian);
                try {
                    if (response != null) {
                        response.close();
                    }
                    if (httpPost != null) {
                        httpPost.releaseConnection();
                    }
                    if (httpClient != null) {
                        httpClient.close();
                    }
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            catch (IOException e) {
                returnDomian = new TaxCommonReturnDomain();
                returnDomian.setCode("01");
                err = e.toString().length() > 1000 ? e.toString().substring(0, 999) : e.toString();
                returnDomian.setMess(err);
                strResult = HstJsonUtil.beanToJson(returnDomian);
                try {
                    if (response != null) {
                        response.close();
                    }
                    if (httpPost != null) {
                        httpPost.releaseConnection();
                    }
                    if (httpClient != null) {
                        httpClient.close();
                    }
                }
                catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
        }
        finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpPost != null) {
                    httpPost.releaseConnection();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return strResult;
    }
}
