package com.litle.sdk;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

//import com.phoenix.common.util.FileFunctions;
//import com.phoenix.common.util.StreamData;

public class Communication {

	private DefaultHttpClient httpclient;

	public Communication() {
		httpclient = new DefaultHttpClient();
	}

	public String requestToServer(String xmlRequest, Properties configuration) {
		String xmlResponse = null;
		String proxyHost = configuration.getProperty("proxyHost");
		String proxyPort = configuration.getProperty("proxyPort");
		if (proxyHost != null && proxyHost.length() > 0 && proxyPort != null
				&& proxyHost.length() > 0) {
			HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort));
			httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
			httpclient.getParams().setParameter(CoreConnectionPNames.SO_LINGER, 0);
		}

		HttpPost post = new HttpPost(configuration.getProperty("url"));
		post.setHeader("Content-Type", "text/xml");
		post.setHeader("Connection","close");
		HttpEntity entity = null;
		try {
			boolean printxml = configuration.getProperty("printxml") != null
					&& configuration.getProperty("printxml").equalsIgnoreCase(
							"true");
			if (printxml) {
				System.out.println("Request XML: " + xmlRequest);
			}
			post.setEntity(new StringEntity(xmlRequest));
			
			HttpResponse response = httpclient.execute(post);
			if(response.getStatusLine().getStatusCode() != 200) {
				throw new LitleOnlineException(response.getStatusLine().getStatusCode() + ":" + response.getStatusLine().getReasonPhrase());
			}
			entity = response.getEntity();
			xmlResponse = EntityUtils.toString(entity);			

			if (printxml) {
				System.out.println("Response XML: " + xmlResponse);
			}
		} catch (IOException e) {
			throw new LitleOnlineException("Exception connection to Litle", e);
		} finally {
			if(entity != null) {
				EntityUtils.consumeQuietly(entity);
			}
			post.abort();
		}
		return xmlResponse;
	}

	public LitleBatchFileResponse sendLitleBatchFileToIBC(File requestFile, String resposeFilePath, Properties configuration) throws Exception {

			String hostName = configuration.getProperty("batchHost");
			String hostPort = configuration.getProperty("batchPort");
			int tcpTimeout = Integer.parseInt(configuration.getProperty("batchTcpTimeout"));
			boolean useSSL = configuration.getProperty("batchUseSSL") != null
					&& configuration.getProperty("batchUseSSL").equalsIgnoreCase(
							"true");
			
	        // connect to the ibc and send the data
	        StreamData streamData = new StreamData();
	        streamData.init(hostName, hostPort, tcpTimeout, useSSL);
	        streamData.dataOut(requestFile);

	        String content = streamData.dataIn();
	        File responseFile = new File(resposeFilePath);
 
			// if file doesnt exists, then create it
			if (!responseFile.exists()) {
				responseFile.createNewFile();
			}
 
			FileWriter fw = new FileWriter(responseFile.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
	        
	        streamData.closeSocket();
	        
	        LitleBatchFileResponse retObj = new LitleBatchFileResponse(null);
	       
			return retObj;
	}
	
}

@@ -713,16 +713,51 @@
 pclient;
+%0A    private StreamData streamData;
 %0A%0A%09publi
@@ -816,16 +816,49 @@
 ient();%0A
+%09%09streamData = new StreamData();%0A
 %09%7D%0A%0A%09pub
@@ -2664,18 +2664,16 @@
 ption %7B%0A
-%0A%09
 %09%09String
@@ -2726,17 +2726,16 @@
 st%22);%0A%09%09
-%09
 String h
@@ -2784,17 +2784,16 @@
 Port%22);%0A
-%09
 %09%09int tc
@@ -2869,17 +2869,16 @@
 t%22));%0A%09%09
-%09
 boolean 
@@ -2931,33 +2931,32 @@
 L%22) != null%0A%09%09%09%09
-%09
 && configuration
@@ -2996,32 +2996,24 @@
 sIgnoreCase(
-%0A%09%09%09%09%09%09%09
 %22true%22);%0A%09%09%09
@@ -3015,119 +3015,8 @@
 ;%0A%09%09
-%09%0A%09        // connect to the ibc and send the data%0A%09        StreamData streamData = new StreamData();%0A%09        
 stre
@@ -3068,25 +3068,19 @@
 seSSL);%0A
-%09        
+%0A%09%09
 streamDa
@@ -3106,24 +3106,17 @@
 ile);%0A%0A%09
-        
+%09
 String c
@@ -3146,24 +3146,17 @@
 aIn();%0A%09
-        
+%09
 File res
@@ -3194,19 +3194,17 @@
 ePath);%0A
- %0A%09
+%0A
 %09%09// if 
@@ -3238,17 +3238,16 @@
 eate it%0A
-%09
 %09%09if (!r
@@ -3273,17 +3273,16 @@
 )) %7B%0A%09%09%09
-%09
 response
@@ -3309,14 +3309,11 @@
 ;%0A%09%09
-%09
 %7D%0A
- %0A%09
+%0A
 %09%09Fi
@@ -3376,17 +3376,16 @@
 e());%0A%09%09
-%09
 Buffered
@@ -3422,17 +3422,16 @@
 (fw);%0A%09%09
-%09
 bw.write
@@ -3441,17 +3441,16 @@
 ntent);%0A
-%09
 %09%09bw.clo
@@ -3455,35 +3455,19 @@
 lose();%0A
-%09        %0A%09        
+%0A%09%09
 streamDa
@@ -3488,27 +3488,11 @@
 ();%0A
-%09        %0A%09        
+%0A%09%09
 Litl
@@ -3557,18 +3557,9 @@
 l);%0A
-%09       %0A%09
+%0A
 %09%09re

Refactor StreamData to be a class member for easier testing