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