ProxySupport.java

  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.maven.plugins.contentpackage;

  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;

  24. import org.apache.maven.execution.MavenSession;
  25. import org.apache.maven.settings.Proxy;
  26. import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
  27. import org.apache.maven.settings.crypto.SettingsDecrypter;
  28. import org.apache.maven.settings.crypto.SettingsDecryptionResult;

  29. /**
  30.  * Read maven proxy settings.
  31.  */
  32. final class ProxySupport {

  33.   private ProxySupport() {
  34.     // static methods only
  35.   }

  36.   static List<io.wcm.tooling.commons.packmgr.Proxy> getMavenProxies(MavenSession mavenSession, SettingsDecrypter decrypter) {
  37.     if (mavenSession == null ||
  38.         mavenSession.getSettings() == null ||
  39.         mavenSession.getSettings().getProxies() == null ||
  40.         mavenSession.getSettings().getProxies().isEmpty()) {
  41.       return Collections.emptyList();
  42.     }
  43.     else {
  44.       final List<Proxy> mavenProxies = mavenSession.getSettings().getProxies();

  45.       final List<io.wcm.tooling.commons.packmgr.Proxy> proxies = new ArrayList<>(mavenProxies.size());

  46.       for (Proxy mavenProxy : mavenProxies) {
  47.         if (mavenProxy.isActive()) {
  48.           Proxy decryptedMavenProxy = decryptProxy(mavenProxy, decrypter);
  49.           proxies.add(new io.wcm.tooling.commons.packmgr.Proxy(decryptedMavenProxy.getId(),
  50.               decryptedMavenProxy.getProtocol(),
  51.               decryptedMavenProxy.getHost(),
  52.               decryptedMavenProxy.getPort(),
  53.               decryptedMavenProxy.getUsername(),
  54.               decryptedMavenProxy.getPassword(),
  55.               decryptedMavenProxy.getNonProxyHosts()));
  56.         }
  57.       }

  58.       return proxies;
  59.     }
  60.   }

  61.   private static Proxy decryptProxy(Proxy proxy, SettingsDecrypter decrypter) {
  62.     final DefaultSettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(proxy);
  63.     SettingsDecryptionResult decryptedResult = decrypter.decrypt(decryptionRequest);
  64.     return decryptedResult.getProxy();
  65.   }

  66. }