package com.sailthru.client.http;

import com.sailthru.client.AbstractSailthruClient.HttpRequestMethod;
import com.sailthru.client.SailthruClient;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 *
 * @author Prajwal Tuladhar <praj@sailthru.com>
 */
public class SailthruHttpClient extends DefaultHttpClient {

    public SailthruHttpClient(ThreadSafeClientConnManager connManager,
			HttpParams params) {
        super(connManager, params);
    }

    private HttpUriRequest buildRequest(String urlString, HttpRequestMethod method, Map<String, String> queryParams) throws UnsupportedEncodingException {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        
        for (Entry<String, String> entry : queryParams.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }

        switch(method) {
            case GET:
                return new HttpGet(urlString + "?" + extractQueryString(nameValuePairs));

            case POST:
                HttpPost httpPost = new HttpPost(urlString);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, SailthruClient.DEFAULT_ENCODING));
                return httpPost;

            case DELETE:
                return new HttpDelete(urlString + "?" + extractQueryString(nameValuePairs));
        }
        return null;
    }
    
    private HttpUriRequest buildRequest(String urlString, HttpRequestMethod method, Map<String, String> queryParams, Map<String, File> files) throws UnsupportedEncodingException {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        
        for( Entry<String, String> entry : queryParams.entrySet() ) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }

        switch(method) {
            case GET:
                return new HttpGet(urlString + "?" + extractQueryString(nameValuePairs));

            case POST:
                HttpPost httpPost = new HttpPost(urlString);
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (Entry<String, String> entry : queryParams.entrySet()) {
                    builder.addTextBody(entry.getKey(), entry.getValue());
                }
                for (Entry<String, File> fileEntry : files.entrySet()) {
                    final String fileKey = fileEntry.getKey();
                    final File file = fileEntry.getValue();
                    final String filename = file.getName();
                    builder.addBinaryBody(fileKey, file, ContentType.APPLICATION_OCTET_STREAM, filename);
                }
                httpPost.setEntity(builder.build());

                return httpPost;

            case DELETE:
                return new HttpDelete(urlString + "?" + extractQueryString(nameValuePairs));
        }
        return null;
    }

    public Object executeHttpRequest(String urlString, HttpRequestMethod method, Map<String, String> params, ResponseHandler<Object> responseHandler, Map<String, String> customHeaders)
            throws IOException {
        HttpUriRequest request = this.buildRequest(urlString, method, params);
        if (customHeaders != null && customHeaders.size() > 0) {
            for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
                final String key = entry.getKey();
                final String value = entry.getValue();
                request.setHeader(key, value);
            }
        }
        
        return super.execute(request, responseHandler);
    }
    
    public Object executeHttpRequest(String urlString, HttpRequestMethod method, Map<String, String> params, Map<String, File> fileParams, ResponseHandler<Object> responseHandler, Map<String, String> customHeaders)
            throws IOException {
        HttpUriRequest request = this.buildRequest(urlString, method, params, fileParams);
        if (customHeaders != null && customHeaders.size() > 0) {
            for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
                final String key = entry.getKey();
                final String value = entry.getValue();
                request.setHeader(key, value);
            }
        }
        return super.execute(request, responseHandler);
    }

    private String extractQueryString(List<NameValuePair> params) {
        return URLEncodedUtils.format(params, SailthruClient.DEFAULT_ENCODING);
    }
}

@@ -927,16 +927,27 @@
 .io.File
+InputStream
 ;%0Aimport
@@ -1119,67 +1119,8 @@
 y;%0A%0A
-/**%0A *%0A * @author Prajwal Tuladhar %3Cpraj@sailthru.com%3E%0A */%0A
 publ
@@ -1175,17 +1175,16 @@
 lient %7B%0A
-%0A
     publ
@@ -1541,32 +1541,24 @@
 luePair%3E();%0A
-        
 %0A        for
@@ -2234,28 +2234,24 @@
 null;%0A    %7D%0A
-    
 %0A    private
@@ -2368,24 +2368,35 @@
 String, File
+InputStream
 %3E files) thr
@@ -2507,24 +2507,16 @@
 air%3E();%0A
-        
 %0A       
@@ -3259,24 +3259,35 @@
 String, File
+InputStream
 %3E fileEntry 
@@ -3401,16 +3401,27 @@
 nal File
+InputStream
  file = 
@@ -3490,22 +3490,29 @@
 e = 
-file.getName()
+%22import_job_data.csv%22
 ;%0A  
@@ -4696,16 +4696,27 @@
 ng, File
+InputStream
 %3E filePa

Make SailthruHttpClient accept FileInputStream instead of File