23
23
import org .apache .jackrabbit .webdav .xml .Namespace ;
24
24
25
25
import java .io .*;
26
- import java .net .MalformedURLException ;
27
26
import java .net .URI ;
28
27
import java .net .URISyntaxException ;
29
28
import java .net .URLDecoder ;
39
38
public abstract class WebDAVWorkspaceManagerBase implements WorkspaceManager {
40
39
41
40
protected static String WS_HOST_URL = AppProperties .getProperty ("workspace.host.url" , "https://irsa.ipac.caltech.edu" );
41
+ private static boolean INFINITY_DEPTH_SUPPORTED = AppProperties .getBooleanProperty ("workspace.propfind.infinity" , true );
42
42
43
43
private static final Logger .LoggerImpl LOG = Logger .getLogger ();
44
44
@@ -100,7 +100,7 @@ private Namespace getNamespace() {
100
100
101
101
102
102
103
- boolean exists (String relRemoteUri ) {
103
+ boolean exists (String relRemoteUri ) throws WsException {
104
104
return getMeta (relRemoteUri , WspaceMeta .Includes .NONE ) != null ;
105
105
}
106
106
@@ -169,8 +169,8 @@ public WsResponse davPut(File upload, String relPath, boolean overwrite, String
169
169
return WsUtil .success (put .getStatusCode (), put .getStatusText (), newUrl );
170
170
} catch (Exception e ) {
171
171
LOG .error (e , "Error while uploading file:" + upload .getPath ());
172
+ return WsUtil .error (500 , "Error while uploading file:" + upload .getPath ());
172
173
}
173
- return WsUtil .error (500 );
174
174
}
175
175
176
176
private WsResponse davGet (File outfile , String fromPath ) {
@@ -207,7 +207,7 @@ public PROTOCOL getProtocol() {
207
207
}
208
208
209
209
@ Override
210
- public WsResponse getList (String parentUri , int depth ) {
210
+ public WsResponse getList (String parentUri , int depth ) throws WsException {
211
211
212
212
WspaceMeta .Includes prop = WspaceMeta .Includes .CHILDREN_PROPS ;
213
213
@@ -284,12 +284,13 @@ public WsResponse delete(String uri) {
284
284
}
285
285
} catch (Exception e ) {
286
286
LOG .error (e , "Error while deleting remote file:" + url );
287
+ return WsUtil .error (e );
287
288
}
288
289
return WsUtil .success (200 , "Deleted " + uri , uri );
289
290
}
290
291
291
292
@ Override
292
- public WsResponse createParent (String newRelPath ) {
293
+ public WsResponse createParent (String newRelPath ) throws WsException {
293
294
String [] parts = newRelPath .split ("/" );
294
295
String cdir = "/" ;
295
296
for (String s : parts ) {
@@ -305,15 +306,15 @@ public WsResponse createParent(String newRelPath) {
305
306
LOG .error ("Unable to create directory:" + newRelPath + " -- " + mkcol .getStatusText ());
306
307
return WsUtil .error (mkcol , "Resource already exist" );
307
308
}
308
- } catch (URISyntaxException |MalformedURLException e ) {
309
+ } catch (URISyntaxException |IOException e ) {
309
310
return WsUtil .error (e );
310
311
}
311
312
}
312
313
}
313
314
return WsUtil .success (200 , "Created" , getResourceUrl (newRelPath ));
314
315
}
315
316
316
- public WsResponse moveFile (String originalFileRelPath , String newPath , boolean overwrite ) {
317
+ public WsResponse moveFile (String originalFileRelPath , String newPath , boolean overwrite ) throws WsException {
317
318
WspaceMeta meta = new WspaceMeta (newPath );
318
319
String parent = meta .getParentPath ();
319
320
@@ -326,14 +327,14 @@ public WsResponse moveFile(String originalFileRelPath, String newPath, boolean o
326
327
MoveMethod move = new MoveMethod (getResourceUrl (originalFileRelPath ), newUrl , overwrite );
327
328
if (!executeMethod (move )) {
328
329
// handle error
329
- LOG .error ("Unable to move:" + originalFileRelPath + " based on url -- " +newUrl + " -- " + move .getStatusText ());
330
+ LOG .error ("Unable to move:" + originalFileRelPath + " based on url -- " + newUrl + " -- " + move .getStatusText ());
330
331
return WsUtil .error (move .getStatusCode (), move .getStatusLine ().getReasonPhrase ());
331
332
}
332
333
return WsUtil .success (move .getStatusCode (), move .getStatusText (), newUrl );
333
334
}
334
335
335
336
@ Override
336
- public WsResponse renameFile (String originalFileRelPath , String newfileName , boolean overwrite ) {
337
+ public WsResponse renameFile (String originalFileRelPath , String newfileName , boolean overwrite ) throws WsException {
337
338
WspaceMeta meta = new WspaceMeta (originalFileRelPath );
338
339
String parent = meta .getParentPath ();
339
340
@@ -354,19 +355,20 @@ public WsResponse renameFile(String originalFileRelPath, String newfileName, boo
354
355
return WsUtil .success (move .getStatusCode (), move .getStatusText (), newUrl );
355
356
}
356
357
357
- public WspaceMeta getMeta (String relPath ) {
358
- return getMeta (relPath , WspaceMeta .Includes .ALL );
359
- }
358
+ // public WspaceMeta getMeta(String relPath) {
359
+ // return getMeta(relPath, WspaceMeta.Includes.ALL);
360
+ // }
360
361
361
- public WspaceMeta getMeta (String relPath , WspaceMeta .Includes includes ) {
362
+ public WspaceMeta getMeta (String relPath , WspaceMeta .Includes includes ) throws WsException {
362
363
363
364
// this can be optimized by retrieving only the props we care for.
364
365
DavMethod pFind = null ;
365
366
try {
367
+ int depth = getDepth (includes );
366
368
if (includes .inclProps ) {
367
- pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_ALL_PROP , includes . depth );
369
+ pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_ALL_PROP , depth );
368
370
} else {
369
- pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_BY_PROPERTY , includes . depth );
371
+ pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_BY_PROPERTY , depth );
370
372
}
371
373
372
374
if (!executeMethod (pFind , false )) {
@@ -394,12 +396,16 @@ public WspaceMeta getMeta(String relPath, WspaceMeta.Includes includes) {
394
396
return root ;
395
397
} catch (Exception e ) {
396
398
LOG .error (e , "Error while getting meta for:" + relPath );
399
+ if (e instanceof WsException ) {
400
+ throw (WsException )e ;
401
+ } else {
402
+ throw new WsException (e .getMessage (), e );
403
+ }
397
404
} finally {
398
405
if (pFind != null ) {
399
406
pFind .releaseConnection ();
400
407
}
401
408
}
402
- return null ;
403
409
}
404
410
405
411
@@ -488,7 +494,7 @@ private WspaceMeta convertToWspaceMeta(WspaceMeta meta, MultiStatusResponse res)
488
494
} else if (name .equals (DavConstants .PROPERTY_GETCONTENTLENGTH )) {
489
495
long size = Long .parseLong (v );
490
496
meta .setSize (size );
491
- //meta.setIsFile(true); // WEBDAV/IRSA only set content length for files. // not WEBDav standard behaviour
497
+ //meta.setIsFile(true); // not WEBDav standard behaviour, in WEBDAV/IRSA only files have content length
492
498
} else if (name .equals (DavConstants .PROPERTY_GETCONTENTTYPE )) {
493
499
meta .setContentType (v );
494
500
} else if (p .getName ().getNamespace ().equals (namespace )) {
@@ -513,16 +519,16 @@ private void addToRoot(WspaceMeta root, MultiStatusResponse res) throws Unsuppor
513
519
root .addChild (meta );
514
520
}
515
521
}
516
- boolean executeMethod (DavMethod method ) {
522
+ boolean executeMethod (DavMethod method ) throws WsException {
517
523
return executeMethod (method , true );
518
524
}
519
525
520
- private boolean executeMethod (DavMethod method , boolean releaseConnection ) {
526
+ private boolean executeMethod (DavMethod method , boolean releaseConnection ) throws WsException {
521
527
try {
522
528
HttpServices .Status status = doExecuteMethod (method );
523
529
return !status .isError ();
524
530
} catch (IOException e ) {
525
- return false ;
531
+ throw new WsException ( e . getMessage (), e ) ;
526
532
} finally {
527
533
if (releaseConnection && method != null ) {
528
534
method .releaseConnection ();
@@ -539,7 +545,7 @@ protected HttpServices.Status doExecuteMethod(DavMethod method) throws IOExcepti
539
545
private WsResponse getResponse (String relPath ){
540
546
DavMethod pFind = null ;
541
547
try {
542
- pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_BY_PROPERTY , WspaceMeta .Includes .ALL_PROPS . depth );
548
+ pFind = new PropFindMethod (getResourceUrl (relPath ), DavConstants .PROPFIND_BY_PROPERTY , getDepth ( WspaceMeta .Includes .ALL_PROPS ) );
543
549
544
550
545
551
if (!executeMethod (pFind , false )) {
@@ -552,12 +558,21 @@ private WsResponse getResponse(String relPath){
552
558
return WsUtil .success (pFind .getStatusCode (), pFind .getStatusText (), pFind .getPath ());
553
559
} catch (Exception e ) {
554
560
LOG .error (e , "Error while getting meta for:" + relPath );
561
+ return WsUtil .error (500 , "Error while getting meta for:" + relPath );
555
562
} finally {
556
563
if (pFind != null ) {
557
564
pFind .releaseConnection ();
558
565
}
559
566
}
560
- return new WsResponse ();
567
+ }
568
+
569
+ /**
570
+ * Return allowed depth
571
+ * @param includes
572
+ * @return
573
+ */
574
+ private int getDepth (WspaceMeta .Includes includes ) {
575
+ return includes .depth > 1 && !INFINITY_DEPTH_SUPPORTED ? 1 : includes .depth ;
561
576
}
562
577
563
578
class WebDAVGetMethod extends DavMethodBase {
0 commit comments