package nc.bs.uapbd.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Map;

/**
 * Http请求包装类
 *
 * @author guoxiangqiao
 * @date 2019年12月11日
 */
public class HttpClientWapper {
    public static final int HTTP_STATUS_SUCCESS = 200;

    /**
     * post请求
     *
     * @param url
     * @param headers
     * @param requestBody
     * @return
     * @throws Exception
     */
    public Response post(String url, Header[] headers, NameValuePair[] requestBody, String requestEntityStr) throws Exception {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        if (headers != null) {
            for (Header header : headers) {
                postMethod.setRequestHeader(header);
            }
        }
        if (requestBody != null) {
            postMethod.setRequestBody(requestBody);
        }
        if (StringUtils.isNotEmpty(requestEntityStr)) {
            StringRequestEntity requestEntity = new StringRequestEntity(requestEntityStr, null, "utf-8");
            postMethod.setRequestEntity(requestEntity);
        }
        int httpStatus = client.executeMethod(postMethod);
        String result = this.getResponseBodyAsString(postMethod);
        return new Response(httpStatus, result);
    }

    public String getResponseBodyAsString(PostMethod postMethod) throws IOException {
        byte[] rawdata = null;
        rawdata = postMethod.getResponseBody();
        return rawdata != null ? EncodingUtil.getString(rawdata, "utf-8") : null;
    }

    /**
     * @param params
     * @return
     * @description Map转URL参数
     * @author guoxqc@yonyou.com
     * @date 2020年10月22日
     */
    public String getURLParam(Map<String, Object> params) {
        StringBuffer paramStr = new StringBuffer();
        if (!params.isEmpty()) {
            for (Map.Entry<String, Object> kv : params.entrySet()) {
                paramStr.append(kv.getKey());
                paramStr.append("=");
                paramStr.append(kv.getValue());
                paramStr.append("&");
            }
            paramStr.deleteCharAt(paramStr.length() - 1);
        }
        return paramStr.toString();
    }

    /**
     * get请求
     *
     * @param url
     * @param headers
     * @param params
     * @return
     * @throws Exception
     */
    public Response get(String url, Header[] headers, NameValuePair[] params) throws Exception {
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        if (params != null) {
            getMethod.setQueryString(params);
        }
        if (headers != null) {
            for (Header header : headers) {
                getMethod.setRequestHeader(header);
            }
        }
        int httpStatus = client.executeMethod(getMethod);
        String result = getMethod.getResponseBodyAsString();
        return new Response(httpStatus, result);
    }

    /**
     * 获取url查询参数
     *
     * @param params
     * @return
     */
    public String getQueryString(NameValuePair[] params) {
        GetMethod getMethod = new GetMethod();
        getMethod.setQueryString(params);
        return getMethod.getQueryString();
    }

    class Response {
        private int httpStatus;
        private String data;

        public int getHttpStatus() {
            return httpStatus;
        }

        public void setHttpStatus(int httpStatus) {
            this.httpStatus = httpStatus;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public Response(int httpStatus, String data) {
            super();
            this.httpStatus = httpStatus;
            this.data = data;
        }

        public Response() {
            super();
        }
    }
}
