Blob Blame History Raw
From 789069406cc9f0ee5e048db1c8e4b0019fdff9b6 Mon Sep 17 00:00:00 2001
From: Mat Booth <mat.booth@redhat.com>
Date: Mon, 13 Jul 2020 13:52:18 +0100
Subject: [PATCH 6/6] Remove dep on aether-connector

Change-Id: I08f91c9ddc84f6f34205452467a71b18badad4a0
Signed-off-by: Mat Booth <mat.booth@redhat.com>
---
 .../org.eclipse.m2e.maven.runtime/pom.xml     |  15 +-
 org.eclipse.m2e.core/META-INF/MANIFEST.MF     |   2 +-
 .../nexus/AetherClientResourceFetcher.java    | 201 ------------------
 .../index/nexus/HttpResourceFetcher.java      | 153 +++++++++++++
 .../index/nexus/NexusIndexManager.java        |   2 +-
 5 files changed, 156 insertions(+), 217 deletions(-)
 delete mode 100644 org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java
 create mode 100644 org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/HttpResourceFetcher.java

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..0440325 100644
--- a/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml
+++ b/m2e-maven-runtime/org.eclipse.m2e.maven.runtime/pom.xml
@@ -33,7 +33,6 @@
     <maven-core.version>3.6.3</maven-core.version>
     <!-- below are m2e-specific addons -->
     <plexus-build-api.version>0.0.7</plexus-build-api.version>
-    <okhttp-connector.version>0.17.8</okhttp-connector.version>
   </properties>
   <dependencies>
     <dependency>
@@ -76,17 +75,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>
@@ -143,8 +131,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 af681a0..51766cf 100644
--- a/org.eclipse.m2e.core/META-INF/MANIFEST.MF
+++ b/org.eclipse.m2e.core/META-INF/MANIFEST.MF
@@ -18,7 +18,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
deleted file mode 100644
index b311824..0000000
--- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/AetherClientResourceFetcher.java
+++ /dev/null
@@ -1,201 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010 Sonatype, Inc.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License 2.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- *      Sonatype, Inc. - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.m2e.core.internal.index.nexus;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-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 org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.OperationCanceledException;
-
-import org.apache.maven.index.updater.ResourceFetcher;
-import org.apache.maven.wagon.authentication.AuthenticationInfo;
-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 final AuthenticationInfo authInfo;
-
-  private final ProxyInfo proxyInfo;
-
-  private final String userAgent;
-
-  private final IProgressMonitor monitor;
-
-  private String baseUrl;
-
-  public AetherClientResourceFetcher(final AuthenticationInfo authInfo, final ProxyInfo proxyInfo,
-      final IProgressMonitor monitor) {
-    this.authInfo = authInfo;
-    this.proxyInfo = 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 disconnect() throws IOException {
-    aetherClient.close();
-  }
-
-  @Deprecated
-  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))) {
-      final byte[] buffer = new byte[1024 * 1024];
-      int n = 0;
-      while(-1 != (n = is.read(buffer))) {
-        os.write(buffer, 0, n);
-        if(monitor.isCanceled()) {
-          throw new OperationCanceledException();
-        }
-      }
-    }
-  }
-
-  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;
-
-    Map<String, String> headers;
-
-    public AetherClientConfigAdapter(String baseUrl, AuthenticationInfo authInfo, ProxyInfo proxyInfo, String userAgent,
-        Map<String, String> headers) {
-      this.baseUrl = baseUrl;
-      this.authInfo = authInfo;
-      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;
-      }
-      //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());
-      }
-      return null;
-    }
-
-    public Map<String, String> getHeaders() {
-      return headers;
-    }
-  }
-}
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/HttpResourceFetcher.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/HttpResourceFetcher.java
new file mode 100644
index 0000000..d1f467d
--- /dev/null
+++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/HttpResourceFetcher.java
@@ -0,0 +1,154 @@
+/*******************************************************************************
+ * Copyright (c) 2020 Red Hat, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+
+package org.eclipse.m2e.core.internal.index.nexus;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+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;
+import org.eclipse.core.runtime.OperationCanceledException;
+
+import org.apache.maven.index.updater.ResourceFetcher;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.proxy.ProxyInfo;
+import org.apache.maven.wagon.proxy.ProxyUtils;
+
+import org.eclipse.m2e.core.internal.MavenPluginActivator;
+
+
+/**
+ * Resource fetcher implementation based on the JDK's built-in HTTP client.
+ */
+public class HttpResourceFetcher implements ResourceFetcher {
+
+  private HttpClient client;
+
+  private Authenticator auth;
+
+  private ProxySelector proxy;
+
+  private IProgressMonitor monitor;
+
+  private String baseUrl;
+
+  public HttpResourceFetcher(final AuthenticationInfo authInfo, final ProxyInfo proxyInfo,
+      final IProgressMonitor monitor) {
+    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();
+  }
+
+  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 {
+  }
+
+    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 {
+    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 = response.body().read(buffer))) {
+        os.write(buffer, 0, n);
+        if(monitor.isCanceled()) {
+          throw new OperationCanceledException();
+        }
+      }
+    } catch(InterruptedException ex) {
+      throw new OperationCanceledException();
+    }
+    monitor.worked(1);
+  }
+
+  static class WagonProxySelector extends ProxySelector {
+
+    private ProxyInfo proxyInfo;
+
+    public WagonProxySelector(ProxyInfo proxyInfo) {
+      this.proxyInfo = proxyInfo;
+    }
+
+    public List<Proxy> select(URI uri) {
+      if(proxyInfo == null || ProxyUtils.validateNonProxyHosts(proxyInfo, uri.getHost())) {
+        return List.of(Proxy.NO_PROXY);
+      }
+      Proxy.Type type = Proxy.Type.HTTP;
+      if(!proxyInfo.getType().equals(ProxyInfo.PROXY_HTTP)) {
+        type = Proxy.Type.SOCKS;
+      }
+      Proxy p = new Proxy(type, new InetSocketAddress(proxyInfo.getHost(), proxyInfo.getPort()));
+      return List.of(p);
+    }
+
+    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+    }
+  }
+}
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java
index 61e3762..5426327 100644
--- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java
+++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/nexus/NexusIndexManager.java
@@ -1181,7 +1181,7 @@ public class NexusIndexManager implements IndexManager, IMavenProjectChangedList
     ProxyInfo proxyInfo = maven.getProxyInfo(repository.getProtocol());
     AuthenticationInfo authenticationInfo = repository.getAuthenticationInfo();
 
-    IndexUpdateRequest request = new IndexUpdateRequest(context, new AetherClientResourceFetcher(authenticationInfo,
+    IndexUpdateRequest request = new IndexUpdateRequest(context, new HttpResourceFetcher(authenticationInfo,
         proxyInfo, monitor));
     File localRepo = repositoryRegistry.getLocalRepository().getBasedir();
     File indexCacheBasedir = new File(localRepo, ".cache/m2e/" + MavenPluginActivator.getVersion()).getCanonicalFile(); //$NON-NLS-1$
-- 
2.26.2