package PostAction;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;

import System.Sys;

public class AddAudio {
	HttpParams params;
	BasicHttpProcessor httpproc;
	HttpRequestExecutor httpexecutor;
	HttpContext context;
	HttpHost host;
	DefaultHttpClientConnection conn;
	ConnectionReuseStrategy connStrategy;
	
	private final String host_str = Sys.HOST;
	private final int port = Sys.port;
	
	public AddAudio() {
		params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
        HttpProtocolParams.setUseExpectContinue(params, true);
        
        httpproc = new BasicHttpProcessor();
        httpproc.addInterceptor(new RequestContent());
        httpproc.addInterceptor(new RequestTargetHost());
        httpproc.addInterceptor(new RequestConnControl());
        httpproc.addInterceptor(new RequestUserAgent());
        httpproc.addInterceptor(new RequestExpectContinue());
        
        httpexecutor = new HttpRequestExecutor();
        context = new BasicHttpContext(null);
        host = new HttpHost(host_str, port);
        conn = new DefaultHttpClientConnection();
        connStrategy = new DefaultConnectionReuseStrategy();
        
        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
	}
	
	public void addAudioToEvent(String path, int eid) throws Exception{	
		try {
			File video_file = new File(path);
			FileEntity fe = new FileEntity(video_file, Sys.MIME_AUDIO);
			
			
			if (!conn.isOpen()) {
                Socket socket = new Socket(host.getHostName(), host.getPort());
                conn.bind(socket, params);
                BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", 
                    "/eventbook/doServlet/AddAudio");
                
                request.setEntity(fe);
                
                System.out.println(">> Request URI: " + request.getRequestLine().getUri());

                request.setParams(params);
                //context.setAttribute("type", type);
                httpexecutor.preProcess(request, httpproc, context);
                HttpResponse response = httpexecutor.execute(request, conn, context);
                response.setParams(params);
               // response.addHeader("Content-Length", buffer.length + "");
               // response.setHeader("Content-type", "application/octec-stream");
                httpexecutor.postProcess(response, httpproc, context);
        
                System.out.println("<< Response: " + response.getStatusLine());
                //System.out.println(EntityUtils.toString(response.getEntity()));
                System.out.println("==============");
                if (!connStrategy.keepAlive(response, context)) {
                    conn.close();
                } else {
                    System.out.println("Connection kept alive...");
                }
			}
		} finally {
			conn.close();
		}
	}
	
	/**
	public static void main(String[] args) throws Exception {
		AddAudio aa = new AddAudio();
		String path = "H:/onenight.mp3";
		aa.addAudioToEvent(path, 2);
	}
	*/
}
