1 | |
package org.apache.maven.wagon.providers.webdav; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.httpclient.HttpException; |
23 | |
import org.apache.commons.httpclient.HttpStatus; |
24 | |
import org.apache.jackrabbit.webdav.DavConstants; |
25 | |
import org.apache.jackrabbit.webdav.DavException; |
26 | |
import org.apache.jackrabbit.webdav.MultiStatus; |
27 | |
import org.apache.jackrabbit.webdav.MultiStatusResponse; |
28 | |
import org.apache.jackrabbit.webdav.client.methods.MkColMethod; |
29 | |
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; |
30 | |
import org.apache.jackrabbit.webdav.property.DavProperty; |
31 | |
import org.apache.jackrabbit.webdav.property.DavPropertyName; |
32 | |
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet; |
33 | |
import org.apache.jackrabbit.webdav.property.DavPropertySet; |
34 | |
import org.apache.maven.wagon.PathUtils; |
35 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
36 | |
import org.apache.maven.wagon.TransferFailedException; |
37 | |
import org.apache.maven.wagon.WagonConstants; |
38 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
39 | |
import org.apache.maven.wagon.repository.Repository; |
40 | |
import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon; |
41 | |
import org.codehaus.plexus.util.FileUtils; |
42 | |
import org.codehaus.plexus.util.StringUtils; |
43 | |
import org.w3c.dom.Node; |
44 | |
|
45 | |
import java.io.File; |
46 | |
import java.io.IOException; |
47 | |
import java.net.URLDecoder; |
48 | |
import java.util.ArrayList; |
49 | |
import java.util.List; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 148 | public class WebDavWagon |
65 | |
extends AbstractHttpClientWagon |
66 | |
{ |
67 | |
protected static final String CONTINUE_ON_FAILURE_PROPERTY = "wagon.webdav.continueOnFailure"; |
68 | |
|
69 | 148 | private final boolean continueOnFailure = Boolean.getBoolean( CONTINUE_ON_FAILURE_PROPERTY ); |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 1 | private static final String[][] protocolMap = |
80 | |
new String[][]{ { "dav:http://", "http://" }, |
81 | |
{ "dav:https://", "https://" }, |
82 | |
{ "dav+http://", "http://" }, |
83 | |
{ "dav+https://", "https://" }, |
84 | |
{ "dav://", "http://" }, |
85 | |
{ "davs://", "https://" } }; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public boolean supportsDirectoryCopy() |
93 | |
{ |
94 | 8 | return true; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected void mkdirs( String dir ) |
107 | |
throws IOException |
108 | |
{ |
109 | 104 | Repository repository = getRepository(); |
110 | 104 | String basedir = repository.getBasedir(); |
111 | |
|
112 | 104 | String baseUrl = repository.getProtocol() + "://" + repository.getHost(); |
113 | 104 | if ( repository.getPort() != WagonConstants.UNKNOWN_PORT ) |
114 | |
{ |
115 | 104 | baseUrl += ":" + repository.getPort(); |
116 | |
} |
117 | |
|
118 | |
|
119 | 104 | String relpath = FileUtils.normalize( getPath( basedir, dir ) + "/" ); |
120 | |
|
121 | 104 | PathNavigator navigator = new PathNavigator( relpath ); |
122 | |
|
123 | |
|
124 | |
|
125 | 104 | int status = SC_NULL; |
126 | |
do |
127 | |
{ |
128 | 186 | String url = baseUrl + "/" + navigator.getPath(); |
129 | 186 | status = doMkCol( url ); |
130 | 186 | if ( status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED |
131 | |
|| status == HttpStatus.SC_METHOD_NOT_ALLOWED ) |
132 | |
{ |
133 | 32 | break; |
134 | |
} |
135 | |
} |
136 | 98 | while ( navigator.backward() ); |
137 | |
|
138 | |
|
139 | 186 | while ( navigator.forward() ) |
140 | |
{ |
141 | 82 | String url = baseUrl + "/" + navigator.getPath(); |
142 | 82 | status = doMkCol( url ); |
143 | 82 | if ( status != HttpStatus.SC_OK && status != HttpStatus.SC_CREATED ) |
144 | |
{ |
145 | 0 | throw new IOException( "Unable to create collection: " + url + "; status code = " + status ); |
146 | |
} |
147 | 82 | } |
148 | 104 | } |
149 | |
|
150 | |
private int doMkCol( String url ) |
151 | |
throws IOException |
152 | |
{ |
153 | 268 | MkColMethod method = null; |
154 | |
try |
155 | |
{ |
156 | 268 | method = new MkColMethod( url ); |
157 | 268 | return execute( method ); |
158 | |
} |
159 | |
finally |
160 | |
{ |
161 | 268 | if ( method != null ) |
162 | |
{ |
163 | 268 | method.releaseConnection(); |
164 | |
} |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public void putDirectory( File sourceDirectory, String destinationDirectory ) |
178 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
179 | |
{ |
180 | 44 | File[] listFiles = sourceDirectory.listFiles(); |
181 | |
|
182 | 116 | for ( int i = 0; i < listFiles.length; i++ ) |
183 | |
{ |
184 | 72 | if ( listFiles[i].isDirectory() ) |
185 | |
{ |
186 | 36 | putDirectory( listFiles[i], destinationDirectory + "/" + listFiles[i].getName() ); |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 36 | String target = destinationDirectory + "/" + listFiles[i].getName(); |
191 | |
|
192 | 36 | put( listFiles[i], target ); |
193 | |
} |
194 | |
} |
195 | |
|
196 | 44 | } |
197 | |
|
198 | |
private boolean isDirectory( String url ) |
199 | |
throws IOException, DavException |
200 | |
{ |
201 | 14 | DavPropertyNameSet nameSet = new DavPropertyNameSet(); |
202 | 14 | nameSet.add( DavPropertyName.create( DavConstants.PROPERTY_RESOURCETYPE ) ); |
203 | |
|
204 | 14 | PropFindMethod method = null; |
205 | |
try |
206 | |
{ |
207 | 14 | method = new PropFindMethod( url, nameSet, DavConstants.DEPTH_0 ); |
208 | 14 | execute( method ); |
209 | 14 | if ( method.succeeded() ) |
210 | |
{ |
211 | 10 | MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); |
212 | 10 | MultiStatusResponse response = multiStatus.getResponses()[0]; |
213 | 10 | DavPropertySet propertySet = response.getProperties( HttpStatus.SC_OK ); |
214 | 10 | DavProperty property = propertySet.get( DavConstants.PROPERTY_RESOURCETYPE ); |
215 | 10 | if ( property != null ) |
216 | |
{ |
217 | 10 | Node node = (Node) property.getValue(); |
218 | 10 | return node.getLocalName().equals( DavConstants.XML_COLLECTION ); |
219 | |
} |
220 | |
} |
221 | 4 | return false; |
222 | |
} |
223 | |
finally |
224 | |
{ |
225 | 14 | if ( method != null ) |
226 | |
{ |
227 | 14 | method.releaseConnection(); |
228 | |
} |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
public List<String> getFileList( String destinationDirectory ) |
233 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
234 | |
{ |
235 | 14 | String repositoryUrl = repository.getUrl(); |
236 | 14 | String url = repositoryUrl + ( repositoryUrl.endsWith( "/" ) ? "" : "/" ) + destinationDirectory; |
237 | |
|
238 | 14 | PropFindMethod method = null; |
239 | |
try |
240 | |
{ |
241 | 14 | if ( isDirectory( url ) ) |
242 | |
{ |
243 | 10 | DavPropertyNameSet nameSet = new DavPropertyNameSet(); |
244 | 10 | nameSet.add( DavPropertyName.create( DavConstants.PROPERTY_DISPLAYNAME ) ); |
245 | |
|
246 | 10 | method = new PropFindMethod( url, nameSet, DavConstants.DEPTH_1 ); |
247 | 10 | int status = execute( method ); |
248 | 10 | if ( method.succeeded() ) |
249 | |
{ |
250 | 10 | ArrayList<String> dirs = new ArrayList<String>(); |
251 | 10 | MultiStatus multiStatus = method.getResponseBodyAsMultiStatus(); |
252 | |
|
253 | 46 | for ( int i = 0; i < multiStatus.getResponses().length; i++ ) |
254 | |
{ |
255 | |
|
256 | 36 | MultiStatusResponse response = multiStatus.getResponses()[i]; |
257 | |
|
258 | 36 | String entryUrl = response.getHref(); |
259 | 36 | String fileName = PathUtils.filename( URLDecoder.decode( entryUrl ) ); |
260 | 36 | if ( entryUrl.endsWith( "/" ) ) |
261 | |
{ |
262 | 18 | if ( i == 0 ) |
263 | |
{ |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | 10 | continue; |
269 | |
} |
270 | |
|
271 | |
|
272 | 8 | fileName = PathUtils.filename( PathUtils.dirname( URLDecoder.decode( entryUrl ) ) ) + "/"; |
273 | |
} |
274 | |
|
275 | 26 | if ( !StringUtils.isEmpty( fileName ) ) |
276 | |
{ |
277 | 26 | dirs.add( fileName ); |
278 | |
} |
279 | |
} |
280 | 10 | return dirs; |
281 | |
} |
282 | |
|
283 | 0 | if ( status == HttpStatus.SC_NOT_FOUND ) |
284 | |
{ |
285 | 0 | throw new ResourceDoesNotExistException( "Destination directory does not exist: " + url ); |
286 | |
} |
287 | |
} |
288 | |
} |
289 | 0 | catch ( DavException e ) |
290 | |
{ |
291 | 0 | throw new TransferFailedException( e.getMessage(), e ); |
292 | |
} |
293 | 0 | catch ( IOException e ) |
294 | |
{ |
295 | 0 | throw new TransferFailedException( e.getMessage(), e ); |
296 | |
} |
297 | |
finally |
298 | |
{ |
299 | 14 | if ( method != null ) |
300 | |
{ |
301 | 10 | method.releaseConnection(); |
302 | |
} |
303 | |
} |
304 | 4 | throw new ResourceDoesNotExistException( |
305 | |
"Destination path exists but is not a " + "WebDAV collection (directory): " + url ); |
306 | |
} |
307 | |
|
308 | |
public String getURL( Repository repository ) |
309 | |
{ |
310 | 148 | String url = repository.getUrl(); |
311 | |
|
312 | |
|
313 | 852 | for ( int i = 0; i < protocolMap.length; i++ ) |
314 | |
{ |
315 | 812 | String protocol = protocolMap[i][0]; |
316 | 812 | if ( url.startsWith( protocol ) ) |
317 | |
{ |
318 | 108 | return protocolMap[i][1] + url.substring( protocol.length() ); |
319 | |
} |
320 | |
} |
321 | |
|
322 | |
|
323 | 40 | return url; |
324 | |
} |
325 | |
|
326 | |
|
327 | |
public void put( File source, String resourceName ) |
328 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
329 | |
{ |
330 | |
try |
331 | |
{ |
332 | 86 | super.put( source, resourceName ); |
333 | |
} |
334 | 10 | catch ( TransferFailedException e ) |
335 | |
{ |
336 | 10 | if ( continueOnFailure ) |
337 | |
{ |
338 | |
|
339 | 2 | System.out.println( |
340 | |
"WARN: Skip unable to transfer '" + resourceName + "' from '" + source.getPath() + "' due to " |
341 | |
+ e.getMessage() ); |
342 | |
} |
343 | |
else |
344 | |
{ |
345 | 8 | throw e; |
346 | |
} |
347 | 72 | } |
348 | 74 | } |
349 | |
} |