package com.nirima.jenkins.plugins.docker;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildVariableContributor;
import hudson.model.Executor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shaded.org.apache.http.client.utils.URIBuilder;

import java.net.URISyntaxException;
import java.util.Map;

/**
 * Contribute docker related vars in build.
 * TODO seems this will never satisfy all use cases and dumping inspect json into WS
 * will be more generic solution
 *
 * @author Kanstantsin Shautsou
 */
@Extension
public class DockerBuildVariableContributor extends BuildVariableContributor {
    private static final Logger LOG = LoggerFactory.getLogger(DockerBuildVariableContributor.class);

    @Override
    public void buildVariablesFor(AbstractBuild build, Map<String, String> variables) {
        final Executor executor = build.getExecutor();
        if (executor != null && executor.getOwner() instanceof DockerComputer) {
            final DockerComputer dockerComputer = (DockerComputer) executor.getOwner();
            variables.put("DOCKER_CONTAINER_ID", dockerComputer.getContainerId());
            variables.put("JENKINS_CLOUD_ID", dockerComputer.getCloudId());

            final DockerCloud cloud = dockerComputer.getCloud();
            if (cloud.isExposeDockerHost()) {
                try {
                    //replace http:// and https:// from docker-java to tcp://
                    final URIBuilder uriBuilder = new URIBuilder(cloud.getServerUrl());
                    if (!uriBuilder.getScheme().equals("unix")) {
                        uriBuilder.setScheme("tcp");
                    }
                    final String dockerHost = uriBuilder.toString();
                    variables.put("DOCKER_HOST", dockerHost);
                } catch (URISyntaxException e) {
                    LOG.error("Can't build 'DOCKER_HOST' var: {}", e.getMessage());
                }
            }
        }
    }
}

@@ -234,100 +234,8 @@
 ry;%0A
-import shaded.org.apache.http.client.utils.URIBuilder;%0A%0Aimport java.net.URISyntaxException;%0A
 impo
@@ -1251,34 +1251,8 @@
 ) %7B%0A
-                try %7B%0A    
     
@@ -1341,57 +1341,28 @@
     
-    final URIBuilder uriBuilder = new URIBuilder(
+String dockerHost = 
 clou
@@ -1377,17 +1377,16 @@
 verUrl()
-)
 ;%0A      
@@ -1399,52 +1399,40 @@
     
-  
-  
 if (
-!uriBuilder.getScheme().equals
+dockerHost.startsWith
 (%22unix
+:
 %22)) 
@@ -1457,136 +1457,73 @@
     
-   
- uriBuilder.setScheme(%22tcp%22);%0A                    %7D%0A                    final String dockerHost = uriBuilder.toS
+dockerHost = %22tcp:%22 + dockerHost.subs
 tring(
+5
 );%0A    
+   
+         %7D%0A
     
@@ -1580,159 +1580,8 @@
 t);%0A
-                %7D catch (URISyntaxException e) %7B%0A                    LOG.error(%22Can't build 'DOCKER_HOST' var: %7B%7D%22, e.getMessage());%0A                %7D%0A
     

remove httpclient dependency for just trivial string replacement