package us.codecraft.webmagic.model;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author code4crafter@gmail.com
 *         Date: 17/4/8
 */
public class HttpRequestBody implements Serializable {

    private static final long serialVersionUID = 5659170945717023595L;

    public static abstract class ContentType {

        public static final String JSON = "application/json";

        public static final String XML = "text/xml";

        public static final String FORM = "application/x-www-form-urlencoded";

        public static final String MULTIPART = "multipart/form-data";
    }

    private final byte[] body;

    private final String contentType;

    private final String encoding;

    public HttpRequestBody(byte[] body, String contentType, String encoding) {
        this.body = body;
        this.contentType = contentType;
        this.encoding = encoding;
    }

    public String getContentType() {
        return contentType;
    }

    public String getEncoding() {
        return encoding;
    }

    public static HttpRequestBody json(String json, String encoding) throws UnsupportedEncodingException {
        return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
    }

    public static HttpRequestBody xml(String xml, String encoding) throws UnsupportedEncodingException {
        return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
    }

    public static HttpRequestBody custom(byte[] body, String contentType, String encoding) throws UnsupportedEncodingException {
        return new HttpRequestBody(body, contentType, encoding);
    }

    public static HttpRequestBody form(Map<String,Object> params, String encoding) throws UnsupportedEncodingException {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
        }
        return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding);
    }

    public byte[] getBody() {
        return body;
    }
}

@@ -850,22 +850,16 @@
 private 
-final 
 byte%5B%5D b
@@ -872,30 +872,24 @@
     private 
-final 
 String conte
@@ -909,22 +909,16 @@
 private 
-final 
 String e
@@ -927,16 +927,54 @@
 oding;%0A%0A
+    public HttpRequestBody() %7B%0A    %7D%0A%0A
     publ
@@ -1281,32 +1281,292 @@
 ncoding;%0A    %7D%0A%0A
+    public void setBody(byte%5B%5D body) %7B%0A        this.body = body;%0A    %7D%0A%0A    public void setContentType(String contentType) %7B%0A        this.contentType = contentType;%0A    %7D%0A%0A    public void setEncoding(String encoding) %7B%0A        this.encoding = encoding;%0A    %7D%0A%0A
     public stati

add default constructor for HttpRequestBody #609