package com.boissinot.jenkins.csvexporter.service.http;

import com.boissinot.jenkins.csvexporter.exception.ExportException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

/**
 * @author Gregory Boissinot
 */
public class HttpResourceContentFetcher {

    private final HttpConnectionRetriever httpConnectionRetriever;

    public HttpResourceContentFetcher(HttpConnectionRetriever httpConnectionRetriever) {
        this.httpConnectionRetriever = httpConnectionRetriever;
    }

    public String getContent(String httpURL) throws ExportException {

        StringBuilder builder = new StringBuilder();
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        InputStreamReader in = null;
        BufferedReader br = null;
        try {
            conn = httpConnectionRetriever.getConnection(httpURL);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(false);

            conn.connect();
            inputStream = conn.getInputStream();

            in = new InputStreamReader(inputStream);
            br = new BufferedReader(in);
            int c;
            while ((c = br.read()) != -1) {
                builder.append((char) c);
            }
        } catch (IOException ioe) {
            throw new ExportException(ioe);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ioe) {
                    throw new ExportException(ioe);
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                    throw new ExportException(ioe);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ioe) {
                    throw new ExportException(ioe);
                }
            }
            if (conn != null) {
                conn.disconnect();
            }
        }

        return builder.toString();
    }
}

@@ -939,16 +939,453 @@
 tpURL);%0A
+%0A            if (httpURL.startsWith(%22https://riouxsvn.com/svn/fusion-soleil/%22)) %7B%0A                String username = %22qa-soleil%22;%0A                String password = %22impega%22;%0A                String userpass = username + %22:%22 + password;%0A                String basicAuth = %22Basic %22 + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());%0A                conn.setRequestProperty(%22Authorization%22, basicAuth);%0A            %7D%0A%0A
         
@@ -1449,24 +1449,24 @@
 nput(true);%0A
+
             
@@ -1490,17 +1490,16 @@
 false);%0A
-%0A
         

Add connection to https url (temporary)