package nl.jpoint.vertx.mod.deploy.command;

import io.vertx.rxjava.core.Vertx;
import io.vertx.rxjava.core.buffer.Buffer;
import nl.jpoint.vertx.mod.deploy.DeployConfig;
import nl.jpoint.vertx.mod.deploy.request.ModuleRequest;
import nl.jpoint.vertx.mod.deploy.util.LogConstants;
import nl.jpoint.vertx.mod.deploy.util.MetadataXPathUtil;
import nl.jpoint.vertx.mod.deploy.util.RxHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

import static rx.Observable.just;


public class ResolveSnapshotVersion<T extends ModuleRequest> implements Command<T> {
    private static final Logger LOG = LoggerFactory.getLogger(ResolveSnapshotVersion.class);
    private final DeployConfig config;
    private final Vertx rxVertx;

    public ResolveSnapshotVersion(DeployConfig config, io.vertx.core.Vertx vertx) {
        this.rxVertx = new Vertx(vertx);
        this.config = config;
    }

    @Override
    public Observable<T> executeAsync(T request) {
        final URI location = config.getNexusUrl().resolve(config.getNexusUrl().getPath() + "/" + request.getMetadataLocation());
        Path filename = createTempFile(request.getArtifactId());
        return new RxHttpUtil(rxVertx, config).get(request.getId(), location, filename)
                .flatMap(x -> {
                    request.setVersion(retrieveAndParseMetadata(filename, request));
                    return just(request);
                }).onErrorReturn(x -> {
                    LOG.error("[{} - {}]: Error downloading artifact {} for url {} -> .", LogConstants.DEPLOY_ARTIFACT_REQUEST, request.getId(), request.getModuleId(), location, x);
                    return request;
                });
    }

    private Path createTempFile(String filename) {
        return Paths.get(System.getProperty("java.io.tmpdir") + "/" + filename);
    }

    private String retrieveAndParseMetadata(Path fileLocation, ModuleRequest request) {
        Buffer b = rxVertx.fileSystem().readFileBlocking(fileLocation.toString());
        return MetadataXPathUtil.getRealSnapshotVersionFromMetadata(b.toString().getBytes(), request);
    }
}

@@ -343,50 +343,479 @@
 ort 
-nl.jpoint.vertx.mod.deploy.util.RxHttpUtil
+org.apache.http.auth.AuthScope;%0Aimport org.apache.http.auth.UsernamePasswordCredentials;%0Aimport org.apache.http.client.CredentialsProvider;%0Aimport org.apache.http.client.methods.CloseableHttpResponse;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.client.methods.HttpUriRequest;%0Aimport org.apache.http.impl.client.BasicCredentialsProvider;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClientBuilder
 ;%0Aim
@@ -892,16 +892,77 @@
 vable;%0A%0A
+import java.io.FileOutputStream;%0Aimport java.io.IOException;%0A
 import j
@@ -1483,22 +1483,8 @@
  %7D%0A%0A
-    @Override%0A
     
@@ -1522,32 +1522,320 @@
 nc(T request) %7B%0A
+        CredentialsProvider provider = new BasicCredentialsProvider();%0A        UsernamePasswordCredentials credentials%0A                = new UsernamePasswordCredentials(config.getHttpAuthUser(), config.getHttpAuthPassword());%0A        provider.setCredentials(AuthScope.ANY, credentials);%0A%0A
         final UR
@@ -1943,24 +1943,77 @@
 ocation());%0A
+        HttpUriRequest get = new HttpGet(location);%0A%0A
         Path
@@ -2061,32 +2061,33 @@
 ifactId());%0A
+%0A
         
 return new R
@@ -2078,295 +2078,523 @@
     
-return new RxHttpUtil(rxVertx, config).get(request.getId(), location, filename)%0A                .flatMap(x -%3E %7B%0A                    request.setVersion(retrieveAndParseMetadata(filename, request));%0A                    return just(request);%0A                %7D).onErrorReturn(x -%3E %7B%0A        
+try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();%0A             CloseableHttpResponse response = client.execute(get)) %7B%0A            LOG.info(%22%5B%7B%7D - %7B%7D%5D: Downloaded Metadata for %7B%7D.%22, LogConstants.DEPLOY_ARTIFACT_REQUEST, request.getId(), request.getModuleId());%0A            response.getEntity().writeTo(new FileOutputStream(filename.toFile()));%0A            request.setVersion(retrieveAndParseMetadata(filename, request));%0A        %7D catch (IOException e) %7B%0A
     
@@ -2645,35 +2645,27 @@
 ing 
-artifact %7B%7D for url %7B%7D -%3E .
+Metadata  -%3E %7B%7D, %7B%7D
 %22, L
@@ -2722,79 +2722,74 @@
 (), 
-request.getModuleId(), location, x);%0A                    return request
+e.getMessage(), e);%0A            throw new IllegalStateException(e)
 ;%0A  
@@ -2794,24 +2794,26 @@
         
+%7D%0A
         
 %7D);%0A    
@@ -2804,17 +2804,35 @@
         
-%7D
+return just(request
 );%0A    %7D

Fix downloading of Metadataa