Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,118 @@ public void testPutObjectIfMatchMissingKeyFail() {
b -> b.bucket(bucketName).key(keyName)));
}

@Test
public void testGetObjectIfMatch() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));

PutObjectResponse initialResponse = s3Client.putObject(
b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

ResponseBytes<GetObjectResponse> response = s3Client.getObjectAsBytes(
b -> b.bucket(bucketName).key(keyName).ifMatch(initialResponse.eTag()));

assertEquals(content, response.asUtf8String());
}

@Test
public void testGetObjectIfMatchFail() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));
s3Client.putObject(b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

S3Exception exception = assertThrows(S3Exception.class,
() -> s3Client.getObjectAsBytes(
b -> b.bucket(bucketName).key(keyName).ifMatch("wrong-etag")));

assertEquals(412, exception.statusCode());
assertEquals("PreconditionFailed", exception.awsErrorDetails().errorCode());
}

@Test
public void testGetObjectIfNoneMatchReturnsNotModified() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));

PutObjectResponse initialResponse = s3Client.putObject(
b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

S3Exception exception = assertThrows(S3Exception.class,
() -> s3Client.getObjectAsBytes(
b -> b.bucket(bucketName).key(keyName)
.ifNoneMatch(initialResponse.eTag())));

assertEquals(304, exception.statusCode());
}

@Test
public void testGetObjectIfModifiedSinceReturnsNotModified() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));
s3Client.putObject(b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

HeadObjectResponse headObjectResponse = s3Client.headObject(
b -> b.bucket(bucketName).key(keyName));

S3Exception exception = assertThrows(S3Exception.class,
() -> s3Client.getObjectAsBytes(
b -> b.bucket(bucketName).key(keyName)
.ifModifiedSince(headObjectResponse.lastModified()
.plusSeconds(60))));

assertEquals(304, exception.statusCode());
}

@Test
public void testHeadObjectIfMatch() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));

PutObjectResponse initialResponse = s3Client.putObject(
b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

HeadObjectResponse response = s3Client.headObject(
b -> b.bucket(bucketName).key(keyName).ifMatch(initialResponse.eTag()));

assertEquals(Long.valueOf(content.length()), response.contentLength());
}

@Test
public void testHeadObjectIfUnmodifiedSinceFail() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "bar";
s3Client.createBucket(b -> b.bucket(bucketName));
s3Client.putObject(b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

HeadObjectResponse headObjectResponse = s3Client.headObject(
b -> b.bucket(bucketName).key(keyName));

S3Exception exception = assertThrows(S3Exception.class,
() -> s3Client.headObject(
b -> b.bucket(bucketName).key(keyName)
.ifUnmodifiedSince(headObjectResponse.lastModified()
.minusSeconds(60))));

assertEquals(412, exception.statusCode());
}

@Test
public void testPutObjectEmpty() throws Exception {
final String bucketName = getBucketName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -61,7 +60,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -107,7 +105,6 @@
import org.apache.hadoop.ozone.s3.signature.SignatureInfo;
import org.apache.hadoop.ozone.s3.util.AuditUtils;
import org.apache.hadoop.ozone.s3.util.S3Utils;
import org.apache.hadoop.ozone.web.utils.OzoneUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -471,7 +468,7 @@ protected AuditMessage.Builder auditMessageFor(AuditAction op) {
Map<String, String> auditMap = getAuditParameters();
auditMap.put("x-amz-request-id", requestIdentifier.getRequestId());
auditMap.put("x-amz-id-2", requestIdentifier.getAmzId());

AuditMessage.Builder builder = new AuditMessage.Builder()
.forOperation(op)
.withParams(auditMap);
Expand Down Expand Up @@ -649,52 +646,6 @@ protected static int parsePartNumberMarker(String partNumberMarker) {
return partMarker;
}

// Parses date string and return long representation. Returns an
// empty if DateStr is null or invalid. Dates in the future are
// considered invalid.
private static OptionalLong parseAndValidateDate(String ozoneDateStr) {
long ozoneDateInMs;
if (ozoneDateStr == null) {
return OptionalLong.empty();
}
try {
ozoneDateInMs = OzoneUtils.formatDate(ozoneDateStr);
} catch (ParseException e) {
// if time not parseable, then return empty()
return OptionalLong.empty();
}

long currentDate = System.currentTimeMillis();
if (ozoneDateInMs <= currentDate) {
return OptionalLong.of(ozoneDateInMs);
} else {
// dates in the future are invalid, so return empty()
return OptionalLong.empty();
}
}

public static boolean checkCopySourceModificationTime(
Long lastModificationTime,
String copySourceIfModifiedSinceStr,
String copySourceIfUnmodifiedSinceStr) {
long copySourceIfModifiedSince = Long.MIN_VALUE;
long copySourceIfUnmodifiedSince = Long.MAX_VALUE;

OptionalLong modifiedDate =
parseAndValidateDate(copySourceIfModifiedSinceStr);
if (modifiedDate.isPresent()) {
copySourceIfModifiedSince = modifiedDate.getAsLong();
}

OptionalLong unmodifiedDate =
parseAndValidateDate(copySourceIfUnmodifiedSinceStr);
if (unmodifiedDate.isPresent()) {
copySourceIfUnmodifiedSince = unmodifiedDate.getAsLong();
}
return (copySourceIfModifiedSince <= lastModificationTime) &&
(lastModificationTime <= copySourceIfUnmodifiedSince);
}

/**
* Create a {@link S3ChunkInputStreamInfo} that contains the necessary information to handle
* the S3 chunk upload.
Expand Down
Loading