// Path: force-update/src/main/java/net/skoumal/forceupdate/VersionProvider.java
// public interface VersionProvider {
// 
//     VersionResult getVersionResult();
// 
// }
// 
// Path: force-update/src/main/java/net/skoumal/forceupdate/VersionResult.java
// public class VersionResult {
// 
//     private List<Version> versionList = new ArrayList<>(1);
// 
//     private List<String> payloadList = new ArrayList<>(1);
// 
//     private boolean isError = false;
// 
//     private String errorMessage = null;
// 
//     private Exception errorException = null;
// 
//     public VersionResult(Version gVersion, String gPayload) {
//         versionList.add(gVersion);
//         payloadList.add(gPayload);
//     }
// 
//     public VersionResult(String gErrorMessage, Exception gErrorException) {
//         errorMessage = gErrorMessage;
//         errorException = gErrorException;
//         isError = true;
//     }
// 
//     public VersionResult(List<Version> gVersionList, List<String> gPayloadList) {
//         versionList = gVersionList;
//         payloadList = gPayloadList;
//     }
// 
//     public Version getVersion() {
//         if(isError || versionList.size() < 1) {
//             return null;
//         } else {
//             return versionList.get(0);
//         }
//     }
// 
//     public String getPayload() {
//         if(isError || payloadList.size() < 1) {
//             return null;
//         } else {
//             return payloadList.get(0);
//         }
//     }
// 
//     public List<Version> getVersionList() {
//         return versionList;
//     }
// 
//     public List<String> getPayloadList() {
//         return payloadList;
//     }
// 
//     public String getErrorMessage() {
//         return errorMessage;
//     }
// 
//     public Exception getErrorException() {
//         return errorException;
//     }
// 
//     public boolean isError() {
//         return isError;
//     }
// }
// 
// Path: force-update/src/main/java/net/skoumal/forceupdate/util/Http.java
// public class Http {
// 
//     private static final int HTTP_TIMEOUT = 10 * 1000;
// 
//     public static String loadString(URL gUrl) throws IOException {
//         HttpURLConnection urlConnection = null;
//         try {
// 
//             urlConnection = (HttpURLConnection) gUrl.openConnection();
//             urlConnection.setRequestMethod("GET");
//             urlConnection.setUseCaches(false);
//             urlConnection.setConnectTimeout(HTTP_TIMEOUT);
//             urlConnection.setReadTimeout(HTTP_TIMEOUT);
//             urlConnection.connect();
// 
//             int status = urlConnection.getResponseCode();
//             if(status >= 200 && status < 300) {
// 
//                 //TODO [1] fix this to not add new line sign to last line
//                 InputStream inputStream = urlConnection.getInputStream();
//                 BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream));
//                 StringBuilder response = new StringBuilder();
//                 String line;
//                 boolean firstLine = true;
//                 while ((line = responseReader.readLine()) != null) {
//                     if(firstLine) {
//                         firstLine = false;
//                     } else {
//                         response.append('\n');
//                     }
//                     response.append(line);
//                 }
// 
//                 return response.toString();
//             } else {
//                 throw new IOException("HTTP status code " + status + " returned.");
//             }
// 
//         } finally {
//             if(urlConnection != null) {
//                 urlConnection.disconnect();
//             }
//         }
//     }
// 
// }


import net.skoumal.forceupdate.VersionProvider;
import net.skoumal.forceupdate.VersionResult;
import net.skoumal.forceupdate.util.Http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

package net.skoumal.forceupdate.provider;



/**
 * Created by gingo on 26.9.2016.
 */
public abstract class AbstractHttpVersionProvider implements VersionProvider {

    private URL url;

