package nz.ac.auckland.scriptella.driver.http;

/**
 * Created by Jack on 13/02/2015.
 */
import org.apache.commons.httpclient.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scriptella.configuration.StringResource;
import scriptella.spi.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;


public class HTTPConnection extends AbstractConnection {

    private String host;
    private String type;
    private int timeOut;
    CloseableHttpClient httpClient;

    Logger logger = LoggerFactory.getLogger("HttpConnection");

    public HTTPConnection() {} // Override default constructor

    public HTTPConnection(String host, String type, int timeOut) {
        this.host = host;
        this.type = type;
        this.timeOut = timeOut;
    }

    public HTTPConnection(ConnectionParameters connectionParameters){
        host = connectionParameters.getStringProperty("url");
        type = connectionParameters.getStringProperty("type");
        timeOut = connectionParameters.getIntegerProperty("timeout");

    }


    @Override
    public void executeScript(Resource resource, ParametersCallback parametersCallback) throws ProviderException {
        run(resource);
    }

    @Override
    public void executeQuery(Resource resource, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {

    }

    private void run(Resource resource){
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(timeOut )
                .setConnectionRequestTimeout(timeOut)
                .setSocketTimeout(timeOut).build();
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

        HttpResponse httpResponse;
        HttpGet httpGet = null;
        HttpPost httpPost = null;

        try {
            if (type.toUpperCase().equals("GET")) {

                URIBuilder uriBuilder = new URIBuilder(host);
                uriBuilder.addParameters(generateParams(resource));

                httpGet = new HttpGet(uriBuilder.build());

                httpResponse = httpClient.execute(httpGet);
            } else { // Post
                httpPost = new HttpPost(host);

                httpPost.setEntity(new UrlEncodedFormEntity(generateParams(resource)));

                httpResponse = httpClient.execute(httpPost);
            }

            logger.info("Response Status: {}", httpResponse.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String l= "";
            while ((l = rd.readLine()) != null) {
                System.out.println(l);
            }

        } catch (HttpException e) {
            logger.error("HTTP Error: ",e);
        } catch (IOException e) {
            logger.error("IO Error: ", e);
        } catch (URISyntaxException e) {
            logger.error("URI Error: ",e);
        } finally {
            if (httpGet != null) {
                httpGet.releaseConnection();
            }
            if (httpPost != null) {
                httpPost.releaseConnection();
            }
        }

    }

    private List<NameValuePair> generateParams( Resource resource){
        BufferedReader br = new BufferedReader(new StringReader(((StringResource)resource).getString()));
        String line;

        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(1);

        try {
            while ((line = br.readLine()) != null) {
                String [] components = line.split("=");
                nameValuePairList.add(new BasicNameValuePair(components[0], components[1]));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return nameValuePairList;
    }


    @Override
    public void close() throws ProviderException {

    }
}

@@ -1684,25 +1684,24 @@
 %22timeout%22);%0A
-%0A
     %7D%0A%0A%0A    
@@ -3115,251 +3115,8 @@
 ());
-%0A            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));%0A            String l= %22%22;%0A            while ((l = rd.readLine()) != null) %7B%0A                System.out.println(l);%0A            %7D
 %0A%0A  
@@ -4140,26 +4140,40 @@
     
-e.printStackTrace(
+logger.error(%22IO exception: %22, e
 );%0A 

Update HTTPConnection.java