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