Skip to content

Commit 167ce51

Browse files
committed
Added helper for creating folder
1 parent 8951e6f commit 167ce51

File tree

7 files changed

+98
-18
lines changed

7 files changed

+98
-18
lines changed

ds3-sdk-integration/src/test/java/com/spectralogic/ds3client/helpers/FileSystemHelper_Test.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515

1616
package com.spectralogic.ds3client.helpers;
1717

18-
import com.google.common.collect.ImmutableList;
1918
import com.spectralogic.ds3client.Ds3Client;
20-
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
21-
import com.spectralogic.ds3client.commands.spectrads3.PutBulkJobSpectraS3Request;
22-
import com.spectralogic.ds3client.commands.spectrads3.PutBulkJobSpectraS3Response;
19+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2320
import com.spectralogic.ds3client.helpers.events.SameThreadEventRunner;
2421
import com.spectralogic.ds3client.integration.Util;
2522
import com.spectralogic.ds3client.integration.test.helpers.TempStorageIds;
@@ -320,10 +317,8 @@ public void createFolderWithSlash() throws IOException {
320317
final String folderName = "FolderNameWithSlash/";
321318

322319
try {
323-
final Ds3Object ds3Object = new Ds3Object(folderName, 0);
324-
final PutBulkJobSpectraS3Response jobResponse = client.putBulkJobSpectraS3(new PutBulkJobSpectraS3Request(BUCKET_NAME, ImmutableList.of(ds3Object)));
325-
326-
client.putFolder(new PutFolderRequest(BUCKET_NAME, folderName, jobResponse.getMasterObjectList().getJobId()));
320+
final PutFolderResponse response = HELPERS.createFolder(BUCKET_NAME, folderName);
321+
assertNotNull(response);
327322
} finally {
328323
deleteAllContents(client, BUCKET_NAME);
329324
}
@@ -334,10 +329,8 @@ public void createFolderWithNoSlash() throws IOException {
334329
final String folderName = "FolderNameNoSlash";
335330

336331
try {
337-
final Ds3Object ds3Object = new Ds3Object(folderName + "/", 0);
338-
final PutBulkJobSpectraS3Response jobResponse = client.putBulkJobSpectraS3(new PutBulkJobSpectraS3Request(BUCKET_NAME, ImmutableList.of(ds3Object)));
339-
340-
client.putFolder(new PutFolderRequest(BUCKET_NAME, folderName, jobResponse.getMasterObjectList().getJobId()));
332+
final PutFolderResponse response = HELPERS.createFolder(BUCKET_NAME, folderName);
333+
assertNotNull(response);
341334
} finally {
342335
deleteAllContents(client, BUCKET_NAME);
343336
}

ds3-sdk-integration/src/test/java/com/spectralogic/ds3client/integration/Smoke_Test.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
package com.spectralogic.ds3client.integration;
1717

18+
import com.google.common.collect.ImmutableList;
1819
import com.google.common.collect.ImmutableMap;
1920
import com.google.common.collect.Iterables;
2021
import com.google.common.collect.Lists;
2122
import com.spectralogic.ds3client.Ds3Client;
2223
import com.spectralogic.ds3client.commands.*;
24+
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
25+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2326
import com.spectralogic.ds3client.commands.interfaces.BulkResponse;
2427
import com.spectralogic.ds3client.commands.spectrads3.*;
2528
import com.spectralogic.ds3client.helpers.*;
@@ -1656,4 +1659,21 @@ public void testGetObjectsWithFullDetails() throws IOException, URISyntaxExcepti
16561659
deleteAllContents(client, bucketName);
16571660
}
16581661
}
1662+
1663+
@Test
1664+
public void createFolderWithSlash() throws IOException {
1665+
final String folderName = "FolderNameWithSlash/";
1666+
final String bucketName = "FolderNameWithSlashTestBucket";
1667+
1668+
try {
1669+
HELPERS.ensureBucketExists(bucketName, envDataPolicyId);
1670+
final Ds3Object ds3Object = new Ds3Object(folderName, 0);
1671+
final PutBulkJobSpectraS3Response jobResponse = client.putBulkJobSpectraS3(new PutBulkJobSpectraS3Request(bucketName, ImmutableList.of(ds3Object)));
1672+
1673+
final PutFolderResponse response = client.putFolder(new PutFolderRequest(bucketName, folderName, jobResponse.getMasterObjectList().getJobId()));
1674+
assertNotNull(response);
1675+
} finally {
1676+
deleteAllContents(client, bucketName);
1677+
}
1678+
}
16591679
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/commands/decorators/PutFolderRequest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.spectralogic.ds3client.commands.decorators;
1717

1818
import com.spectralogic.ds3client.commands.PutObjectRequest;
19+
import com.spectralogic.ds3client.exceptions.FolderNameMissingTrailingForwardSlash;
1920

2021
import java.util.UUID;
2122