    public AbstractHttpVersionProvider(String gUrl) {

        try {
            url = new URL(gUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public VersionResult getVersionResult() {

        try {
            String stringResponse = Http.loadString(url);
            return processHttpResponse(stringResponse);
        } catch (IOException e) {
            return new VersionResult(e.toString(), e);
        }

    }

    public String getUrl() {
        return url.toString();
    }


// Path: force-update/src/main/java/net/skoumal/forceupdate/VersionProvider.java
// public interface VersionProvider {
// 
//     VersionResult getVersionResult();
// 
// }
// 
// Path: force-update/src/main/java/net/skoumal/forceupdate/VersionResult.java
// public class VersionResult {
// 
//     private List<Version> versionList = new ArrayList<>(1);
// 
//     private List<String> payloadList = new ArrayList<>(1);
// 
//     private boolean isError = false;
// 
//     private String errorMessage = null;
// 
//     private Exception errorException = null;
// 
//     public VersionResult(Version gVersion, String gPayload) {
//         versionList.add(gVersion);
//         payloadList.add(gPayload);
//     }
// 
//     public VersionResult(String gErrorMessage, Exception gErrorException) {
//         errorMessage = gErrorMessage;
//         errorException = gErrorException;
//         isError = true;
//     }
// 
//     public VersionResult(List<Version> gVersionList, List<String> gPayloadList) {
//         versionList = gVersionList;
//         payloadList = gPayloadList;
//     }
// 
//     public Version getVersion() {
//         if(isError || versionList.size() < 1) {
//             return null;
//         } else {
//             return versionList.get(0);
//         }
//     }
// 
//     public String getPayload() {
//         if(isError || payloadList.size() < 1) {
//             return null;
//         } else {
//             return payloadList.get(0);
//         }
//     }
// 
//     public List<Version> getVersionList() {
//         return versionList;
//     }
// 
//     public List<String> getPayloadList() {
//         return payloadList;
//     }
// 
//     public String getErrorMessage() {
//         return errorMessage;
//     }
// 
//     public Exception getErrorException() {
//         return errorException;
//     }
// 
//     public boolean isError() {
//         return isError;
//     }
// }
// 
// Path: force-update/src/main/java/net/skoumal/forceupdate/util/Http.java
// public class Http {
// 
//     private static final int HTTP_TIMEOUT = 10 * 1000;
// 
//     public static String loadString(URL gUrl) throws IOException {
//         HttpURLConnection urlConnection = null;
//         try {
// 
//             urlConnection = (HttpURLConnection) gUrl.openConnection();
//             urlConnection.setRequestMethod("GET");
//             urlConnection.setUseCaches(false);
//             urlConnection.setConnectTimeout(HTTP_TIMEOUT);
//             urlConnection.setReadTimeout(HTTP_TIMEOUT);
//             urlConnection.connect();
// 
//             int status = urlConnection.getResponseCode();
//             if(status >= 200 && status < 300) {
// 
//                 //TODO [1] fix this to not add new line sign to last line
//                 InputStream inputStream = urlConnection.getInputStream();
//                 BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream));
//                 StringBuilder response = new StringBuilder();
//                 String line;
//                 boolean firstLine = true;
//                 while ((line = responseReader.readLine()) != null) {
//                     if(firstLine) {
//                         firstLine = false;
//                     } else {
//                         response.append('\n');
//                     }
//                     response.append(line);
//                 }
// 
//                 return response.toString();
//             } else {
//                 throw new IOException("HTTP status code " + status + " returned.");
//             }
// 
//         } finally {
//             if(urlConnection != null) {
//                 urlConnection.disconnect();
//             }
//         }
//     }
// 
// }


// Path: force-update/src/main/java/net/skoumal/forceupdate/provider/AbstractHttpVersionProvider.java
import net.skoumal.forceupdate.VersionProvider;
import net.skoumal.forceupdate.VersionResult;
import net.skoumal.forceupdate.util.Http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

package net.skoumal.forceupdate.provider;



/**
 * Created by gingo on 26.9.2016.
 */
public abstract class AbstractHttpVersionProvider implements VersionProvider {

    private URL url;

    public AbstractHttpVersionProvider(String gUrl) {

        try {
            url = new URL(gUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public VersionResult getVersionResult() {

        try {
            String stringResponse = Http.loadString(url);
            return processHttpResponse(stringResponse);
        } catch (IOException e) {
            return new VersionResult(e.toString(), e);
        }

    }

    public String getUrl() {
        return url.toString();
    }


    protected abstract VersionResult processHttpResponse(String gResponseString);
