Skip to content

Commit f191563

Browse files
RachelTuckerrpmoore
authored andcommitted
JSDK-297: Add new Header value to HEAD Object. (#577)
* JSDK-297: Add new Header value to HEAD Object. Adding in parsing for new headers 'creation-date' and 'version-id' into the head object response payload. * JSDK-297: Add new Header value to HEAD Object. Generated head object call with new header parsing.
1 parent bbfd3a9 commit f191563

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.spectralogic.ds3client.networking.Metadata;
2121
import com.spectralogic.ds3client.models.ChecksumType;
2222
import com.google.common.collect.ImmutableMap;
23+
import java.time.ZonedDateTime;
24+
import java.util.UUID;
2325

2426
public class HeadObjectResponse extends AbstractResponse {
2527

@@ -29,19 +31,25 @@ public enum Status { EXISTS, DOESNTEXIST, UNKNOWN }
2931

3032
private final ChecksumType.Type blobChecksumType;
3133

34+
private final ZonedDateTime creationDate;
35+
3236
private final Metadata metadata;
3337

3438
private final long objectSize;
3539

3640
private final Status status;
3741

38-
public HeadObjectResponse(final ImmutableMap<Long, String> blobChecksums, final ChecksumType.Type blobChecksumType, final Metadata metadata, final long objectSize, final Status status, final String checksum, final ChecksumType.Type checksumType) {
42+
private final UUID versionId;
43+
44+
public HeadObjectResponse(final ImmutableMap<Long, String> blobChecksums, final ChecksumType.Type blobChecksumType, final ZonedDateTime creationDate, final Metadata metadata, final long objectSize, final Status status, final UUID versionId, final String checksum, final ChecksumType.Type checksumType) {
3945
super(checksum, checksumType);
4046
this.blobChecksums = blobChecksums;
4147
this.blobChecksumType = blobChecksumType;
48+
this.creationDate = creationDate;
4249
this.metadata = metadata;
4350
this.objectSize = objectSize;
4451
this.status = status;
52+
this.versionId = versionId;
4553
}
4654

4755
public ImmutableMap<Long, String> getBlobChecksums() {
@@ -52,6 +60,10 @@ public ChecksumType.Type getBlobChecksumType() {
5260
return this.blobChecksumType;
5361
}
5462

63+
public ZonedDateTime getCreationDate() {
64+
return this.creationDate;
65+
}
66+
5567
public Metadata getMetadata() {
5668
return this.metadata;
5769
}
@@ -64,4 +76,8 @@ public Status getStatus() {
6476
return this.status;
6577
}
6678

79+
public UUID getVersionId() {
80+
return this.versionId;
81+
}
82+
6783
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/commands/parsers/HeadObjectResponseParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.spectralogic.ds3client.networking.Metadata;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.spectralogic.ds3client.models.ChecksumType;
23+
import java.time.ZonedDateTime;
24+
import java.util.UUID;
2325
import com.spectralogic.ds3client.commands.HeadObjectResponse;
2426
import com.spectralogic.ds3client.commands.parsers.interfaces.AbstractResponseParser;
2527
import com.spectralogic.ds3client.commands.parsers.utils.ResponseParserUtils;
@@ -41,10 +43,12 @@ public HeadObjectResponse parseXmlResponse(final WebResponse response) throws IO
4143
case 200:
4244
final ChecksumType.Type blobChecksumType = getBlobChecksumType(response.getHeaders());
4345
final ImmutableMap<Long, String> blobChecksumMap = getBlobChecksumMap(response.getHeaders());
44-
return new HeadObjectResponse(blobChecksumMap, blobChecksumType, metadata, objectSize, HeadObjectResponse.Status.EXISTS, this.getChecksum(), this.getChecksumType());
46+
final ZonedDateTime creationDate = getCreationDate(response.getHeaders());
47+
final UUID versionId = getVersionId(response.getHeaders());
48+
return new HeadObjectResponse(blobChecksumMap, blobChecksumType, creationDate, metadata, objectSize, HeadObjectResponse.Status.EXISTS, versionId, this.getChecksum(), this.getChecksumType());
4549

4650
case 404:
47-
return new HeadObjectResponse(ImmutableMap.of(), ChecksumType.Type.NONE, metadata, objectSize, HeadObjectResponse.Status.DOESNTEXIST, this.getChecksum(), this.getChecksumType());
51+
return new HeadObjectResponse(ImmutableMap.of(), ChecksumType.Type.NONE, null, metadata, objectSize, HeadObjectResponse.Status.DOESNTEXIST, null, this.getChecksum(), this.getChecksumType());
4852

4953
default:
5054
assert false: "validateStatusCode should have made it impossible to reach this line";

ds3-sdk/src/main/java/com/spectralogic/ds3client/commands/parsers/utils/ResponseParserUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import java.io.IOException;
3636
import java.io.InputStream;
3737
import java.io.StringWriter;
38+
import java.time.ZonedDateTime;
3839
import java.util.List;
40+
import java.util.UUID;
3941

4042
public final class ResponseParserUtils {
4143

@@ -44,6 +46,8 @@ public final class ResponseParserUtils {
4446

4547
private static final String BLOB_CHECKSUM_TYPE_HEADER = "ds3-blob-checksum-type";
4648
private static final String BLOB_CHECKSUM_HEADER = "ds3-blob-checksum-offset-";
49+
private static final String VERSION_ID_HEADER = "version-id";
50+
private static final String CREATION_DATE_HEADER = "creation-date";
4751

4852
private ResponseParserUtils() {
4953
// pass
@@ -150,6 +154,42 @@ private static String readResponseString(final WebResponse response) throws IOEx
150154
}
151155
}
152156

157+
/**
158+
* Retrieves the version id from the response headers. If there is no version id header,
159+
* then null is returned. This is used in HeadObject response parsing.
160+
* @param headers The http response headers from the BP.
161+
* @return The version id if one exists.
162+
*/
163+
public static UUID getVersionId(final Headers headers) {
164+
final List<String> versions = headers.get(VERSION_ID_HEADER);
165+
switch (versions.size()) {
166+
case 0:
167+
return null;
168+
case 1:
169+
return UUID.fromString(versions.get(0));
170+
default:
171+
throw new IllegalArgumentException(String.format("Response has more than one header value for '%s'", VERSION_ID_HEADER));
172+
}
173+
}
174+
175+
/**
176+
* Retrieves the creation date from the response headers. If there is no creation date header,
177+
* then null is returned. This is used in HeadObject response parsing.
178+
* @param headers The http response headers from the BP.
179+
* @return The creation date if one exists.
180+
*/
181+
public static ZonedDateTime getCreationDate(final Headers headers) {
182+
final List<String> dates = headers.get(CREATION_DATE_HEADER);
183+
switch (dates.size()) {
184+
case 0:
185+
return null;
186+
case 1:
187+
return ZonedDateTime.parse(dates.get(0));
188+
default:
189+
throw new IllegalArgumentException(String.format("Response has more than one header value for '%s'", CREATION_DATE_HEADER));
190+
}
191+
}
192+
153193
/**
154194
* Retrieves the blob checksum type from the response headers. If there is no blob checksum
155195
* type header, than NONE is returned. If there is more than one value for the blob checksum

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.nio.file.StandardOpenOption;
4646
import java.text.ParseException;
4747
import java.text.SimpleDateFormat;
48+
import java.time.Month;
49+
import java.time.ZoneId;
4850
import java.util.*;
4951
import java.util.regex.Matcher;
5052
import java.util.regex.Pattern;
@@ -480,13 +482,16 @@ public void createObjectWithNullAndEmptyMetadata() throws IOException, URISyntax
480482

481483
@Test
482484
public void headObjectWithMetadata() throws IOException {
485+
final UUID versionId = UUID.randomUUID();
483486

484487
final Map<String, String> responseHeaders = new HashMap<>();
485488
responseHeaders.put("x-amz-meta-key", "value");
486489
responseHeaders.put("ds3-blob-checksum-type", "MD5");
487490
responseHeaders.put("ds3-blob-checksum-offset-0", "4nQGNX4nyz0pi8Hvap79PQ==");
488491
responseHeaders.put("ds3-blob-checksum-offset-10485760", "965Aa0/n8DlO1IwXYFh4bg==");
489492
responseHeaders.put("ds3-blob-checksum-offset-20971520", "iV2OqJaXJ/jmqgRSb1HmFA==");
493+
responseHeaders.put("creation-date", "2019-07-11T20:35:47.000Z");
494+
responseHeaders.put("version-id", versionId.toString());
490495

491496

492497
final HeadObjectRequest request = new HeadObjectRequest("bucket", "obj");
@@ -510,6 +515,15 @@ public void headObjectWithMetadata() throws IOException {
510515
assertTrue(response.getBlobChecksums().containsKey(entry.getKey()));
511516
assertThat(response.getBlobChecksums().get(entry.getKey()), is(entry.getValue()));
512517
}
518+
519+
assertThat(response.getVersionId(), is(versionId));
520+
assertThat(response.getCreationDate().getYear(), is(2019));
521+
assertThat(response.getCreationDate().getMonth(), is(Month.JULY));
522+
assertThat(response.getCreationDate().getDayOfMonth(), is(11));
523+
assertThat(response.getCreationDate().getHour(), is(20));
524+
assertThat(response.getCreationDate().getMinute(), is(35));
525+
assertThat(response.getCreationDate().getSecond(), is(47));
526+
assertThat(response.getCreationDate().getZone(), is(ZoneId.of("Z")));
513527
}
514528

515529
@Test

ds3-sdk/src/test/java/com/spectralogic/ds3client/commands/parsers/utils/ResponseParserUtils_Test.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
import com.google.common.collect.ImmutableMap;
1919
import com.spectralogic.ds3client.MockedHeaders;
2020
import com.spectralogic.ds3client.models.ChecksumType;
21-
import com.spectralogic.ds3client.networking.Headers;
2221
import org.junit.Test;
2322
import org.slf4j.Logger;
2423
import org.slf4j.LoggerFactory;
2524

25+
import java.time.Month;
26+
import java.time.ZoneId;
27+
import java.time.ZonedDateTime;
2628
import java.util.Map;
29+
import java.util.UUID;
2730

2831
import static com.spectralogic.ds3client.commands.parsers.utils.ResponseParserUtils.*;
2932
import static org.hamcrest.CoreMatchers.is;
@@ -125,4 +128,35 @@ public void getBlobChecksumTypeTest() {
125128
final ChecksumType.Type result = getBlobChecksumType(new MockedHeaders(TEST_RESPONSE_HEADERS));
126129
assertThat(result, is(ChecksumType.Type.MD5));
127130
}
131+
132+
@Test
133+
public void getCreationDateTest() {
134+
final ImmutableMap<String, String> headers = ImmutableMap.of(
135+
"creation-date", "2019-07-11T20:35:47.000Z",
136+
"version-id", "eec64ea6-8434-492f-a068-ef516da801a3"
137+
);
138+
139+
final ZonedDateTime result = getCreationDate(new MockedHeaders(headers));
140+
141+
assertThat(result.getYear(), is(2019));
142+
assertThat(result.getMonth(), is(Month.JULY));
143+
assertThat(result.getDayOfMonth(), is(11));
144+
assertThat(result.getHour(), is(20));
145+
assertThat(result.getMinute(), is(35));
146+
assertThat(result.getSecond(), is(47));
147+
assertThat(result.getZone(), is(ZoneId.of("Z")));
148+
}
149+
150+
@Test
151+
public void getVersionIdTest() {
152+
final UUID versionId = UUID.fromString("eec64ea6-8434-492f-a068-ef516da801a3");
153+
154+
final ImmutableMap<String, String> headers = ImmutableMap.of(
155+
"creation-date", "2019-07-11T20:35:47.000Z",
156+
"version-id", versionId.toString()
157+
);
158+
159+
final UUID result = getVersionId(new MockedHeaders(headers));
160+
assertThat(result, is(versionId));
161+
}
128162
}

0 commit comments

Comments
 (0)