@@ -24,22 +25,22 @@
2425
* ensure the correct creation of a folder using the Put Object command.
2526
*
2627
* A folder is created by putting an object with no content, of zero size, and with the
27-
* content-length=0. Also, the name of the folder must end with a slash '/'
28+
* content-length=0. Also, the name of the folder must end with a forward slash '/'.
2829
*/
2930
public class PutFolderRequest {
3031

3132
private final PutObjectRequest putObjectRequest;
3233

3334
public PutFolderRequest(final String bucketName, final String folderName, final UUID job) {
34-
putObjectRequest = new PutObjectRequest(bucketName, ensureEndsWithSlash(folderName), job, 0, 0, null);
35+
validateNameEndsWithSlash(folderName);
36+
putObjectRequest = new PutObjectRequest(bucketName, folderName, job, 0, 0, null);
3537
putObjectRequest.getHeaders().put("Content-Length", "0");
3638
}
3739

38-
private static String ensureEndsWithSlash(final String bucketName) {
39-
if (bucketName.endsWith("/")) {
40-
return bucketName;
40+
private static void validateNameEndsWithSlash(final String folderName) {
41+
if (!folderName.endsWith("/")) {
42+
throw new FolderNameMissingTrailingForwardSlash(folderName);
4143
}
42-
return bucketName + "/";
4344
}
4445

4546
public PutObjectRequest getPutObjectRequest() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.exceptions;
17+
18+
/**
19+
* Denotes an invalid folder name. Folder names must end with a forward slash '/'.
20+
*/
21+
public class FolderNameMissingTrailingForwardSlash extends IllegalArgumentException {
22+
23+
public FolderNameMissingTrailingForwardSlash(final String folderName) {
24+
super("Invalid folder name '" + folderName + "': folder names must end with a forward slash '/'.");
25+
}
26+
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpers.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.base.Function;
1919
import com.google.common.collect.FluentIterable;
2020
import com.spectralogic.ds3client.Ds3Client;
21+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2122
import com.spectralogic.ds3client.helpers.options.ReadJobOptions;
2223
import com.spectralogic.ds3client.helpers.options.WriteJobOptions;
2324
import com.spectralogic.ds3client.helpers.strategy.transferstrategy.TransferStrategy;
@@ -580,4 +581,9 @@ public static String stripLeadingPath(final String objectName, final String pref
580581
public abstract ObjectStorageSpaceVerificationResult objectsFromBucketWillFitInDirectory(final String bucketName,
581582
final Collection<String> objectNames,
582583
final Path destinationDirectory);
584+
585+
/**
586+
* Creates a folder in the specified bucket
587+
*/
588+
public abstract PutFolderResponse createFolder(final String bucketName, final String folderName) throws IOException;
583589
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpersImpl.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import com.spectralogic.ds3client.commands.HeadBucketRequest;
2626
import com.spectralogic.ds3client.commands.HeadBucketResponse;
2727
import com.spectralogic.ds3client.commands.PutBucketRequest;
28+
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
29+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2830
import com.spectralogic.ds3client.commands.spectrads3.*;
31+
import com.spectralogic.ds3client.exceptions.FolderNameMissingTrailingForwardSlash;
2932
import com.spectralogic.ds3client.helpers.events.EventRunner;
3033
import com.spectralogic.ds3client.helpers.events.SameThreadEventRunner;
3134
import com.spectralogic.ds3client.helpers.options.ReadJobOptions;
@@ -593,4 +596,28 @@ public ObjectStorageSpaceVerificationResult objectsFromBucketWillFitInDirectory(
593596
return fileSystemHelper.objectsFromBucketWillFitInDirectory(this,
594597
bucketName, objectNames, destinationDirectory);
595598
}
599+
600+
/**
601+
* Creates a folder in the specified bucket.
602+
*/
603+
@Override
604+
public PutFolderResponse createFolder(final String bucketName, final String folderName) throws IOException {
605+
final String normalizedFolderName = ensureNameEndsWithSlash(folderName);
606+
final Ds3Object ds3Object = new Ds3Object(normalizedFolderName, 0);
607+
final PutBulkJobSpectraS3Response jobResponse = client
608+
.putBulkJobSpectraS3(new PutBulkJobSpectraS3Request(bucketName, ImmutableList.of(ds3Object)));
609+
610+
return client.putFolder(new PutFolderRequest(bucketName, normalizedFolderName, jobResponse.getMasterObjectList().getJobId()));
611+
}
612+
613+
/**
614+
* Ensures that a folder names ends with a trailing forward slash. This prevents the
615+
* accidental creation of zero-length files when attempting to create a folder.
616+
*/
617+
private static String ensureNameEndsWithSlash(final String folderName) {
618+
if (folderName.endsWith("/")) {
619+
return folderName;
620+
}
621+
return folderName + "/";
622+
}
596623
}

ds3-sdk/src/test/java/com/spectralogic/ds3client/Ds3Client_Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import com.google.common.collect.Multimap;
2121
import com.google.common.collect.TreeMultimap;
2222
import com.spectralogic.ds3client.commands.*;
23+
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
2324
import com.spectralogic.ds3client.commands.spectrads3.*;
2425
import com.spectralogic.ds3client.exceptions.ContentLengthNotMatchException;
26+
import com.spectralogic.ds3client.exceptions.FolderNameMissingTrailingForwardSlash;
2527
import com.spectralogic.ds3client.models.*;
2628
import com.spectralogic.ds3client.models.Objects;
2729
import com.spectralogic.ds3client.models.bulk.Ds3Object;
@@ -1136,4 +1138,9 @@ public void getObjectVerifyFullPayload() throws IOException {
11361138
jobIdString,
11371139
0));
11381140
}
1141+
1142+
@Test (expected = FolderNameMissingTrailingForwardSlash.class)
1143+
public void createFolderWithNoSlash() throws IOException {
1144+
new PutFolderRequest("BucketName", "FolderNameNoSlash", UUID.randomUUID());
1145+
}
11391146
}

0 commit comments

Comments
 (0)