1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.wagon.shared;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import org.apache.maven.artifact.manager.WagonConfigurationException;
34 import org.apache.maven.artifact.manager.WagonManager;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.logging.Log;
37 import org.apache.maven.settings.Proxy;
38 import org.apache.maven.settings.Server;
39 import org.apache.maven.settings.Settings;
40 import org.apache.maven.wagon.UnsupportedProtocolException;
41 import org.apache.maven.wagon.Wagon;
42 import org.apache.maven.wagon.WagonException;
43 import org.apache.maven.wagon.observers.Debug;
44 import org.apache.maven.wagon.proxy.ProxyInfo;
45 import org.apache.maven.wagon.repository.Repository;
46 import org.apache.maven.wagon.repository.RepositoryPermissions;
47 import org.codehaus.plexus.util.StringUtils;
48
49 public class WagonUtils {
50
51
52
53
54
55
56
57
58
59
60
61
62 public static Wagon createWagon(String id, String url, WagonManager wagonManager, Settings settings, Log logger)
63 throws WagonException, UnsupportedProtocolException, WagonConfigurationException {
64 Wagon wagon = null;
65
66 final Repository repository = new Repository(id, url);
67 repository.setPermissions(getPermissions(id, settings));
68
69 wagon = wagonManager.getWagon(repository);
70
71 if (logger.isDebugEnabled()) {
72 Debug debug = new Debug();
73 wagon.addSessionListener(debug);
74 wagon.addTransferListener(debug);
75 }
76
77 ProxyInfo proxyInfo = getProxyInfo(settings);
78 if (proxyInfo != null) {
79 wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()), proxyInfo);
80 } else {
81 wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()));
82 }
83
84 return wagon;
85 }
86
87 protected static RepositoryPermissions getPermissions(String id, Settings settings) {
88
89 if (StringUtils.isBlank(id)) {
90 return null;
91 }
92
93
94 Server server = settings.getServer(id);
95 if (server == null) {
96 return null;
97 }
98
99
100 String filePerms = server.getFilePermissions();
101 String dirPerms = server.getDirectoryPermissions();
102
103
104 if (StringUtils.isBlank(filePerms) && StringUtils.isBlank(dirPerms)) {
105 return null;
106 }
107
108
109 RepositoryPermissions permissions = new RepositoryPermissions();
110 permissions.setFileMode(filePerms);
111 permissions.setDirectoryMode(dirPerms);
112 return permissions;
113 }
114
115 public static WagonFileSet getWagonFileSet(String fromDir, String includes, String excludes,
116 boolean isCaseSensitive, String toDir) {
117 WagonFileSet fileSet = new WagonFileSet();
118 fileSet.setDirectory(fromDir);
119
120 if (!StringUtils.isBlank(includes)) {
121 fileSet.setIncludes(StringUtils.split(includes, ","));
122 }
123
124 if (!StringUtils.isBlank(excludes)) {
125 fileSet.setExcludes(StringUtils.split(excludes, ","));
126 }
127
128 fileSet.setCaseSensitive(isCaseSensitive);
129
130 fileSet.setOutputDirectory(toDir);
131
132 return fileSet;
133
134 }
135
136
137
138
139
140
141
142 public static ProxyInfo getProxyInfo(Settings settings) {
143 ProxyInfo proxyInfo = null;
144 if (settings != null && settings.getActiveProxy() != null) {
145 Proxy settingsProxy = settings.getActiveProxy();
146
147 proxyInfo = new ProxyInfo();
148 proxyInfo.setHost(settingsProxy.getHost());
149 proxyInfo.setType(settingsProxy.getProtocol());
150 proxyInfo.setPort(settingsProxy.getPort());
151 proxyInfo.setNonProxyHosts(settingsProxy.getNonProxyHosts());
152 proxyInfo.setUserName(settingsProxy.getUsername());
153 proxyInfo.setPassword(settingsProxy.getPassword());
154 }
155
156 return proxyInfo;
157 }
158
159 }