001package org.apache.maven.artifact.manager;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023
024import org.apache.maven.artifact.Artifact;
025import org.apache.maven.artifact.repository.ArtifactRepository;
026import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
027import org.apache.maven.execution.MavenExecutionRequest;
028import org.apache.maven.execution.MavenSession;
029import org.apache.maven.plugin.LegacySupport;
030import org.apache.maven.repository.MirrorSelector;
031import org.apache.maven.settings.Mirror;
032import org.apache.maven.settings.Proxy;
033import org.apache.maven.settings.Server;
034import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
035import org.apache.maven.settings.crypto.SettingsDecrypter;
036import org.apache.maven.settings.crypto.SettingsDecryptionResult;
037import org.apache.maven.wagon.ResourceDoesNotExistException;
038import org.apache.maven.wagon.TransferFailedException;
039import org.apache.maven.wagon.authentication.AuthenticationInfo;
040import org.apache.maven.wagon.proxy.ProxyInfo;
041import org.codehaus.plexus.component.annotations.Component;
042import org.codehaus.plexus.component.annotations.Requirement;
043import org.codehaus.plexus.logging.Logger;
044
045@Component( role = WagonManager.class )
046public class DefaultWagonManager
047    extends org.apache.maven.repository.legacy.DefaultWagonManager
048    implements WagonManager
049{
050
051    // NOTE: This must use a different field name than in the super class or IoC has no chance to inject the loggers
052    @Requirement
053    private Logger log;
054
055    @Requirement
056    private LegacySupport legacySupport;
057
058    @Requirement
059    private SettingsDecrypter settingsDecrypter;
060
061    @Requirement
062    private MirrorSelector mirrorSelector;
063
064    @Requirement
065    private ArtifactRepositoryFactory artifactRepositoryFactory;
066
067    public AuthenticationInfo getAuthenticationInfo( String id )
068    {
069        MavenSession session = legacySupport.getSession();
070
071        if ( session != null && id != null )
072        {
073            MavenExecutionRequest request = session.getRequest();
074
075            if ( request != null )
076            {
077                List<Server> servers = request.getServers();
078
079                if ( servers != null )
080                {
081                    for ( Server server : servers )
082                    {
083                        if ( id.equalsIgnoreCase( server.getId() ) )
084                        {
085                            SettingsDecryptionResult result =
086                                settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) );
087                            server = result.getServer();
088
089                            AuthenticationInfo authInfo = new AuthenticationInfo();
090                            authInfo.setUserName( server.getUsername() );
091                            authInfo.setPassword( server.getPassword() );
092                            authInfo.setPrivateKey( server.getPrivateKey() );
093                            authInfo.setPassphrase( server.getPassphrase() );
094
095                            return authInfo;
096                        }
097                    }
098                }
099            }
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}