Skip to content

Commit ea8489c

Browse files
committed
Merge pull request #42 from hansdude/master
Removed the Closeable from Ds3Response.
2 parents 3c45598 + 60f1738 commit ea8489c

29 files changed

+201
-208
lines changed

README.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import com.spectralogic.ds3client.Ds3Client;
2727
import com.spectralogic.ds3client.Ds3ClientBuilder;
2828
import com.spectralogic.ds3client.commands.GetServiceRequest;
2929
import com.spectralogic.ds3client.commands.GetServiceResponse;
30-
import com.spectralogic.ds3client.models.Credentials;
3130
import com.spectralogic.ds3client.models.Bucket;
31+
import com.spectralogic.ds3client.models.Credentials;
3232

3333
import java.io.IOException;
3434
import java.security.SignatureException;
@@ -42,12 +42,11 @@ public class Ds3ServiceListExample {
4242
new Credentials("accessKey", "secretKey")).withHttpSecure(false).build();
4343

4444
// Tell the client to get us a list of all buckets, this is called a service list.
45-
try (final GetServiceResponse response = client.getService(new GetServiceRequest())) {
45+
final GetServiceResponse response = client.getService(new GetServiceRequest());
4646

47-
// Iterate through all the buckets and print them to the console.
48-
for (final Bucket bucket : response.getResult().getBuckets()) {
49-
System.out.println(bucket.getName());
50-
}
47+
// Iterate through all the buckets and print them to the console.
48+
for (final Bucket bucket : response.getResult().getBuckets()) {
49+
System.out.println(bucket.getName());
5150
}
5251
}
5352
}
@@ -100,17 +99,17 @@ public class BulkPutExample {
10099

101100
// Create the write job with the bucket we want to write to and the list
102101
// of objects that will be written
103-
final Ds3ClientHelpers.WriteJob job = helper.startWriteJob(bucketName, objects);
102+
final Ds3ClientHelpers.Job job = helper.startWriteJob(bucketName, objects);
104103

105104
// Start the write job using an Object Putter that will read the files
106105
// from the local file system.
107-
job.write(new FileObjectPutter(inputPath));
106+
job.transfer(new FileObjectPutter(inputPath));
108107
}
109108
}
110109

111110
```
112111

113-
This next example is a little more complex and will perform a bulk get from a DS3 Appliance using the commands that directly correspond to the REST commands that are actually made against the DS3 Appliance. This example is meant to demonstrate the flexibilty of the SDK when writing applications where the helper functions cannot be used. The [Apache Commons IO](http://commons.apache.org/proper/commons-io/) library is used in this example for dealing with IO Streams.
112+
This next example is a little more complex and will perform a bulk get from a DS3 Appliance using the commands that directly correspond to the REST commands that are actually made against the DS3 Appliance. This example is meant to demonstrate the flexibilty of the SDK when writing applications where the helper functions cannot be used.
114113

115114
```java
116115

@@ -127,14 +126,12 @@ import com.spectralogic.ds3client.models.bulk.MasterObjectList;
127126
import com.spectralogic.ds3client.models.bulk.Objects;
128127
import com.spectralogic.ds3client.serializer.XmlProcessingException;
129128

130-
import org.apache.commons.io.IOUtils;
131-
132129
import java.io.IOException;
133-
import java.io.InputStream;
134-
import java.io.OutputStream;
130+
import java.nio.channels.FileChannel;
135131
import java.nio.file.FileSystems;
136132
import java.nio.file.Files;
137133
import java.nio.file.Path;
134+
import java.nio.file.StandardOpenOption;
138135
import java.security.SignatureException;
139136
import java.util.ArrayList;
140137
import java.util.List;
@@ -167,26 +164,27 @@ public class Ds3BulkGetExample {
167164
}
168165

