/*
 * (C) Copyright 2018 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.WebDriverManager.chromedriver;
import static io.github.bonigarcia.wdm.WebDriverManager.edgedriver;
import static io.github.bonigarcia.wdm.WebDriverManager.firefoxdriver;
import static io.github.bonigarcia.wdm.WebDriverManager.iedriver;
import static io.github.bonigarcia.wdm.WebDriverManager.operadriver;
import static io.github.bonigarcia.wdm.WebDriverManager.phantomjs;
import static java.lang.invoke.MethodHandles.lookup;
import static org.apache.commons.io.FileUtils.openInputStream;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import org.slf4j.Logger;

import io.javalin.Context;
import io.javalin.Handler;
import io.javalin.Javalin;

/**
 * WebDriverManager server.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 3.0.0
 */
public class Server {

    final Logger log = getLogger(lookup().lookupClass());

    public Server(int port) {
        Javalin app = Javalin.create().start(port);
        Handler handler = ctx -> {
            String requestMethod = ctx.method();
            String requestPath = ctx.path();
            log.info("Server request: {} {}", requestMethod, requestPath);

            Optional<WebDriverManager> driverManager = createDriverManager(
                    requestPath);
            if (driverManager.isPresent()) {
                resolveDriver(ctx, driverManager.get());
            }
        };

        app.get("/chromedriver", handler);
        app.get("/firefoxdriver", handler);
        app.get("/edgedriver", handler);
        app.get("/iedriver", handler);
        app.get("/operadriver", handler);
        app.get("/phantomjs", handler);

        log.info("WebDriverManager server listening on port {}", port);
    }

    private Optional<WebDriverManager> createDriverManager(String requestPath) {
        Optional<WebDriverManager> out = Optional.empty();
        switch (requestPath.substring(1)) {
        case "chromedriver":
            out = Optional.of(chromedriver());
            break;
        case "firefoxdriver":
            out = Optional.of(firefoxdriver());
            break;
        case "edgedriver":
            out = Optional.of(edgedriver());
            break;
        case "iedriver":
            out = Optional.of(iedriver());
            break;
        case "operadriver":
            out = Optional.of(operadriver());
            break;
        case "phantomjs":
            out = Optional.of(phantomjs());
            break;
        default:
            log.warn("Unknown option {}", requestPath);
            out = Optional.empty();
        }
        return out;
    }

    private synchronized void resolveDriver(Context ctx,
            WebDriverManager driverManager) throws IOException {
        driverManager.setup();
        File binary = new File(driverManager.getBinaryPath());
        String binaryVersion = driverManager.getDownloadedVersion();
        String binaryName = binary.getName();
        String binaryLength = String.valueOf(binary.length());

        ctx.res.setHeader("Content-Disposition",
                "attachment; filename=\"" + binaryName + "\"");
        ctx.result(openInputStream(binary));

        log.info("Server response: {} {} ({} bytes)", binaryName, binaryVersion,
                binaryLength);
    }

    public static void main(String[] args) {
        new Server(WebDriverManager.config().getServerPort());
    }

}
@@ -1285,16 +1285,61 @@
 eption;%0A
+import java.util.List;%0Aimport java.util.Map;%0A
 import j
@@ -3503,16 +3503,635 @@
 eption %7B
+%0A%0A        // Query string (for configuration parameters)%0A        Map%3CString, List%3CString%3E%3E queryParamMap = ctx.queryParamMap();%0A        if (!queryParamMap.isEmpty()) %7B%0A            log.info(%22Using query string for configuration: %7B%7D%22,%0A                    ctx.queryString());%0A            for (String key : queryParamMap.keySet()) %7B%0A                String configKey = %22wdm.%22 + key;%0A                String configValue = queryParamMap.get(key).get(0);%0A                log.info(%22%5Ct%7B%7D = %7B%7D%22, configKey, configValue);%0A                System.setProperty(configKey, configValue);%0A            %7D%0A        %7D%0A%0A        // Resolve driver
 %0A       
@@ -4392,24 +4392,44 @@
 length());%0A%0A
+        // Response%0A
         ctx.
@@ -4570,25 +4570,24 @@
 m(binary));%0A
-%0A
         log.
@@ -4655,16 +4655,16 @@
 ersion,%0A
-
         
@@ -4681,24 +4681,166 @@
 naryLength);
+%0A%0A        // Clear configuration%0A        for (String key : queryParamMap.keySet()) %7B%0A            System.clearProperty(%22wdm.%22 + key);%0A        %7D
 %0A    %7D%0A%0A    

Use query string for wdm configuration in server