Skip to content

Commit 8951e6f

Browse files
committed
Created PutFolder command which wraps PutObject
1 parent c71b878 commit 8951e6f

File tree

5 files changed

+128
-10
lines changed

5 files changed

+128
-10
lines changed

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

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
package com.spectralogic.ds3client.helpers;
1717

18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
20-
18+
import com.google.common.collect.ImmutableList;
2119
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;
2223
import com.spectralogic.ds3client.helpers.events.SameThreadEventRunner;
2324
import com.spectralogic.ds3client.integration.Util;
2425
import com.spectralogic.ds3client.integration.test.helpers.TempStorageIds;
@@ -31,6 +32,8 @@
3132
import org.junit.Before;
3233
import org.junit.BeforeClass;
3334
import org.junit.Test;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3437

3538
import java.io.BufferedReader;
3639
import java.io.IOException;
@@ -39,17 +42,15 @@
3942
import java.nio.file.Files;
4043
import java.nio.file.Path;
4144
import java.nio.file.Paths;
42-
import java.util.*;
45+
import java.util.ArrayList;
46+
import java.util.Arrays;
47+
import java.util.List;
48+
import java.util.UUID;
4349
import java.util.concurrent.atomic.AtomicInteger;
4450
import java.util.concurrent.atomic.AtomicLong;
4551

4652
import static com.spectralogic.ds3client.integration.Util.deleteAllContents;
47-
48-
import static org.junit.Assert.assertEquals;
49-
import static org.junit.Assert.assertTrue;
50-
import static org.junit.Assert.assertFalse;
51-
import static org.junit.Assert.assertNull;
52-
import static org.junit.Assert.assertNotNull;
53+
import static org.junit.Assert.*;
5354

5455
public class FileSystemHelper_Test {
5556
private static final Logger LOG = LoggerFactory.getLogger(FileSystemHelper_Test.class);
@@ -313,4 +314,32 @@ public long getAvailableFileSpace(final Path path) throws IOException {
313314
throw new IOException("IOExceptionAtor");
314315
}
315316
}
317+
318+
@Test
319+
public void createFolderWithSlash() throws IOException {
320+
final String folderName = "FolderNameWithSlash/";
321+
322+
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()));
327+
} finally {
328+
deleteAllContents(client, BUCKET_NAME);
329+
}
330+
}
331+
332+
@Test
333+
public void createFolderWithNoSlash() throws IOException {
334+
final String folderName = "FolderNameNoSlash";
335+
336+
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()));
341+
} finally {
342+
deleteAllContents(client, BUCKET_NAME);
343+
}
344+
}
316345
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/Ds3Client.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.spectralogic.ds3client.annotations.Resource;
2121
import com.spectralogic.ds3client.annotations.ResponsePayloadModel;
2222
import com.spectralogic.ds3client.commands.*;
23+
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
24+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2325
import com.spectralogic.ds3client.commands.spectrads3.*;
2426
import com.spectralogic.ds3client.commands.spectrads3.notifications.*;
2527
import com.spectralogic.ds3client.models.JobNode;
@@ -35,6 +37,8 @@ public interface Ds3Client extends Closeable {
3537

3638
ConnectionDetails getConnectionDetails();
3739

40+
PutFolderResponse putFolder(final PutFolderRequest request)
41+
throws IOException;
3842

3943

4044
AbortMultiPartUploadResponse abortMultiPartUpload(final AbortMultiPartUploadRequest request)

ds3-sdk/src/main/java/com/spectralogic/ds3client/Ds3ClientImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import com.spectralogic.ds3client.commands.*;
21+
import com.spectralogic.ds3client.commands.decorators.PutFolderRequest;
22+
import com.spectralogic.ds3client.commands.decorators.PutFolderResponse;
2123
import com.spectralogic.ds3client.commands.parsers.*;
2224
import com.spectralogic.ds3client.commands.spectrads3.*;
2325
import com.spectralogic.ds3client.commands.spectrads3.notifications.*;
@@ -45,6 +47,12 @@ public ConnectionDetails getConnectionDetails() {
4547
return this.netClient.getConnectionDetails();
4648
}
4749

50+
@Override
51+
public PutFolderResponse putFolder(final PutFolderRequest request) throws IOException {
52+
final PutObjectResponse response = putObject(request.getPutObjectRequest());
53+
return new PutFolderResponse(response);
54+
}
55+
4856
@Override
4957
public AbortMultiPartUploadResponse abortMultiPartUpload(final AbortMultiPartUploadRequest request) throws IOException {
5058
return new AbortMultiPartUploadResponseParser().response(this.netClient.getResponse(request));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.commands.decorators;
17+
18+
import com.spectralogic.ds3client.commands.PutObjectRequest;
19+
20+
import java.util.UUID;
21+
22+
/**
23+
* Decorates the {@link com.spectralogic.ds3client.commands.PutObjectRequest} and is used to
24+
* ensure the correct creation of a folder using the Put Object command.
25+
*
26+
* 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+
*/
29+
public class PutFolderRequest {
30+
31+
private final PutObjectRequest putObjectRequest;
32+
33+
public PutFolderRequest(final String bucketName, final String folderName, final UUID job) {
34+
putObjectRequest = new PutObjectRequest(bucketName, ensureEndsWithSlash(folderName), job, 0, 0, null);
35+
putObjectRequest.getHeaders().put("Content-Length", "0");
36+
}
37+
38+
private static String ensureEndsWithSlash(final String bucketName) {
39+
if (bucketName.endsWith("/")) {
40+
return bucketName;
41+
}
42+
return bucketName + "/";
43+
}
44+
45+
public PutObjectRequest getPutObjectRequest() {
46+
return putObjectRequest;
47+
}
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.commands.decorators;
17+
18+
import com.spectralogic.ds3client.commands.PutObjectResponse;
19+
20+
/**
21+
* Decorates the {@link com.spectralogic.ds3client.commands.PutObjectResponse} and is used
22+
* by command {@link com.spectralogic.ds3client.Ds3Client#putFolder(PutFolderRequest)}
23+
*/
24+
public class PutFolderResponse extends PutObjectResponse {
25+
26+
public PutFolderResponse(final PutObjectResponse response) {
27+
super(response.getChecksum(), response.getChecksumType());
28+
}
29+
}

0 commit comments

Comments
 (0)