View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.plugins.aem.maven;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.settings.Proxy;
28  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
29  import org.apache.maven.settings.crypto.SettingsDecrypter;
30  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
31  
32  /**
33   * Read maven proxy settings.
34   */
35  final class ProxySupport {
36  
37    private ProxySupport() {
38      // static methods only
39    }
40  
41    static List<io.wcm.tooling.commons.packmgr.Proxy> getMavenProxies(MavenSession mavenSession, SettingsDecrypter decrypter) {
42      if (mavenSession == null ||
43          mavenSession.getSettings() == null ||
44          mavenSession.getSettings().getProxies() == null ||
45          mavenSession.getSettings().getProxies().isEmpty()) {
46        return Collections.emptyList();
47      }
48      else {
49        final List<Proxy> mavenProxies = mavenSession.getSettings().getProxies();
50  
51        final List<io.wcm.tooling.commons.packmgr.Proxy> proxies = new ArrayList<>(mavenProxies.size());
52  
53        for (Proxy mavenProxy : mavenProxies) {
54          if (mavenProxy.isActive()) {
55            Proxy decryptedMavenProxy = decryptProxy(mavenProxy, decrypter);
56            proxies.add(new io.wcm.tooling.commons.packmgr.Proxy(decryptedMavenProxy.getId(),
57                decryptedMavenProxy.getProtocol(),
58                decryptedMavenProxy.getHost(),
59                decryptedMavenProxy.getPort(),
60                decryptedMavenProxy.getUsername(),
61                decryptedMavenProxy.getPassword(),
62                decryptedMavenProxy.getNonProxyHosts()));
63          }
64        }
65  
66        return proxies;
67      }
68    }
69  
70    private static Proxy decryptProxy(Proxy proxy, SettingsDecrypter decrypter) {
71      final DefaultSettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(proxy);
72      SettingsDecryptionResult decryptedResult = decrypter.decrypt(decryptionRequest);
73      return decryptedResult.getProxy();
74    }
75  
76  }