Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64e1fb7c67fa7de7324f3257fcd9c3f3f4466b1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * 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.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.AbstractResourceFetcher;
import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.apache.maven.wagon.proxy.ProxyInfo;

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 extends AbstractResourceFetcher {

  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) throws IOException {
    aetherClient = new OkHttpAetherClient(new AetherClientConfigAdapter(authInfo, proxyInfo, userAgent,
        new HashMap<String, String>()));
    this.baseUrl = url;
  }

  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();
        }
      }
    }
  }

  class AetherClientConfigAdapter extends AetherClientConfig {
    private final Logger log = LoggerFactory.getLogger(AetherClientConfigAdapter.class);

    int connectionTimeout;

    int requestTimeout;

    AuthenticationInfo authInfo;

    ProxyInfo proxyInfo;

    String userAgent;

    Map<String, String> headers;

    public AetherClientConfigAdapter(AuthenticationInfo authInfo, ProxyInfo proxyInfo, String userAgent,
        Map<String, String> headers) {
      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;
      }

      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;
    }
  }
}

Back to the top