View Javadoc

1   package org.codehaus.mojo.wagon.shared;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import org.apache.maven.artifact.manager.WagonConfigurationException;
19  import org.apache.maven.artifact.manager.WagonManager;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugin.logging.Log;
22  import org.apache.maven.settings.Proxy;
23  import org.apache.maven.settings.Server;
24  import org.apache.maven.settings.Settings;
25  import org.apache.maven.wagon.UnsupportedProtocolException;
26  import org.apache.maven.wagon.Wagon;
27  import org.apache.maven.wagon.WagonException;
28  import org.apache.maven.wagon.observers.Debug;
29  import org.apache.maven.wagon.proxy.ProxyInfo;
30  import org.apache.maven.wagon.repository.Repository;
31  import org.apache.maven.wagon.repository.RepositoryPermissions;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  public class WagonUtils {
35  
36      /**
37       * Convenient method to create a wagon
38       *
39       * @param id
40       * @param url
41       * @param wagonManager
42       * @param settings
43       * @param logger
44       * @return
45       * @throws MojoExecutionException
46       */
47      public static Wagon createWagon(String id, String url, WagonManager wagonManager, Settings settings, Log logger)
48              throws WagonException, UnsupportedProtocolException, WagonConfigurationException {
49          Wagon wagon = null;
50  
51          final Repository repository = new Repository(id, url);
52          repository.setPermissions(getPermissions(id, settings));
53  
54          wagon = wagonManager.getWagon(repository);
55  
56          if (logger.isDebugEnabled()) {
57              Debug debug = new Debug();
58              wagon.addSessionListener(debug);
59              wagon.addTransferListener(debug);
60          }
61  
62          ProxyInfo proxyInfo = getProxyInfo(settings);
63          if (proxyInfo != null) {
64              wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()), proxyInfo);
65          } else {
66              wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()));
67          }
68  
69          return wagon;
70      }
71  
72      protected static RepositoryPermissions getPermissions(String id, Settings settings) {
73          // May not have an id
74          if (StringUtils.isBlank(id)) {
75              return null;
76          }
77  
78          // May not be a server matching that id
79          Server server = settings.getServer(id);
80          if (server == null) {
81              return null;
82          }
83  
84          // Extract perms (if there are any)
85          String filePerms = server.getFilePermissions();
86          String dirPerms = server.getDirectoryPermissions();
87  
88          // Check to see if custom permissions were supplied
89          if (StringUtils.isBlank(filePerms) && StringUtils.isBlank(dirPerms)) {
90              return null;
91          }
92  
93          // There are custom permissions specified in settings.xml for this server
94          RepositoryPermissions permissions = new RepositoryPermissions();
95          permissions.setFileMode(filePerms);
96          permissions.setDirectoryMode(dirPerms);
97          return permissions;
98      }
99  
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 }