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  @Component( role = WagonManager.class )
46  public class DefaultWagonManager
47      extends org.apache.maven.repository.legacy.DefaultWagonManager
48      implements WagonManager
49  {
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      {
69          MavenSession session = legacySupport.getSession();
70  
71          if ( session != null && id != null )
72          {
73              MavenExecutionRequest request = session.getRequest();
74  
75              if ( request != null )
76              {
77                  List<Server> servers = request.getServers();
78  
79                  if ( servers != null )
80                  {
81                      for ( Server server : servers )
82                      {
83                          if ( id.equalsIgnoreCase( server.getId() ) )
84                          {
85                              SettingsDecryptionResult result =
86                                  settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) );
87                              server = result.getServer();
88  
89                              AuthenticationInfo authInfo = new AuthenticationInfo();
90                              authInfo.setUserName( server.getUsername() );
91                              authInfo.setPassword( server.getPassword() );
92                              authInfo.setPrivateKey( server.getPrivateKey() );
93                              authInfo.setPassphrase( server.getPassphrase() );
94  
95                              return authInfo;
96                          }
97                      }
98                  }
99              }
100         }
101 
102         // empty one to prevent NPE
103        return new AuthenticationInfo();
104     }
105 
106     public ProxyInfo getProxy( String protocol )
107     {
108         MavenSession session = legacySupport.getSession();
109 
110         if ( session != null && protocol != null )
111         {
112             MavenExecutionRequest request = session.getRequest();
113 
114             if ( request != null )
115             {
116                 List<Proxy> proxies = request.getProxies();
117 
118                 if ( proxies != null )
119                 {
120                     for ( Proxy proxy : proxies )
121                     {
122                         if ( proxy.isActive() && protocol.equalsIgnoreCase( proxy.getProtocol() ) )
123                         {
124                             SettingsDecryptionResult result =
125                                 settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( proxy ) );
126                             proxy = result.getProxy();
127 
128                             ProxyInfo proxyInfo = new ProxyInfo();
129                             proxyInfo.setHost( proxy.getHost() );
130                             proxyInfo.setType( proxy.getProtocol() );
131                             proxyInfo.setPort( proxy.getPort() );
132                             proxyInfo.setNonProxyHosts( proxy.getNonProxyHosts() );
133                             proxyInfo.setUserName( proxy.getUsername() );
134                             proxyInfo.setPassword( proxy.getPassword() );
135 
136                             return proxyInfo;
137                         }
138                     }
139                 }
140             }
141         }
142 
143         return null;
144     }
145 
146     public void getArtifact( Artifact artifact, ArtifactRepository repository )
147         throws TransferFailedException, ResourceDoesNotExistException
148     {
149         getArtifact( artifact, repository, null, false );
150     }
151 
152     public void getArtifact( Artifact artifact, List<ArtifactRepository> remoteRepositories )
153         throws TransferFailedException, ResourceDoesNotExistException
154     {
155         getArtifact( artifact, remoteRepositories, null, false );
156     }
157 
158     @Deprecated
159     public ArtifactRepository getMirrorRepository( ArtifactRepository repository )
160     {
161 
162         Mirror mirror = mirrorSelector.getMirror( repository, legacySupport.getSession().getSettings().getMirrors() );
163 
164         if ( mirror != null )
165         {
166             String id = mirror.getId();
167             if ( id == null )
168             {
169                 // TODO: this should be illegal in settings.xml
170                 id = repository.getId();
171             }
172 
173             log.debug( "Using mirror: " + mirror.getUrl() + " (id: " + id + ")" );
174 
175             repository = artifactRepositoryFactory.createArtifactRepository( id, mirror.getUrl(),
176                                                                      repository.getLayout(), repository.getSnapshots(),
177                                                                      repository.getReleases() );
178         }
179         return repository;
180     }
181 
182 }