001    package org.codehaus.mojo.wagon.shared;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005     * agreements. See the NOTICE file distributed with this work for additional information regarding
006     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance with the License. You may obtain a
008     * copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software distributed under the License
013     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014     * or implied. See the License for the specific language governing permissions and limitations under
015     * the License.
016     */
017    
018    import org.apache.maven.artifact.manager.WagonConfigurationException;
019    import org.apache.maven.artifact.manager.WagonManager;
020    import org.apache.maven.plugin.MojoExecutionException;
021    import org.apache.maven.plugin.logging.Log;
022    import org.apache.maven.settings.Proxy;
023    import org.apache.maven.settings.Server;
024    import org.apache.maven.settings.Settings;
025    import org.apache.maven.wagon.UnsupportedProtocolException;
026    import org.apache.maven.wagon.Wagon;
027    import org.apache.maven.wagon.WagonException;
028    import org.apache.maven.wagon.observers.Debug;
029    import org.apache.maven.wagon.proxy.ProxyInfo;
030    import org.apache.maven.wagon.repository.Repository;
031    import org.apache.maven.wagon.repository.RepositoryPermissions;
032    import org.codehaus.plexus.util.StringUtils;
033    
034    public class WagonUtils {
035    
036        /**
037         * Convenient method to create a wagon
038         *
039         * @param id
040         * @param url
041         * @param wagonManager
042         * @param settings
043         * @param logger
044         * @return
045         * @throws MojoExecutionException
046         */
047        public static Wagon createWagon(String id, String url, WagonManager wagonManager, Settings settings, Log logger)
048                throws WagonException, UnsupportedProtocolException, WagonConfigurationException {
049            Wagon wagon = null;
050    
051            final Repository repository = new Repository(id, url);
052            repository.setPermissions(getPermissions(id, settings));
053    
054            wagon = wagonManager.getWagon(repository);
055    
056            if (logger.isDebugEnabled()) {
057                Debug debug = new Debug();
058                wagon.addSessionListener(debug);
059                wagon.addTransferListener(debug);
060            }
061    
062            ProxyInfo proxyInfo = getProxyInfo(settings);
063            if (proxyInfo != null) {
064                wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()), proxyInfo);
065            } else {
066                wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()));
067            }
068    
069            return wagon;
070        }
071    
072        protected static RepositoryPermissions getPermissions(String id, Settings settings) {
073            // May not have an id
074            if (StringUtils.isBlank(id)) {
075                return null;
076            }
077    
078            // May not be a server matching that id
079            Server server = settings.getServer(id);
080            if (server == null) {
081                return null;
082            }
083    
084            // Extract perms (if there are any)
085            String filePerms = server.getFilePermissions();
086            String dirPerms = server.getDirectoryPermissions();
087    
088            // Check to see if custom permissions were supplied
089            if (StringUtils.isBlank(filePerms) && StringUtils.isBlank(dirPerms)) {
090                return null;
091            }
092    
093            // There are custom permissions specified in settings.xml for this server
094            RepositoryPermissions permissions = new RepositoryPermissions();
095            permissions.setFileMode(filePerms);
096            permissions.setDirectoryMode(dirPerms);
097            return permissions;
098        }
099    
100        public static WagonFileSet getWagonFileSet(String fromDir, String includes, String excludes,
101                boolean isCaseSensitive, String toDir) {
102            WagonFileSet fileSet = new WagonFileSet();
103            fileSet.setDirectory(fromDir);
104    
105            if (!StringUtils.isBlank(includes)) {
106                fileSet.setIncludes(StringUtils.split(includes, ","));
107            }
108    
109            if (!StringUtils.isBlank(excludes)) {
110                fileSet.setExcludes(StringUtils.split(excludes, ","));
111            }
112    
113            fileSet.setCaseSensitive(isCaseSensitive);
114    
115            fileSet.setOutputDirectory(toDir);
116    
117            return fileSet;
118    
119        }
120    
121        /**
122         * Convenience method to map a <code>Proxy</code> object from the user system settings to a <code>ProxyInfo</code>
123         * object.
124         *
125         * @return a proxyInfo object or null if no active proxy is define in the settings.xml
126         */
127        public static ProxyInfo getProxyInfo(Settings settings) {
128            ProxyInfo proxyInfo = null;
129            if (settings != null && settings.getActiveProxy() != null) {
130                Proxy settingsProxy = settings.getActiveProxy();
131    
132                proxyInfo = new ProxyInfo();
133                proxyInfo.setHost(settingsProxy.getHost());
134                proxyInfo.setType(settingsProxy.getProtocol());
135                proxyInfo.setPort(settingsProxy.getPort());
136                proxyInfo.setNonProxyHosts(settingsProxy.getNonProxyHosts());
137                proxyInfo.setUserName(settingsProxy.getUsername());
138                proxyInfo.setPassword(settingsProxy.getPassword());
139            }
140    
141            return proxyInfo;
142        }
143    
144    }