Skip to content

Commit

Permalink
add arg builder to {set,get,delete}BucketLifeCycle APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed May 26, 2020
1 parent 411dfff commit 39e8464
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 80 deletions.
28 changes: 28 additions & 0 deletions api/src/main/java/io/minio/DeleteBucketLifeCycleArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

/** Argument class of MinioClient.deleteBucketLifeCycle(). */
public class DeleteBucketLifeCycleArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link DeleteBucketLifeCycleArgs}. */
public static final class Builder
extends BucketArgs.Builder<Builder, DeleteBucketLifeCycleArgs> {}
}
27 changes: 27 additions & 0 deletions api/src/main/java/io/minio/GetBucketLifeCycleArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

/** Argument class of MinioClient.getBucketLifeCycle(). */
public class GetBucketLifeCycleArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link GetBucketLifeCycleArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, GetBucketLifeCycleArgs> {}
}
145 changes: 124 additions & 21 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4400,7 +4400,7 @@ public void setBucketPolicy(String bucketName, String policy)
}

/**
* Sets life cycle configuration to a bucket.
* Sets life-cycle configuration to a bucket.
*
* <pre>Example:{@code
* // Lets consider variable 'lifeCycleXml' contains below XML String;
Expand All @@ -4415,7 +4415,7 @@ public void setBucketPolicy(String bucketName, String policy)
* // </Rule>
* // </LifecycleConfiguration>
* //
* minioClient.setBucketLifecycle("my-bucketname", lifeCycleXml);
* minioClient.setBucketLifeCycle("my-bucketname", lifeCycleXml);
* }</pre>
*
* @param bucketName Name of the bucket.
Expand All @@ -4432,21 +4432,64 @@ public void setBucketPolicy(String bucketName, String policy)
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public void setBucketLifeCycle(String bucketName, String lifeCycle)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if ((lifeCycle == null) || "".equals(lifeCycle)) {
throw new IllegalArgumentException("life cycle cannot be empty");
setBucketLifeCycle(
SetBucketLifeCycleArgs.builder().bucket(bucketName).config(lifeCycle).build());
}

/**
* Sets life-cycle configuration to a bucket.
*
* <pre>Example:{@code
* // Lets consider variable 'lifeCycleXml' contains below XML String;
* // <LifecycleConfiguration>
* // <Rule>
* // <ID>expire-bucket</ID>
* // <Prefix></Prefix>
* // <Status>Enabled</Status>
* // <Expiration>
* // <Days>365</Days>
* // </Expiration>
* // </Rule>
* // </LifecycleConfiguration>
* //
* minioClient.setBucketLifeCycle(
* SetBucketLifeCycleArgs.builder().bucket("my-bucketname").config(lifeCycleXml).build());
* }</pre>
*
* @param args {@link SetBucketLifeCycleArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public void setBucketLifeCycle(SetBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
Response response = executePut(bucketName, null, null, queryParamMap, lifeCycle, 0);
response.body().close();
Response response = executePut(args.bucket(), null, null, queryParamMap, args.config(), 0);
response.close();
}

/**
* Deletes life cycle configuration of a bucket.
* Deletes life-cycle configuration of a bucket.
*
* <pre>Example:{@code
* deleteBucketLifeCycle("my-bucketname");
Expand All @@ -4465,21 +4508,53 @@ public void setBucketLifeCycle(String bucketName, String lifeCycle)
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public void deleteBucketLifeCycle(String bucketName)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
deleteBucketLifeCycle(DeleteBucketLifeCycleArgs.builder().bucket(bucketName).build());
}

/**
* Deletes life-cycle configuration of a bucket.
*
* <pre>Example:{@code
* deleteBucketLifeCycle(DeleteBucketLifeCycleArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link DeleteBucketLifeCycleArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public void deleteBucketLifeCycle(DeleteBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
Response response = executeDelete(bucketName, "", queryParamMap);
response.body().close();
Response response = executeDelete(args.bucket(), "", queryParamMap);
response.close();
}

/**
* Gets life cycle configuration of a bucket.
* Gets life-cycle configuration of a bucket.
*
* <pre>Example:{@code
* String lifecycle = minioClient.getBucketLifecycle("my-bucketname");
* String lifecycle = minioClient.getBucketLifeCycle("my-bucketname");
* }</pre>
*
* @param bucketName Name of the bucket.
Expand All @@ -4496,28 +4571,56 @@ public void deleteBucketLifeCycle(String bucketName)
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public String getBucketLifeCycle(String bucketName)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
return getBucketLifeCycle(GetBucketLifeCycleArgs.builder().bucket(bucketName).build());
}

/**
* Gets life-cycle configuration of a bucket.
*
* <pre>Example:{@code
* String lifecycle =
* minioClient.getBucketLifeCycle(
* GetBucketLifeCycleArgs.builder().bucket("my-bucketname").build());
* }</pre>
*
* @param args {@link GetBucketLifeCycleArgs} object.
* @return String - Life cycle configuration as XML string.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public String getBucketLifeCycle(GetBucketLifeCycleArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("lifecycle", "");
String bodyContent = "";
Response response = null;
try {
response = executeGet(bucketName, null, null, queryParamMap);
bodyContent = new String(response.body().bytes(), StandardCharsets.UTF_8);
try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) {
return new String(response.body().bytes(), StandardCharsets.UTF_8);
} catch (ErrorResponseException e) {
if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_LIFECYCLE_CONFIGURATION) {
throw e;
}
} finally {
if (response != null) {
response.body().close();
}
}

return bodyContent;
return "";
}

/**
Expand Down
50 changes: 50 additions & 0 deletions api/src/main/java/io/minio/SetBucketLifeCycleArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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;

/** Argument class of MinioClient.setBucketLifeCycle(). */
public class SetBucketLifeCycleArgs extends BucketArgs {
private String config;

public String config() {
return config;
}

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

/** Argument builder of {@link SetBucketLifeCycleArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, SetBucketLifeCycleArgs> {
private void validateConfig(String config) {
if (config == null || config.isEmpty()) {
throw new IllegalArgumentException("empty life-cycle configuration");
}
}

protected void validate(SetBucketLifeCycleArgs args) {
super.validate(args);
validateConfig(args.config);
}

public Builder config(String config) {
validateConfig(config);
operations.add(args -> args.config = config);
return this;
}
}
}
Loading

0 comments on commit 39e8464

Please sign in to comment.