/*
 * (C) Copyright 2015 Boni Garcia (http://bonigarcia.github.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package io.github.bonigarcia.wdm;

import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
import static org.apache.http.HttpHeaders.USER_AGENT;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Optional;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

/**
 * Manager for Chrome.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.0.0
 */
public class ChromeDriverManager extends WebDriverManager {

    @Override
    protected DriverManagerType getDriverManagerType() {
        return CHROME;
    }

    @Override
    protected String getDriverName() {
        return "chromedriver";
    }

    @Override
    protected String getDriverVersion() {
        return config().getChromeDriverVersion();
    }

    @Override
    protected URL getDriverUrl() {
        return getDriverUrlCkeckingMirror(config().getChromeDriverUrl());
    }

    @Override
    protected Optional<URL> getMirrorUrl() {
        return Optional.of(config().getChromeDriverMirrorUrl());
    }

    @Override
    protected Optional<String> getExportParameter() {
        return Optional.of(config().getChromeDriverExport());
    }

    @Override
    protected void setDriverVersion(String version) {
        config().setChromeDriverVersion(version);
    }

    @Override
    protected void setDriverUrl(URL url) {
        config().setChromeDriverUrl(url);
    }

    @Override
    protected List<URL> getDrivers() throws IOException {
        URL driverUrl = getDriverUrl();
        List<URL> urls;
        if (isUsingTaobaoMirror()) {
            urls = getDriversFromMirror(driverUrl);
        } else {
            urls = getDriversFromXml(driverUrl);
        }
        return urls;
    }

    @Override
    protected String getCurrentVersion(URL url, String driverName) {
        if (isUsingTaobaoMirror()) {
            int i = url.getFile().lastIndexOf(SLASH);
            int j = url.getFile().substring(0, i).lastIndexOf(SLASH) + 1;
            return url.getFile().substring(j, i);
        } else {
            return super.getCurrentVersion(url, driverName);
        }
    }

    @Override
    protected Optional<String> getBrowserVersion() {
        return getDefaultBrowserVersion(getProgramFilesEnv(),
                "\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe",
                "google-chrome",
                "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
                "--version", getDriverManagerType().toString());
    }

    @Override
    protected Optional<String> getLatestVersion() {
        String url = config().getChromeDriverUrl() + "LATEST_RELEASE";
        HttpGet request = new HttpGet(url);
        request.addHeader("User-Agent", USER_AGENT);
        HttpResponse response;
        Optional<String> version = Optional.empty();
        try {
            response = httpClient.execute(request);
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));
            version = Optional.of(IOUtils.toString(rd));
        } catch (IOException e) {
            log.warn("Exception reading {} to get latest version of {}", url,
                    getDriverName(), e);
        }
        log.debug("Latest version of {} according to {} is {}", getDriverName(),
                url, version);
        return version;
    }

}

@@ -761,81 +761,50 @@
 tic 
-org.apache.http.HttpHeaders.USER_AGENT;%0D%0A%0D%0Aimport java.io.BufferedReader;
+java.nio.charset.Charset.defaultCharset;%0D%0A
 %0D%0Aim
@@ -856,22 +856,16 @@
 utStream
-Reader
 ;%0D%0Aimpor
@@ -977,94 +977,8 @@
 ls;%0D
-%0Aimport org.apache.http.HttpResponse;%0D%0Aimport org.apache.http.client.methods.HttpGet;%0D
 %0A%0D%0A/
@@ -3393,139 +3393,8 @@
 %22;%0D%0A
-        HttpGet request = new HttpGet(url);%0D%0A        request.addHeader(%22User-Agent%22, USER_AGENT);%0D%0A        HttpResponse response;%0D%0A
     
@@ -3470,16 +3470,28 @@
         
+InputStream 
 response
@@ -3507,143 +3507,112 @@
 ient
-.execute(request);%0D%0A            BufferedReader rd = new BufferedReader(%0D%0A                    new InputStreamReader(response.getEntity()
+%0D%0A                    .execute(httpClient.createHttpGet(new URL(url))).getEntity()%0D%0A                    
 .get
@@ -3620,18 +3620,16 @@
 ontent()
-))
 ;%0D%0A     
@@ -3675,17 +3675,41 @@
 String(r
-d
+esponse, defaultCharset()
 ));%0D%0A   
@@ -3722,18 +3722,16 @@
  catch (
-IO
 Exceptio
@@ -3862,32 +3862,72 @@
 e);%0D%0A        %7D%0D%0A
+        if (version.isPresent()) %7B%0D%0A    
         log.debu
@@ -3977,57 +3977,78 @@
 %7B%7D%22,
- getDriverName(),%0D%0A                url, version);
+%0D%0A                    getDriverName(), url, version.get());%0D%0A        %7D
 %0D%0A  

Simplify getLatest logic for chromedriver