Skip to content

Commit

Permalink
Implement Args builder for getObject API (#954)
Browse files Browse the repository at this point in the history
* Implement Args builder for getObject API

New Argument class: `GetObjectArgs`

Arguments supported:
 - offset (Long)
 - length (Long)
 - ssec (ServerSideEncryptionWithCustomerKey)

Since there are multiple APIs / args classes that need sse as an
argument, introduced an intermediate abstract class `SsecObjectArgs`
that supports the `ssec` argument, and provides common logic related to
it e.g. validations.

Since the return type of the methods that support downloading the
object as a file is `void`, we cannot name it same as `getObject`. So
introduced a new method called `downloadObject` for downloading the
object, which internally uses `getObject`. And created a separate args
class `DownloadObjectArgs` for it.

Also,

- Enhanced the method `FunctionalTest#handleException` to support
logging of the arguments as well.
- Moved the class `ServerSideEncryptionWithCustomerKey` outside of
`ServerSideEncryption` and made it into a standalone class.

* Incorporate review comments

- Change `sse` to `ssec` in javadoc comments on stateObject
- Remove unused static variables from `SsecObjectArgs`
  • Loading branch information
anjalshireesh authored May 29, 2020
1 parent 19b2011 commit 928b31b
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 523 deletions.
14 changes: 7 additions & 7 deletions api/src/main/java/io/minio/ComposeSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ComposeSource {
private Long length;
private Map<String, String> headerMap;
private CopyConditions copyConditions;
private ServerSideEncryption sse;
private ServerSideEncryptionCustomerKey ssec;
private long objectSize;
private Map<String, String> headers;

Expand Down Expand Up @@ -79,7 +79,7 @@ public ComposeSource(
Long length,
Map<String, String> headerMap,
CopyConditions copyConditions,
ServerSideEncryption sse)
ServerSideEncryptionCustomerKey ssec)
throws IllegalArgumentException {
if (bucketName == null) {
throw new IllegalArgumentException("Source bucket name cannot be empty");
Expand Down Expand Up @@ -111,7 +111,7 @@ public ComposeSource(
this.headerMap = null;
}
this.copyConditions = copyConditions;
this.sse = sse;
this.ssec = ssec;
}

/** Constructs header . */
Expand Down Expand Up @@ -166,8 +166,8 @@ public void buildHeaders(long objectSize, String etag) throws IllegalArgumentExc
headers.putAll(copyConditions.getConditions());
}

if (sse != null) {
headers.putAll(sse.copySourceHeaders());
if (ssec != null) {
headers.putAll(ssec.copySourceHeaders());
}

this.objectSize = objectSize;
Expand All @@ -194,8 +194,8 @@ public CopyConditions copyConditions() {
return copyConditions;
}

public ServerSideEncryption sse() {
return sse;
public ServerSideEncryptionCustomerKey ssec() {
return ssec;
}

public Map<String, String> headers() {
Expand Down
58 changes: 58 additions & 0 deletions api/src/main/java/io/minio/DownloadObjectArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class DownloadObjectArgs extends SsecObjectArgs {
private String fileName;

public String fileName() {
return fileName;
}

public static Builder builder() {
return new Builder();
}

public static final class Builder extends SsecObjectArgs.Builder<Builder, DownloadObjectArgs> {
public Builder fileName(String fileName) {
validateFileName(fileName);
operations.add(args -> args.fileName = fileName);
return this;
}

private void validateFileName(String fileName) {
if (fileName == null) {
return;
}

if (fileName.isEmpty()) {
throw new IllegalArgumentException("filename should be either null or non-empty");
}

Path filePath = Paths.get(fileName);
boolean fileExists = Files.exists(filePath);

if (fileExists && !Files.isRegularFile(filePath)) {
throw new IllegalArgumentException(fileName + ": not a regular file");
}
}
}
}
60 changes: 60 additions & 0 deletions api/src/main/java/io/minio/GetObjectArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio;

public class GetObjectArgs extends SsecObjectArgs {
private Long offset;
private Long length;

public Long length() {
return length;
}

public Long offset() {
return offset;
}

public static Builder builder() {
return new Builder();
}

public static final class Builder extends SsecObjectArgs.Builder<Builder, GetObjectArgs> {
public Builder offset(Long offset) {
validateOffset(offset);
operations.add(args -> args.offset = offset);
return this;
}

public Builder length(Long length) {
validateLength(length);
operations.add(args -> args.length = length);
return this;
}

private void validateLength(Long length) {
if (length != null && length <= 0) {
throw new IllegalArgumentException("length should be greater than zero");
}
}

private void validateOffset(Long offset) {
if (offset != null && offset < 0) {
throw new IllegalArgumentException("offset should be zero or greater");
}
}
}
}
Loading

0 comments on commit 928b31b

Please sign in to comment.