169166
// Prime DS3 with the BulkGet command so that it can start to get objects off of tape.
170-
// All Response objects to the SDK implement the Closeable interface and can be used in try-with-resource blocks
171-
try (final BulkGetResponse bulkResponse = client.bulkGet(new BulkGetRequest(bucket, objectList))) {
172-
173-
// The bulk response returns a list of lists which is designed to optimize data transmission from DS3.
174-
final MasterObjectList list = bulkResponse.getResult();
175-
for (final Objects objects : list.getObjects()) {
176-
for (final BulkObject obj : objects) {
177-
178-
// Perform the operation to get the object from DS3.
179-
try (final GetObjectResponse getObjectResponse = client.getObject(
180-
new GetObjectRequest(bucket, obj.getName(), obj.getOffset(), list.getJobId()))) {
181-
182-
final Path filePath = dirPath.resolve(obj.getName());
183-
// Here we are using automatic resource cleanup to make sure the streams we use are cleaned up after use.
184-
try (final InputStream objStream = getObjectResponse.getContent();
185-
final OutputStream fileOut = Files.newOutputStream(filePath)) {
186-
IOUtils.copy(objStream, fileOut); //Using IOUtils to copy the object contents to a file.
187-
}
188-
}
189-
}
167+
final BulkGetResponse bulkResponse = client.bulkGet(new BulkGetRequest(bucket, objectList));
168+
169+
// The bulk response returns a list of lists which is designed to optimize data transmission from DS3.
170+
final MasterObjectList list = bulkResponse.getResult();
171+
for (final Objects objects : list.getObjects()) {
172+
for (final BulkObject obj : objects) {
173+
final FileChannel channel = FileChannel.open(
174+
dirPath.resolve(obj.getName()),
175+
StandardOpenOption.WRITE,
176+
StandardOpenOption.CREATE,
177+
StandardOpenOption.TRUNCATE_EXISTING
178+
);
179+
180+
// Perform the operation to get the object from DS3.
181+
client.getObject(new GetObjectRequest(
182+
bucket,
183+
obj.getName(),
184+
obj.getOffset(),
185+
list.getJobId(),
186+
channel
187+
));
190188
}
191189
}
192190
}

integration/src/main/java/com/spectralogic/ds3client/integration/ResourceObjectPutter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.nio.channels.SeekableByteChannel;
1111
import java.nio.file.StandardOpenOption;
1212

13-
public class ResourceObjectPutter implements Ds3ClientHelpers.ObjectTransferrer {
13+
public class ResourceObjectPutter implements Ds3ClientHelpers.ObjectChannelBuilder {
1414

1515
private final String basePath;
1616

integration/src/main/java/com/spectralogic/ds3client/integration/Util.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static void deleteAllContents(final Ds3Client client, final String bucket
7171

7272
final Iterable<Contents> objects = helpers.listObjects(bucketName);
7373
for(final Contents contents : objects) {
74-
client.deleteObject(new DeleteObjectRequest(bucketName, contents.getKey())).close();
74+
client.deleteObject(new DeleteObjectRequest(bucketName, contents.getKey()));
7575
}
7676

77-
client.deleteBucket(new DeleteBucketRequest(bucketName)).close();
77+
client.deleteBucket(new DeleteBucketRequest(bucketName));
7878
}
7979
}

integration/src/test/java/com/spectralogic/ds3client/integration/BucketIntegration_Test.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.spectralogic.ds3client.commands.*;
55
import com.spectralogic.ds3client.models.ListBucketResult;
66
import com.spectralogic.ds3client.serializer.XmlProcessingException;
7+
78
import org.junit.BeforeClass;
89
import org.junit.Test;
910

@@ -27,7 +28,7 @@ public static void startup() {
2728
@Test
2829
public void createBucket() throws IOException, SignatureException {
2930
final String bucketName = "test_create_bucket";
30-
client.putBucket(new PutBucketRequest(bucketName)).close();
31+
client.putBucket(new PutBucketRequest(bucketName));
3132

3233
HeadBucketResponse response = null;
3334
try {
@@ -36,43 +37,39 @@ public void createBucket() throws IOException, SignatureException {
3637
}
3738
finally {
3839
if (response != null) {
39-
response.close();
40-
client.deleteBucket(new DeleteBucketRequest(bucketName)).close();
40+
client.deleteBucket(new DeleteBucketRequest(bucketName));
4141
}
4242
}
4343
}
4444

4545
@Test
4646
public void deleteBucket() throws IOException, SignatureException {
4747
final String bucketName = "test_delete_bucket";
48-
client.putBucket(new PutBucketRequest(bucketName)).close();
48+
client.putBucket(new PutBucketRequest(bucketName));
4949

5050
HeadBucketResponse response = client.headBucket(new HeadBucketRequest(bucketName));
5151
assertThat(response.getStatus(), is(HeadBucketResponse.Status.EXISTS));
52-
response.close();
5352

54-
client.deleteBucket(new DeleteBucketRequest(bucketName)).close();
53+
client.deleteBucket(new DeleteBucketRequest(bucketName));
5554

5655
response = client.headBucket(new HeadBucketRequest(bucketName));
5756
assertThat(response.getStatus(), is(HeadBucketResponse.Status.DOESNTEXIST));
58-
response.close();
5957
}
6058

6159
@Test
6260
public void emptyBucket() throws IOException, SignatureException {
6361
final String bucketName = "test_empty_bucket";
6462

6563
try {
66-
client.putBucket(new PutBucketRequest(bucketName)).close();
64+
client.putBucket(new PutBucketRequest(bucketName));
6765

68-
try (final GetBucketResponse request = client.getBucket(new GetBucketRequest(bucketName))) {
69-
final ListBucketResult result = request.getResult();
70-
assertThat(result.getContentsList(), is(notNullValue()));
71-
assertTrue(result.getContentsList().isEmpty());
72-
}
66+
final GetBucketResponse request = client.getBucket(new GetBucketRequest(bucketName));
67+
final ListBucketResult result = request.getResult();
68+
assertThat(result.getContentsList(), is(notNullValue()));
69+
assertTrue(result.getContentsList().isEmpty());
7370
}
7471
finally {
75-
client.deleteBucket(new DeleteBucketRequest(bucketName)).close();
72+
client.deleteBucket(new DeleteBucketRequest(bucketName));
7673
}
7774
}
7875

@@ -81,7 +78,7 @@ public void listContents() throws IOException, SignatureException, XmlProcessing
8178
final String bucketName = "test_contents_bucket";
8279

8380
try {
84-
client.putBucket(new PutBucketRequest(bucketName)).close();
81+
client.putBucket(new PutBucketRequest(bucketName));
8582
Util.loadBookTestData(client, bucketName);
8683

8784
final GetBucketResponse response = client.getBucket(new GetBucketRequest(bucketName));
@@ -90,8 +87,6 @@ public void listContents() throws IOException, SignatureException, XmlProcessing
9087

9188
assertFalse(result.getContentsList().isEmpty());
9289
assertThat(result.getContentsList().size(), is(4));
93-
94-
response.close();
9590
}
9691
finally {
9792
Util.deleteAllContents(client, bucketName);

sdk/src/main/java/com/spectralogic/ds3client/commands/AbstractResponse.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.spectralogic.ds3client.networking.FailedRequestException;
2121
import com.spectralogic.ds3client.networking.WebResponse;
2222
import com.spectralogic.ds3client.serializer.XmlOutput;
23+
2324
import org.apache.commons.io.IOUtils;
2425

2526
import java.io.IOException;
@@ -92,11 +93,6 @@ private String readResponseString() throws IOException {
9293
}
9394
}
9495

95-
@Override
96-
public void close() throws IOException {
97-
this.response.close();
98-
}
99-
10096
public String getMd5() {
10197
return md5;
10298
}

sdk/src/main/java/com/spectralogic/ds3client/commands/AllocateJobChunkResponse.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,23 @@ public int getRetryAfterSeconds() {
5252

5353
@Override
5454
protected void processResponse() throws IOException {
55-
checkStatusCode(200, 503, 404);
56-
final WebResponse response = this.getResponse();
57-
switch (this.getStatusCode()) {
58-
case 200:
59-
this.status = Status.ALLOCATED;
60-
this.objects = parseChunk(response);
61-
break;
62-
case 503:
63-
this.status = Status.RETRYLATER;
64-
this.retryAfterSeconds = parseRetryAfter(response);
65-
break;
66-
case 404:
67-
this.status = Status.NOTFOUND;
68-
break;
69-
default:
70-
assert false : "checkStatusCode should have made it impossible to reach this line.";
55+
try (final WebResponse response = this.getResponse()) {
56+
checkStatusCode(200, 503, 404);
57+
switch (this.getStatusCode()) {
58+
case 200:
59+
this.status = Status.ALLOCATED;
60+
this.objects = parseChunk(response);
61+
break;
62+
case 503:
63+
this.status = Status.RETRYLATER;
64+
this.retryAfterSeconds = parseRetryAfter(response);
65+
break;
66+
case 404:
67+
this.status = Status.NOTFOUND;
68+
break;
69+
default:
70+
assert false : "checkStatusCode should have made it impossible to reach this line.";
71+
}
7172
}
7273
}
7374

sdk/src/main/java/com/spectralogic/ds3client/commands/BulkResponse.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.spectralogic.ds3client.models.bulk.MasterObjectList;
1919
import com.spectralogic.ds3client.networking.WebResponse;
2020
import com.spectralogic.ds3client.serializer.XmlOutput;
21+
2122
import org.apache.commons.io.IOUtils;
2223

2324
import java.io.IOException;
@@ -36,13 +37,13 @@ public MasterObjectList getResult() {
3637

3738
@Override
3839
protected void processResponse() throws IOException {
39-
this.checkStatusCode(200);
40-
try(final StringWriter writer = new StringWriter();
41-
final InputStream content = this.getResponse().getResponseStream()) {
42-
43-
IOUtils.copy(content, writer, UTF8);
44-
45-
this.result = XmlOutput.fromXml(writer.toString(), MasterObjectList.class);
40+
try (final WebResponse response = this.getResponse()) {
41+
this.checkStatusCode(200);
42+
try(final StringWriter writer = new StringWriter();
43+
final InputStream content = response.getResponseStream()) {
44+
IOUtils.copy(content, writer, UTF8);
45+
this.result = XmlOutput.fromXml(writer.toString(), MasterObjectList.class);
46+
}
4647
}
4748
}
4849
}

sdk/src/main/java/com/spectralogic/ds3client/commands/CancelJobResponse.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public CancelJobResponse(final WebResponse response) throws IOException {
2626

2727
@Override
2828
protected void processResponse() throws IOException {
29-
checkStatusCode(204);
29+
try {
30+
checkStatusCode(204);
31+
} finally {
32+
this.getResponse().close();
33+
}
3034
}
3135
}

sdk/src/main/java/com/spectralogic/ds3client/commands/DeleteBucketResponse.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public DeleteBucketResponse(final WebResponse response) throws IOException {
2626

2727
@Override
2828
protected void processResponse() throws IOException {
29-
this.checkStatusCode(204);
29+
try {
30+
this.checkStatusCode(204);
31+
} finally {
32+
this.getResponse().close();
33+
}
3034
}
3135
}

sdk/src/main/java/com/spectralogic/ds3client/commands/DeleteObjectResponse.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public DeleteObjectResponse(final WebResponse response) throws IOException {
2626

2727
@Override
2828
protected void processResponse() throws IOException {
29-
this.checkStatusCode(204);
29+
try {
30+
this.checkStatusCode(204);
31+
} finally {
32+
this.getResponse().close();
33+
}
3034
}
3135
}

0 commit comments

Comments
 (0)