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