View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.manager;
20  
21  import java.util.List;
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
25  import org.apache.maven.execution.MavenExecutionRequest;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.LegacySupport;
28  import org.apache.maven.repository.MirrorSelector;
29  import org.apache.maven.settings.Mirror;
30  import org.apache.maven.settings.Proxy;
31  import org.apache.maven.settings.Server;
32  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
33  import org.apache.maven.settings.crypto.SettingsDecrypter;
34  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
35  import org.apache.maven.wagon.ResourceDoesNotExistException;
36  import org.apache.maven.wagon.TransferFailedException;
37  import org.apache.maven.wagon.authentication.AuthenticationInfo;
38  import org.apache.maven.wagon.proxy.ProxyInfo;
39  import org.codehaus.plexus.component.annotations.Component;
40  import org.codehaus.plexus.component.annotations.Requirement;
41  import org.codehaus.plexus.logging.Logger;
42  
43  /**
44   * Manages <a href="https://maven.apache.org/wagon">Wagon</a> related operations in Maven.
45   */
46  @Component(role = WagonManager.class)
47  public class DefaultWagonManager extends org.apache.maven.repository.legacy.DefaultWagonManager
48          implements WagonManager {
49  
50      // NOTE: This must use a different field name than in the super class or IoC has no chance to inject the loggers
51      @Requirement
52      private Logger log;
53  
54      @Requirement
55      private LegacySupport legacySupport;
56  
57      @Requirement
58      private SettingsDecrypter settingsDecrypter;
59  
60      @Requirement
61      private MirrorSelector mirrorSelector;
62  
63      @Requirement
64      private ArtifactRepositoryFactory artifactRepositoryFactory;
65  
66      public AuthenticationInfo getAuthenticationInfo(String id) {
67          MavenSession session = legacySupport.getSession();
68  
69          if (session != null && id != null) {
70              MavenExecutionRequest request = session.getRequest();
71  
72              if (request != null) {
73                  List<Server> servers = request.getServers();
74  
75                  if (servers != null) {
76                      for (Server server : servers) {
77                          if (id.equalsIgnoreCase(server.getId())) {
78                              SettingsDecryptionResult result =
79                                      settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(server));
80                              server = result.getServer();
81  
82                              AuthenticationInfo authInfo = new AuthenticationInfo();
83                              authInfo.setUserName(server.getUsername());
84                              authInfo.setPassword(server.getPassword());
85                              authInfo.setPrivateKey(server.getPrivateKey());
86                              authInfo.setPassphrase(server.getPassphrase());
87  
88                              return authInfo;
89                          }
90                      }
91                  }
92              }
93          }
94  
95          // empty one to prevent NPE
96          return new AuthenticationInfo();
97      }
98  
99      public ProxyInfo getProxy(String protocol) {
100         MavenSession session = legacySupport.getSession();
101 
102         if (session != null && protocol != null) {
103             MavenExecutionRequest request = session.getRequest();
104 
105             if (request != null) {
106                 List<Proxy> proxies = request.getProxies();
107 
108                 if (proxies != null) {
109                     for (Proxy proxy : proxies) {
110                         if (proxy.isActive() && protocol.equalsIgnoreCase(proxy.getProtocol())) {
111                             SettingsDecryptionResult result =
112                                     settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(proxy));
113                             proxy = result.getProxy();
114 
115                             ProxyInfo proxyInfo = new ProxyInfo();
116                             proxyInfo.setHost(proxy.getHost());
117                             proxyInfo.setType(proxy.getProtocol());
118                             proxyInfo.setPort(proxy.getPort());
119                             proxyInfo.setNonProxyHosts(proxy.getNonProxyHosts());
120                             proxyInfo.setUserName(proxy.getUsername());
121                             proxyInfo.setPassword(proxy.getPassword());
122 
123                             return proxyInfo;
124                         }
125                     }
126                 }
127             }
128         }
129 
130         return null;
131     }
132 
133     public void getArtifact(Artifact artifact, ArtifactRepository repository)
134             throws TransferFailedException, ResourceDoesNotExistException {
135         getArtifact(artifact, repository, null, false);
136     }
137 
138     public void getArtifact(Artifact artifact, List<ArtifactRepository> remoteRepositories)
139             throws TransferFailedException, ResourceDoesNotExistException {
140         getArtifact(artifact, remoteRepositories, null, false);
141     }
142 
143     @Deprecated
144     public ArtifactRepository getMirrorRepository(ArtifactRepository repository) {
145 
146         Mirror mirror = mirrorSelector.getMirror(
147                 repository, legacySupport.getSession().getSettings().getMirrors());
148 
149         if (mirror != null) {
150             String id = mirror.getId();
151             if (id == null) {
152                 // TODO this should be illegal in settings.xml
153                 id = repository.getId();
154             }
155 
156             log.debug("Using mirror: " + mirror.getUrl() + " (id: " + id + ")");
157 
158             repository = artifactRepositoryFactory.createArtifactRepository(
159                     id, mirror.getUrl(), repository.getLayout(), repository.getSnapshots(), repository.getReleases());
160         }
161         return repository;
162     }
163 }