/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minidev.json.JSONObject
 */
package com.hst.taxService.util;

import com.hst.taxService.util.HttpClientService;
import com.hst.taxService.util.Obj;
import com.hst.taxService.util.OpensnsException;
import com.hst.taxService.util.PrivateData;
import com.hst.taxService.util.PublicData;
import com.hst.taxService.util.RequestMode;
import com.hst.taxService.util.SecurityUtil;
import com.hst.taxService.util.Servicedata;
import com.hst.taxService.util.ValidataUtil;
import com.hst.taxService.util.XMLUtil;
import java.util.List;
import java.util.Map;
import net.minidev.json.JSONObject;

public class OpenApiV1 {
    public String handle(String serverName, Map<String, String> headers, RequestMode requestMode) throws OpensnsException {
        this.requestDataValidata(serverName, headers, requestMode);
        String dataType = headers.get("dataType");
        String clientReqParam = "";
        String aesParams = "";
        try {
            if ("json".equalsIgnoreCase(dataType)) {
                clientReqParam = this.installJsonStr(requestMode, clientReqParam);
            } else if ("xml".equalsIgnoreCase(dataType)) {
                clientReqParam = this.installXmlStr(requestMode);
            }
        }
        catch (Exception exception) {
            throw new OpensnsException("0201", "\u8f93\u5165\u53c2\u6570\u683c\u5f0f\u6709\u8bef");
        }
        System.out.println("\u5ba2\u6237\u7aef\u7ec4\u88c5\u7684\u53c2\u6570\u683c\u5f0f\uff1a" + clientReqParam);
        try {
            aesParams = (String)SecurityUtil.AESEncrypt(clientReqParam, SecurityUtil.app_secret);
        }
        catch (Exception exception) {
            throw new OpensnsException("0701", "\u52a0\u5bc6\u5f02\u5e38");
        }
        HttpClientService httpService = new HttpClientService();
        String result = httpService.sendSyncSingleHttp(serverName, headers, aesParams);
        System.out.println("result:" + result);
        return result;
    }

    private void requestDataValidata(String serverName, Map<String, String> headers, RequestMode requestMode) throws OpensnsException {
        if (ValidataUtil.isEmpty(serverName) || headers == null || headers.isEmpty() || requestMode == null || requestMode.getPublic() == null || requestMode.getPrivate() == null) {
            throw new OpensnsException("0101", "\u5165\u53c2\u4e3a\u7a7a");
        }
        if (ValidataUtil.isEmpty(headers.get("appKey")) || ValidataUtil.isEmpty(headers.get("dataType")) || ValidataUtil.isEmpty(headers.get("accessToken")) || ValidataUtil.isEmpty(headers.get("appRate"))) {
            throw new OpensnsException("0201", "\u5fc5\u586b\u9879\u8f93\u5165\u53c2\u6570\u7a7a");
        }
        if (!ValidataUtil.isEmpty(headers.get("signMethod")) && !"AES".equals(headers.get("signMethod"))) {
            throw new OpensnsException("070103", "\u65e0\u6cd5\u8bc6\u522b\u52a0\u5bc6\u7b97\u6cd5");
        }
        if (ValidataUtil.isEmpty(requestMode.getPublic().getMethod()) || ValidataUtil.isEmpty(requestMode.getPublic().getTimestamp()) || ValidataUtil.isEmpty(requestMode.getPublic().getVersion())) {
            throw new OpensnsException("0201", "\u5165\u53c2\u4e3a\u7a7a");
        }
        String dataType = headers.get("dataType");
        if (!"json".equalsIgnoreCase(dataType) && !"xml".equalsIgnoreCase(dataType)) {
            throw new OpensnsException("0201", "\u8f93\u5165\u53c2\u6570\u683c\u5f0f\u6709\u8bef");
        }
    }

    private String installJsonStr(RequestMode requestMode, String clientReqParam) throws Exception {
        JSONObject jSONObj = new JSONObject();
        JSONObject publicObj = new JSONObject();
        JSONObject privateObj = new JSONObject();
        publicObj.put((Object)"method", (Object)requestMode.getPublic().getMethod());
        publicObj.put((Object)"version", (Object)requestMode.getPublic().getVersion());
        publicObj.put((Object)"timestamp", (Object)requestMode.getPublic().getTimestamp());
        privateObj.put((Object)"servicedata", requestMode.getPrivate().getServicedata());
        jSONObj.put((Object)"public", (Object)publicObj);
        jSONObj.put((Object)"private", (Object)privateObj);
        clientReqParam = jSONObj.toString();
        return clientReqParam;
    }

    private String installXmlStr(RequestMode requestMode) throws Exception {
        String clientReqParam = "";
        Obj obj = new Obj();
        PublicData pubDataXml = requestMode.getPublic();
        pubDataXml.setClassName("object");
        StringBuffer eTag = new StringBuffer();
        Object servicedata = requestMode.getPrivate().getServicedata();
        List serviceDataList = (List)servicedata;
        if (serviceDataList != null && serviceDataList.size() > 0) {
            for (Map map : serviceDataList) {
                eTag.append("<e class=\"object\">");
                if (map == null || map.size() <= 0) continue;
                for (String key : map.keySet()) {
                    eTag.append("<").append(key).append(" type=\"string\">").append((String)map.get(key)).append("</").append(key).append(">");
                }
                eTag.append("</e>");
            }
        }
        PrivateData<Object> privateData = new PrivateData<Object>();
        privateData.setClassName("object");
        Servicedata sd = new Servicedata();
        sd.setClassName("array");
        sd.setValue("");
        privateData.setSdtata(sd);
        obj.setPublicData(pubDataXml);
        obj.setPrivateData(privateData);
        String clientReqXml = XMLUtil.convertToXml(obj);
        clientReqParam = clientReqXml.replace("<replaceData></replaceData>", eTag.toString());
        return clientReqParam;
    }
}
