/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletInputStream
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.apache.http.HttpEntity
 *  org.apache.http.HttpResponse
 *  org.apache.http.client.entity.EntityBuilder
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.util.EntityUtils
 *  org.springframework.web.bind.annotation.RequestMapping
 *  org.springframework.web.bind.annotation.RequestMethod
 *  org.springframework.web.bind.annotation.RequestParam
 */
package com.hst.test;

import com.hst.common.util.Base64;
import com.hst.test.SSLClient;
import com.ifugle.util.export.AESUtil;
import com.ifugle.util.export.URLSignUtil;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

public class UrlTest {
    @RequestMapping(value={"/XXXX"}, method={RequestMethod.GET})
    public Map<String, Object> getXXXX(HttpServletRequest request, HttpServletResponse response, @RequestParam String client_id) throws Exception {
        String client_secret = "XXXXX";
        HashMap<String, Object> result = new HashMap<String, Object>();
        if (!URLSignUtil.check(request, client_id, client_secret)) {
            result.put("success", false);
            result.put("msg", "\u9a8c\u8bc1\u7b7e\u540d\u5931\u8d25");
            return result;
        }
        return null;
    }

    @RequestMapping(value={"/XXXX"}, method={RequestMethod.POST})
    public Map<String, Object> postXXXX(HttpServletRequest request, HttpServletResponse response, @RequestParam String client_id) throws Exception {
        String client_secret = "XXXXX";
        HashMap<String, Object> result = new HashMap<String, Object>();
        if (!URLSignUtil.check(request, client_id, client_secret)) {
            result.put("success", false);
            result.put("msg", "\u9a8c\u8bc1\u7b7e\u540d\u5931\u8d25");
            return result;
        }
        String sign = null;
        ServletInputStream in = null;
        try {
            in = request.getInputStream();
            sign = IOUtils.toString((InputStream)in, (String)"UTF-8");
        }
        finally {
            if (in != null) {
                in.close();
            }
        }
        AESUtil.decryptWithUnzipFromBase64(sign, client_secret);
        return null;
    }

    public static void main(String[] args) throws Exception {
        UrlTest test = new UrlTest();
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("client_id", "idBrnPgod6nsQReHcQHgzRyh");
        String fj = Base64.byteToString(UrlTest.getBytesFromFile("D:\\head\\test.xlsx")).replaceAll("\r\n", "");
        String fj2 = Base64.byteToString(UrlTest.getBytesFromFile("D:\\head\\test.pptx")).replaceAll("\r\n", "");
        String fj3 = Base64.byteToString(UrlTest.getBytesFromFile("D:\\head\\100000015210000007011.txt")).replaceAll("\r\n", "");
        String datajson = "{     \"xh\":\"1000000091\",   \"lx\":\"0\",   \"data\":[  { \"fjxh\":\"3\", \"fjmc\":\"test.xlsx\",\"fj\":\"" + fj + "\"}," + " { \"fjxh\":\"4\", \"fjmc\":\"test.pptx\",\"fj\":\"" + fj2 + "\"}," + " { \"fjxh\":\"5\", \"fjmc\":\"test.txt\",\"fj\":\"" + fj3 + "\"}" + "   ]" + "}";
        String clientSecret = "OuZzTS+xtYJVte7wcaU1hF40eHaRT+Ug/uVyqeFZ5Ww=";
        test.doPost("https://test.dingtax.cn", "/dsb/api/tpc/xxzx/attachment/upload", params, AESUtil.encryptWithZipToBase64(datajson, clientSecret), "utf-8", "idBrnPgod6nsQReHcQHgzRyh", clientSecret);
    }

    public 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);
        SSLClient httpClient = new SSLClient();
        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);
        if (response.getStatusLine().getStatusCode() == 200) {
            result = AESUtil.decryptWithUnzipFromBase64(result, "OuZzTS+xtYJVte7wcaU1hF40eHaRT+Ug/uVyqeFZ5Ww=");
        }
        System.out.println(result);
        return result;
    }

    public static byte[] getBytesFromFile(String filePath) {
        File f = new File(filePath);
        if (f.isDirectory()) {
            return null;
        }
        try {
            int n;
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            while ((n = stream.read(b)) != -1) {
                out.write(b, 0, n);
            }
            stream.close();
            out.close();
            return out.toByteArray();
        }
        catch (IOException iOException) {
            return null;
        }
    }
}
