Blob Blame History Raw
From 6483400667ee34972a392d7efef4eb817bec9862 Mon Sep 17 00:00:00 2001
From: Mat Booth <mat.booth@redhat.com>
Date: Tue, 17 Nov 2020 10:32:54 +0000
Subject: [PATCH 6/6] Remove dep on aether-connector

---
 .../.factorypath                              |   3 -
 .../org.eclipse.m2e.maven.runtime/pom.xml     |  18 +-
 org.eclipse.m2e.core/META-INF/MANIFEST.MF     |   2 +-
 .../nexus/AetherClientResourceFetcher.java    | 198 +++++++-----------
 4 files changed, 79 insertions(+), 142 deletions(-)

diff --git a/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/.factorypath b/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/.factorypath
index 72b8866..fe9f4ca 100644
--- a/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/.factorypath
+++ b/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/.factorypath
@@ -38,9 +38,6 @@
     <factorypathentry kind="VARJAR" id="M2_REPO/org/eclipse/sisu/org.eclipse.sisu.plexus/0.3.4/org.eclipse.sisu.plexus-0.3.4.jar" enabled="true" runInBatchMode="false"/>
     <factorypathentry kind="VARJAR" id="M2_REPO/javax/enterprise/cdi-api/1.0/cdi-api-1.0.jar" enabled="true" runInBatchMode="false"/>
     <factorypathentry kind="VARJAR" id="M2_REPO/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar" enabled="true" runInBatchMode="false"/>
-    <factorypathentry kind="VARJAR" id="M2_REPO/io/takari/aether/aether-connector-okhttp/0.17.8/aether-connector-okhttp-0.17.8.jar" enabled="true" runInBatchMode="false"/>
-    <factorypathentry kind="VARJAR" id="M2_REPO/com/squareup/okhttp3/okhttp/3.14.1/okhttp-3.14.1.jar" enabled="true" runInBatchMode="false"/>
-    <factorypathentry kind="VARJAR" id="M2_REPO/com/squareup/okio/okio/1.17.3/okio-1.17.3.jar" enabled="true" runInBatchMode="false"/>
     <factorypathentry kind="VARJAR" id="M2_REPO/org/apache/maven/resolver/maven-resolver-impl/1.4.1/maven-resolver-impl-1.4.1.jar" enabled="true" runInBatchMode="false"/>
     <factorypathentry kind="VARJAR" id="M2_REPO/org/apache/maven/resolver/maven-resolver-connector-basic/1.4.1/maven-resolver-connector-basic-1.4.1.jar" enabled="true" runInBatchMode="false"/>
     <factorypathentry kind="VARJAR" id="M2_REPO/org/apache/maven/resolver/maven-resolver-transport-wagon/1.4.1/maven-resolver-transport-wagon-1.4.1.jar" enabled="true" runInBatchMode="false"/>
diff --git a/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml b/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml
index 0ff5875..10b69ff 100644
--- a/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml
+++ b/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml
@@ -76,17 +76,6 @@
       <version>${plexus-build-api.version}</version>
       <optional>true</optional>
     </dependency>
-    <dependency>
-      <groupId>io.takari.aether</groupId>
-      <artifactId>aether-connector-okhttp</artifactId>
-      <version>${okhttp-connector.version}</version>
-      <exclusions>
-        <exclusion>
-          <groupId>org.eclipse.aether</groupId>
-          <artifactId>aether-util</artifactId>
-        </exclusion>
-      </exclusions>
-    </dependency>
     <dependency>
       <groupId>org.apache.maven.resolver</groupId>
       <artifactId>maven-resolver-impl</artifactId>
@@ -106,6 +95,10 @@
       <groupId>org.apache.maven.wagon</groupId>
       <artifactId>wagon-file</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.wagon</groupId>
+      <artifactId>wagon-http</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.fusesource.jansi</groupId>
       <artifactId>jansi</artifactId>
@@ -143,8 +136,7 @@
               org.codehaus.plexus.*;provider=m2e,
               org.sonatype.plexus.*;provider=m2e,
               org.eclipse.aether.*;provider=m2e,
-              com.google.inject.*;provider=m2e,
-              io.takari.*;provider=m2e
+              com.google.inject.*;provider=m2e
             </_exportcontents>
 
             <Import-Package>
