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  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
26  import org.apache.maven.execution.MavenExecutionRequest;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.LegacySupport;
29  import org.apache.maven.repository.MirrorSelector;
30  import org.apache.maven.settings.Mirror;
31  import org.apache.maven.settings.Proxy;
32  import org.apache.maven.settings.Server;
33  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
34  import org.apache.maven.settings.crypto.SettingsDecrypter;
35  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
36  import org.apache.maven.wagon.ResourceDoesNotExistException;
37  import org.apache.maven.wagon.TransferFailedException;
38  import org.apache.maven.wagon.authentication.AuthenticationInfo;
39  import org.apache.maven.wagon.proxy.ProxyInfo;
40  import org.codehaus.plexus.component.annotations.Component;
41  import org.codehaus.plexus.component.annotations.Requirement;
42  import org.codehaus.plexus.logging.Logger;
43  
44  /**
45   * Manages <a href="https://maven.apache.org/wagon">Wagon</a> related operations in Maven.
46   */
47  @Component(role = WagonManager.class)
48  public class DefaultWagonManager extends org.apache.maven.repository.legacy.DefaultWagonManager
49          implements WagonManager {
50  
51      // NOTE: This must use a different field name than in the super class or IoC has no chance to inject the loggers
52      @Requirement
53      private Logger log;
54  
55      @Requirement
56      private LegacySupport legacySupport;
57  
58      @Requirement
59      private SettingsDecrypter settingsDecrypter;
60  
61      @Requirement
62      private MirrorSelector mirrorSelector;
63  
64      @Requirement
65      private ArtifactRepositoryFactory artifactRepositoryFactory;
66  
67      public AuthenticationInfo getAuthenticationInfo(String id) {
68          MavenSession session = legacySupport.getSession();
69  
70          if (session != null && id != null) {
71              MavenExecutionRequest request = session.getRequest();
72  
73              if (request != null) {
74                  List<Server> servers = request.getServers();
75  
76                  if (servers != null) {
77                      for (Server server : servers) {
78                          if (id.equalsIgnoreCase(server.getId())) {
79                              SettingsDecryptionResult result =
80                                      settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(server));
81                              server = result.getServer();
82  
83                              AuthenticationInfo authInfo = new AuthenticationInfo();
84                              authInfo.setUserName(server.getUsername());
85                              authInfo.setPassword(server.getPassword());
86                              authInfo.setPrivateKey(server.getPrivateKey());
87                              authInfo.setPassphrase(server.getPassphrase());
88  
89                              return authInfo;
90                          }
91                      }
92                  }
93              }
94          }
95  
96          // empty one to prevent NPE
97          return new AuthenticationInfo();
98      }
99  
100     public ProxyInfo getProxy(String protocol) {
101         MavenSession session = legacySupport.getSession();
102 
103         if (session != null && protocol != null) {
104             MavenExecutionRequest request = session.getRequest();
105 
106             if (request != null) {
107                 List<Proxy> proxies = request.getProxies();
108 
109                 if (proxies != null) {
110                     for (Proxy proxy : proxies) {
111                         if (proxy.isActive() && protocol.equalsIgnoreCase(proxy.getProtocol())) {
112                             SettingsDecryptionResult result =
113                                     settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(proxy));
114                             proxy = result.getProxy();
115 
116                             ProxyInfo proxyInfo = new ProxyInfo();
117                             proxyInfo.setHost(proxy.getHost());
118                             proxyInfo.setType(proxy.getProtocol());
119                             proxyInfo.setPort(proxy.getPort());
120                             proxyInfo.setNonProxyHosts(proxy.getNonProxyHosts());
121                             proxyInfo.setUserName(proxy.getUsername());
122                             proxyInfo.setPassword(proxy.getPassword());
123 
124                             return proxyInfo;
125                         }
126                     }
127                 }
128             }
129         }
130 
131         return null;
132     }
133 
134     public void getArtifact(Artifact artifact, ArtifactRepository repository)
135             throws TransferFailedException, ResourceDoesNotExistException {
136         getArtifact(artifact, repository, null, false);
137     }
138 
139     public void getArtifact(Artifact artifact, List<ArtifactRepository> remoteRepositories)
140             throws TransferFailedException, ResourceDoesNotExistException {
141         getArtifact(artifact, remoteRepositories, null, false);
142     }
143 
144     @Deprecated
145     public ArtifactRepository getMirrorRepository(ArtifactRepository repository) {
146 
147         Mirror mirror = mirrorSelector.getMirror(
148                 repository, legacySupport.getSession().getSettings().getMirrors());
149 
150         if (mirror != null) {
151             String id = mirror.getId();
152             if (id == null) {
153                 // TODO this should be illegal in settings.xml
154                 id = repository.getId();
155             }
156 
157             log.debug("Using mirror: " + mirror.getUrl() + " (id: " + id + ")");
158 
159             repository = artifactRepositoryFactory.createArtifactRepository(
160                     id, mirror.getUrl(), repository.getLayout(), repository.getSnapshots(), repository.getReleases());
161         }
162         return repository;
163     }
164 }