// Path: websnapshot-api/src/main/java/org/asqatasun/websnapshot/urlmanager/utils/UrlUtils.java
// public final class UrlUtils {
// 
//     public static final String URL_AVAILABLE = "OK";
//     public static final String URL_NOT_AVAILABLE = "This url is not available";
//     private static final int MIN_HTTP_VALID_CODE = 200;
//     private static final int MAX_HTTP_VALID_CODE = 400;
// 
//     /**
//      * private constructor
//      */
//     private UrlUtils() {
//     }
// 
//     /**
//      *
//      * @param url
//      * @return
//      */
//     public static boolean checkIfURLIsValid(String url) {
//         String[] schemes = {"http", "https"};
//         UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
//         return urlValidator.isValid(url);
//     }
// 
//     /**
//      *
//      * @param targetUrl
//      * @return
//      */
//     public static String checkURLAvailable(String targetUrl) {
//         try {
//             URL url = new URL(targetUrl);
//             URLConnection urlConnection = url.openConnection();
// 
//             HttpURLConnection.setFollowRedirects(true);
//             HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
//             httpURLConnection.setRequestMethod("HEAD");
// 
//             if (httpURLConnection.getResponseCode() >= MIN_HTTP_VALID_CODE
//                     && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
//                 return URL_AVAILABLE;
//             }
//             urlConnection = url.openConnection();
//             httpURLConnection.disconnect();
//             HttpURLConnection.setFollowRedirects(true);
//             httpURLConnection = (HttpURLConnection) urlConnection;
//             httpURLConnection.setRequestMethod("GET");
// 
//             if (httpURLConnection.getResponseCode()
//                     >= MIN_HTTP_VALID_CODE
//                     && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
//                 return URL_AVAILABLE;
//             } else {
//                 return Integer.toString(httpURLConnection.getResponseCode()) + " " + httpURLConnection.getResponseMessage();
//             }
//         } catch (Exception e) {
//             return URL_NOT_AVAILABLE;
//         }
//     }
// }


import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.asqatasun.websnapshot.urlmanager.utils.UrlUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
        if (webDriver.equals(FIREFOX_BROWSER_NAME)) {
            driver = new FirefoxDriver(new FirefoxBinary(new File(firefoxBinaryPath)), new FirefoxProfile());
        } else {
            DesiredCapabilities caps = DesiredCapabilities.phantomjs();
            caps.setCapability(
                    PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                    phantomJsBinaryPath);
            driver = new PhantomJSDriver(caps);
        }
        driver.manage().window().setSize(new Dimension(windowWidth, windowHeight));
        return driver;
    }

    /**
     *
     * @param driver
     * @param url
     */
    private String loadPage(RemoteWebDriver driver, String url) {
        try {
            driver.get(url);
            driver.executeScript("if (getComputedStyle(document.body, null).backgroundColor === 'rgba(0, 0, 0, 0)'"
                    + "|| getComputedStyle(document.body, null).backgroundColor === 'transparent') {"
                    + "document.body.style.backgroundColor = 'white';"
                    + "}");
        } catch (Exception e) {
            Logger.getLogger(this.getClass()).debug("Cannot load this url : " + url);
            return SnapshotCreationResponse.ERROR;
        }
        if (!url.equals(driver.getCurrentUrl())) {
// Path: websnapshot-api/src/main/java/org/asqatasun/websnapshot/urlmanager/utils/UrlUtils.java
// public final class UrlUtils {
// 
//     public static final String URL_AVAILABLE = "OK";
//     public static final String URL_NOT_AVAILABLE = "This url is not available";
//     private static final int MIN_HTTP_VALID_CODE = 200;
//     private static final int MAX_HTTP_VALID_CODE = 400;
// 
//     /**
//      * private constructor
//      */
//     private UrlUtils() {
//     }
// 
//     /**
//      *
//      * @param url
//      * @return
//      */
//     public static boolean checkIfURLIsValid(String url) {
//         String[] schemes = {"http", "https"};
//         UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
//         return urlValidator.isValid(url);
//     }
// 
//     /**
//      *
//      * @param targetUrl
//      * @return
//      */
//     public static String checkURLAvailable(String targetUrl) {
//         try {
//             URL url = new URL(targetUrl);
//             URLConnection urlConnection = url.openConnection();
// 
//             HttpURLConnection.setFollowRedirects(true);
//             HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
//             httpURLConnection.setRequestMethod("HEAD");
// 
//             if (httpURLConnection.getResponseCode() >= MIN_HTTP_VALID_CODE
//                     && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
//                 return URL_AVAILABLE;
//             }
//             urlConnection = url.openConnection();
//             httpURLConnection.disconnect();
//             HttpURLConnection.setFollowRedirects(true);
//             httpURLConnection = (HttpURLConnection) urlConnection;
//             httpURLConnection.setRequestMethod("GET");
// 
//             if (httpURLConnection.getResponseCode()
//                     >= MIN_HTTP_VALID_CODE
//                     && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
//                 return URL_AVAILABLE;
//             } else {
//                 return Integer.toString(httpURLConnection.getResponseCode()) + " " + httpURLConnection.getResponseMessage();
//             }
//         } catch (Exception e) {
//             return URL_NOT_AVAILABLE;
//         }
//     }
// }


// Path: websnapshot-service/src/main/java/org/asqatasun/websnapshot/service/SnapshotCreatorImpl.java
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.asqatasun.websnapshot.urlmanager.utils.UrlUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;

        if (webDriver.equals(FIREFOX_BROWSER_NAME)) {
            driver = new FirefoxDriver(new FirefoxBinary(new File(firefoxBinaryPath)), new FirefoxProfile());
        } else {
            DesiredCapabilities caps = DesiredCapabilities.phantomjs();
            caps.setCapability(
                    PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                    phantomJsBinaryPath);
            driver = new PhantomJSDriver(caps);
        }
        driver.manage().window().setSize(new Dimension(windowWidth, windowHeight));
        return driver;
    }

    /**
     *
     * @param driver
     * @param url
     */
    private String loadPage(RemoteWebDriver driver, String url) {
        try {
            driver.get(url);
            driver.executeScript("if (getComputedStyle(document.body, null).backgroundColor === 'rgba(0, 0, 0, 0)'"
                    + "|| getComputedStyle(document.body, null).backgroundColor === 'transparent') {"
                    + "document.body.style.backgroundColor = 'white';"
                    + "}");
        } catch (Exception e) {
            Logger.getLogger(this.getClass()).debug("Cannot load this url : " + url);
            return SnapshotCreationResponse.ERROR;
        }
        if (!url.equals(driver.getCurrentUrl())) {
            String urlAvailibility = UrlUtils.checkURLAvailable(driver.getCurrentUrl());