diff --git a/org.eclipse.m2e.core/META-INF/MANIFEST.MF b/org.eclipse.m2e.core/META-INF/MANIFEST.MF
index 7f97012..6d8a47f 100644
--- a/org.eclipse.m2e.core/META-INF/MANIFEST.MF
+++ b/org.eclipse.m2e.core/META-INF/MANIFEST.MF
@@ -19,7 +19,7 @@ Require-Bundle: org.eclipse.osgi;bundle-version="3.10.0",
  com.google.guava;bundle-version="[20.0,29.0)"
 Bundle-ActivationPolicy: lazy
 Bundle-ClassPath: .
-Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Bundle-RequiredExecutionEnvironment: JavaSE-11
 Export-Package: org.eclipse.m2e.core,
  org.eclipse.m2e.core.archetype;x-friends:="org.eclipse.m2e.core.ui",
  org.eclipse.m2e.core.embedder,
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java
index b311824..b508478 100644
--- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java
+++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java
@@ -15,21 +15,27 @@ package org.eclipse.m2e.core.internal.index.nexus;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.security.NoSuchAlgorithmException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.net.ssl.SSLContext;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.net.Authenticator;
+import java.net.InetSocketAddress;
+import java.net.PasswordAuthentication;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpClient.Redirect;
+import java.net.http.HttpClient.Version;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.time.Duration;
+import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
@@ -41,161 +47,103 @@ import org.apache.maven.wagon.proxy.ProxyInfo;
 import org.apache.maven.wagon.proxy.ProxyUtils;
 
 import org.eclipse.m2e.core.internal.MavenPluginActivator;
