10
10
import java .util .HashMap ;
11
11
import java .util .List ;
12
12
import java .util .Map ;
13
+ import java .util .UUID ;
13
14
14
15
import org .json .JSONException ;
15
16
import org .json .JSONObject ;
@@ -29,6 +30,7 @@ public class DocLibClient extends SharePointClient {
29
30
* Instantiates a new file API client.
30
31
*
31
32
* @param serverUrl
33
+ * @param siteRelativeUrl
32
34
* @param credentials
33
35
*/
34
36
public DocLibClient (String serverUrl , String siteRelativeUrl , Credentials credentials ) {
@@ -261,7 +263,7 @@ public ListenableFuture<FileSystemItem> createFolder(String path) {
261
263
if (path == null || path .length () == 0 ) {
262
264
throw new IllegalArgumentException ("path cannot be null or empty" );
263
265
}
264
- final ListenableFuture <FileSystemItem > fileMetadata = createEmpty (path , null , FileConstants .FOLDER_CREATE );
266
+ final ListenableFuture <FileSystemItem > fileMetadata = createEmpty (path , ( String ) null , FileConstants .FOLDER_CREATE );
265
267
return fileMetadata ;
266
268
}
267
269
@@ -298,14 +300,15 @@ public ListenableFuture<FileSystemItem> createFile(String fileName) {
298
300
throw new IllegalArgumentException ("fileName cannot be null or empty" );
299
301
}
300
302
301
- final ListenableFuture <FileSystemItem > fileMetadata = createEmpty (fileName , null , FileConstants .FILE_CREATE );
303
+ final ListenableFuture <FileSystemItem > fileMetadata = createEmpty (fileName , ( String ) null , FileConstants .FILE_CREATE );
302
304
return fileMetadata ;
303
305
}
304
306
305
307
/**
306
308
* Creates an empty file.
307
309
*
308
310
* @param fileName
311
+ * @param library
309
312
* @return OfficeFuture<FileSystemItem>
310
313
*/
311
314
public ListenableFuture <FileSystemItem > createFile (String fileName , String library ) {
@@ -369,6 +372,74 @@ public void onSuccess(JSONObject json) {
369
372
return result ;
370
373
}
371
374
375
+
376
+ /**
377
+ * Creates an empty file.
378
+ *
379
+ * @param fileName
380
+ * @return OfficeFuture<FileSystemItem>
381
+ */
382
+ public ListenableFuture <FileSystemItem > createFile (String fileName , UUID libraryId ) {
383
+ if (fileName == null || fileName .length () == 0 ) {
384
+ throw new IllegalArgumentException ("fileName cannot be null or empty" );
385
+ }
386
+
387
+ if (libraryId == null || fileName .length () == 0 ) {
388
+ throw new IllegalArgumentException ("libraryName cannot be null or empty" );
389
+ }
390
+
391
+ final ListenableFuture <FileSystemItem > fileMetadata = createEmpty (fileName , libraryId , FileConstants .FILE_CREATE );
392
+ return fileMetadata ;
393
+ }
394
+
395
+ /**
396
+ * Creates a file with a given path inside a given library
397
+ *
398
+ * @param fileName
399
+ * @param libraryId
400
+ * @param overwrite
401
+ * @param content
402
+ * @return OfficeFuture<FileSystemItem>
403
+ */
404
+ public ListenableFuture <FileSystemItem > createFile (String fileName , UUID libraryId , boolean overwrite ,
405
+ byte [] content ) {
406
+
407
+ if (fileName == null || fileName .length () == 0 ) {
408
+ throw new IllegalArgumentException ("fileName cannot be null or empty" );
409
+ }
410
+
411
+ String urlPart = urlEncode (String .format ("Add(name='%s', overwrite='%s')" , fileName ,
412
+ Boolean .toString (overwrite )));
413
+
414
+ String url ;
415
+ if (libraryId == null ) {
416
+ url = getSiteUrl () + "_api/files/" + urlPart ;
417
+ } else {
418
+ url = getSiteUrl () + String .format ("_api/web/lists(guid'%s')/files/" , libraryId ) + urlPart ;
419
+ }
420
+ final SettableFuture <FileSystemItem > result = SettableFuture .create ();
421
+ Map <String , String > headers = new HashMap <String , String >();
422
+ headers .put ("Content-Type" , "application/octet-stream" );
423
+
424
+ ListenableFuture <JSONObject > request = executeRequestJsonWithDigest (url , "POST" , headers , content );
425
+
426
+ Futures .addCallback (request , new FutureCallback <JSONObject >() {
427
+ @ Override
428
+ public void onFailure (Throwable t ) {
429
+ result .setException (t );
430
+ }
431
+
432
+ @ Override
433
+ public void onSuccess (JSONObject json ) {
434
+ FileSystemItem item = new FileSystemItem ();
435
+ item .loadFromJson (json , true );
436
+ result .set (item );
437
+ }
438
+
439
+ });
440
+ return result ;
441
+ }
442
+
372
443
/**
373
444
* Creates the file with a given file name and content
374
445
*
@@ -382,7 +453,7 @@ public void onSuccess(JSONObject json) {
382
453
*/
383
454
public ListenableFuture <FileSystemItem > createFile (String fileName , boolean overwrite , byte [] content ) {
384
455
385
- return createFile (fileName , null , overwrite , content );
456
+ return createFile (fileName , ( String ) null , overwrite , content );
386
457
}
387
458
388
459
/**
@@ -567,6 +638,52 @@ public void onSuccess(JSONObject json) {
567
638
return result ;
568
639
}
569
640
641
+
642
+ /**
643
+ * Creates the empty.
644
+ *
645
+ * @param path
646
+ * @param metadata
647
+ * content for the file
648
+ * @return OfficeFuture<FileSystemItem>
649
+ */
650
+ private ListenableFuture <FileSystemItem > createEmpty (String path , UUID libraryId , String metadata ) {
651
+
652
+ final SettableFuture <FileSystemItem > result = SettableFuture .create ();
653
+
654
+ String postUrl = null ;
655
+ if (libraryId == null ) {
656
+ postUrl = getSiteUrl () + "_api/files" ;
657
+ } else {
658
+ postUrl = getSiteUrl () + String .format ("_api/web/lists(guid'%s')/files" , libraryId );
659
+ }
660
+
661
+ byte [] payload = null ;
662
+ try {
663
+ String completeMetada = String .format (metadata , path );
664
+ payload = completeMetada .getBytes (Constants .UTF8_NAME );
665
+ ListenableFuture <JSONObject > request = executeRequestJsonWithDigest (postUrl , "POST" , null , payload );
666
+
667
+ Futures .addCallback (request , new FutureCallback <JSONObject >() {
668
+ @ Override
669
+ public void onFailure (Throwable t ) {
670
+ result .setException (t );
671
+ }
672
+
673
+ @ Override
674
+ public void onSuccess (JSONObject json ) {
675
+ FileSystemItem item = new FileSystemItem ();
676
+ item .loadFromJson (json , true );
677
+ result .set (item );
678
+ }
679
+ });
680
+
681
+ } catch (UnsupportedEncodingException e ) {
682
+ result .setException (e );
683
+ }
684
+ return result ;
685
+ }
686
+
570
687
private ListenableFuture <Void > fileOp (final String operation , String source , String destination , boolean overwrite ,
571
688
String library ) {
572
689
final SettableFuture <Void > result = SettableFuture .create ();
0 commit comments