-import org.eclipse.m2e.core.internal.Messages;
-
-import io.takari.aether.client.AetherClient;
-import io.takari.aether.client.AetherClientAuthentication;
-import io.takari.aether.client.AetherClientConfig;
-import io.takari.aether.client.AetherClientProxy;
-import io.takari.aether.client.Response;
-import io.takari.aether.okhttp.OkHttpAetherClient;
 
 
 public class AetherClientResourceFetcher implements ResourceFetcher {
 
-  private AetherClient aetherClient;
+  private HttpClient client;
 
-  private final AuthenticationInfo authInfo;
+  private Authenticator auth;
 
-  private final ProxyInfo proxyInfo;
+  private ProxySelector proxy;
 
-  private final String userAgent;
-
-  private final IProgressMonitor monitor;
+  private IProgressMonitor monitor;
 
   private String baseUrl;
 
   public AetherClientResourceFetcher(final AuthenticationInfo authInfo, final ProxyInfo proxyInfo,
       final IProgressMonitor monitor) {
-    this.authInfo = authInfo;
-    this.proxyInfo = proxyInfo;
+    if(authInfo != null) {
+      auth = new Authenticator() {
+        @Override
+        protected PasswordAuthentication getPasswordAuthentication() {
+          return new PasswordAuthentication(authInfo.getUserName(), authInfo.getPassword().toCharArray());
+        }
+      };
+    }
+    proxy = new WagonProxySelector(proxyInfo);
     this.monitor = (monitor != null) ? monitor : new NullProgressMonitor();
-    this.userAgent = MavenPluginActivator.getUserAgent();
   }
 
-  public void connect(String id, String url) {
-    this.baseUrl = url;
-    aetherClient = new OkHttpAetherClient(
-        new AetherClientConfigAdapter(baseUrl, authInfo, proxyInfo, userAgent,
-        new HashMap<String, String>()));
+  public void connect(String id, String url) throws IOException {
+    baseUrl = url;
+    HttpClient.Builder builder = HttpClient.newBuilder().version(Version.HTTP_1_1).followRedirects(Redirect.NORMAL)
+        .connectTimeout(Duration.ofSeconds(10)).proxy(proxy);
+    if(auth != null) {
+      builder.authenticator(auth);
+    }
+    client = builder.build();
   }
 
   public void disconnect() throws IOException {
-    aetherClient.close();
   }
 
-  @Deprecated
+  public InputStream retrieve(String name) throws IOException, FileNotFoundException {
+    final File target = File.createTempFile(name, "");
+    retrieve(name, target);
+    return new FileInputStream(target) {
+      @Override
+      public void close() throws IOException {
+        super.close();
+        target.delete();
+      }
+    };
+  }
+
   public void retrieve(String name, File targetFile) throws IOException, FileNotFoundException {
-    String url = baseUrl + "/" + name;
-    try (Response response = aetherClient.get(url);
-        InputStream is = response.getInputStream();
-        OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) {
+    System.out.println();
+    System.out.println("Requesting: " + baseUrl + "/" + name);
+    HttpRequest request = HttpRequest.newBuilder().uri(URI.create(baseUrl + "/" + name)).timeout(Duration.ofMinutes(2))
+        .header("User-Agent", MavenPluginActivator.getUserAgent()).GET().build();
+    System.out.println(request.headers());
+    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) {
+      HttpResponse<InputStream> response = client.send(request, BodyHandlers.ofInputStream());
+      System.out.println(response.headers());
       final byte[] buffer = new byte[1024 * 1024];
       int n = 0;
-      while(-1 != (n = is.read(buffer))) {
+      while(-1 != (n = response.body().read(buffer))) {
         os.write(buffer, 0, n);
         if(monitor.isCanceled()) {
           throw new OperationCanceledException();
         }
       }
+    } catch(InterruptedException ex) {
+      throw new OperationCanceledException();
     }
+    monitor.worked(1);
   }
 
-  public InputStream retrieve(String name) throws IOException, FileNotFoundException {
-    String url = baseUrl + "/" + name;
-    Response response = aetherClient.get(url);
-
-    return response.getInputStream();
-  }
-
-  class AetherClientConfigAdapter extends AetherClientConfig {
-    private final Logger log = LoggerFactory.getLogger(AetherClientConfigAdapter.class);
-
-    int connectionTimeout;
-
-    int requestTimeout;
-
-    AuthenticationInfo authInfo;
-
-    ProxyInfo proxyInfo;
-
-    String userAgent;
-
-    String baseUrl;
+  static class WagonProxySelector extends ProxySelector {
 
-    Map<String, String> headers;
+    private ProxyInfo proxyInfo;
 
-    public AetherClientConfigAdapter(String baseUrl, AuthenticationInfo authInfo, ProxyInfo proxyInfo, String userAgent,
-        Map<String, String> headers) {
-      this.baseUrl = baseUrl;
-      this.authInfo = authInfo;
+    public WagonProxySelector(ProxyInfo proxyInfo) {
       this.proxyInfo = proxyInfo;
-      this.userAgent = userAgent;
-      this.headers = headers;
-
-      try {
-        // ensure JVM's trust & key stores are used
-        setSslSocketFactory(SSLContext.getDefault().getSocketFactory());
-      } catch(NoSuchAlgorithmException ex) {
-        log.warn(Messages.AetherClientConfigAdapter_error_sslContext);
-      }
-    }
-
-    public String getUserAgent() {
-      return userAgent;
     }
 
-    public int getConnectionTimeout() {
-      return connectionTimeout;
-    }
-
-    public int getRequestTimeout() {
-      return requestTimeout;
-    }
-
-    public AetherClientProxy getProxy() {
-
-      if(proxyInfo == null) {
-        return null;
+    public List<Proxy> select(URI uri) {
+      if(proxyInfo == null || ProxyUtils.validateNonProxyHosts(proxyInfo, uri.getHost())) {
+        return List.of(Proxy.NO_PROXY);
       }
-      //Bug 512006 don't return the proxy for nonProxyHosts 
-      try {
-        if(ProxyUtils.validateNonProxyHosts(proxyInfo, new URL(baseUrl).getHost())) {
-          return null;
-        }
-      } catch(MalformedURLException ignore) {
-      }
-
-      return new AetherClientProxy() {
-
-        public String getHost() {
-          return proxyInfo.getHost();
-        }
-
-        public int getPort() {
-          return proxyInfo.getPort();
-        }
-
-        public AetherClientAuthentication getAuthentication() {
-
-          if(proxyInfo != null && proxyInfo.getUserName() != null && proxyInfo.getPassword() != null) {
-            return new AetherClientAuthentication(proxyInfo.getUserName(), proxyInfo.getPassword());
-          }
-          return null;
-        }
-      };
-    }
-
-    public AetherClientAuthentication getAuthentication() {
-
-      if(authInfo != null) {
-        return new AetherClientAuthentication(authInfo.getUserName(), authInfo.getPassword());
+      Proxy.Type type = Proxy.Type.HTTP;
+      if(!proxyInfo.getType().equals(ProxyInfo.PROXY_HTTP)) {
+        type = Proxy.Type.SOCKS;
       }
-      return null;
+      Proxy p = new Proxy(type, new InetSocketAddress(proxyInfo.getHost(), proxyInfo.getPort()));
+      return List.of(p);
     }
 
-    public Map<String, String> getHeaders() {
-      return headers;
+    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
     }
   }
 }
-- 
2.28.0