Skip to content

Commit

Permalink
Add new method to set/get object retention
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish committed Jan 27, 2020
1 parent 8166510 commit b5c1c80
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/io/minio/DateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ public class DateFormat {
public static final DateTimeFormatter HTTP_HEADER_DATE_FORMAT =
DateTimeFormat.forPattern("EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'").withZoneUTC().withLocale(Locale.US);

public static final DateTimeFormatter RETENTION_DATE_FORMAT =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH':'mm':'ss'Z'").withZoneUTC().withLocale(Locale.US);

private DateFormat() {}
}
104 changes: 104 additions & 0 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
import io.minio.messages.SelectObjectContentRequest;
import io.minio.org.apache.commons.validator.routines.InetAddressValidator;

import io.minio.messages.ObjectLockRetention;
import io.minio.messages.ObjectLockRetentionConfiguration;

import io.minio.notification.NotificationInfo;

import okhttp3.HttpUrl;
Expand Down Expand Up @@ -3895,6 +3898,107 @@ public ObjectLockConfiguration getDefaultRetention(String bucketName)
}


/**
* Applies object retention lock onto an object.
*
* </p><b>Example:</b><br>
* <pre>{@code minioClient.setObjectRetention("my-bucketname", "my-object", config );
* System.out.println("Set object retention on my-object successfully."); }</pre>
*
* @param bucketName Bucket name.
* @param objectName Object name.
* @param config Object lock configuration.
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoSuchAlgorithmException
* upon requested algorithm was not found during signature calculation
* @throws InsufficientDataException upon getting EOFException while reading given
* InputStream even before reading given length
* @throws IOException upon connection error
* @throws InvalidKeyException
* upon an invalid access key or secret key
* @throws NoResponseException upon no response from server
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
* @throws InvalidResponseException upon a non-xml response from server
*/
public void setObjectLockRetention(String bucketName, String objectName, ObjectLockRetentionConfiguration config)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException, InvalidResponseException {

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("retention", "");

if (config.version() != null || !("").equals(config.version())) {
queryParamMap.put("versionId", config.version());
}

// Set Headers
Map<String, String> headerMap = new HashMap<>();
if ( config.governanceBypass()) {
headerMap.put("x-amz-bypass-governance-retention", "True");
}

ObjectLockRetention objectLockRetention = new ObjectLockRetention(config.mode(),
config.retainUntil().toString((DateFormat.RETENTION_DATE_FORMAT)));

HttpResponse response = executePut(bucketName, objectName, headerMap, queryParamMap, objectLockRetention, 0);
response.body().close();
}

/**
* Fetches object retention lock of an object.
*
* </p><b>Example:</b><br>
* <pre>{@code
* ObjectLockRetention objectLockRetention = minioClient.getObjectRetention("my-bucketname",
* "my-object", "version-Id" );
* System.out.println("Mode " + objectLockRetention.mode());
* System.out.println("Retanetion Until " + objectLockRetention.getRetentionDate()); }</pre>
*
* @param bucketName Bucket name.
* @param objectName Object name.
* @param versionId Version Id.
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoSuchAlgorithmException
* upon requested algorithm was not found during signature calculation
* @throws InsufficientDataException upon getting EOFException while reading given
* InputStream even before reading given length
* @throws IOException upon connection error
* @throws InvalidKeyException
* upon an invalid access key or secret key
* @throws NoResponseException upon no response from server
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
* @throws InvalidResponseException upon a non-xml response from server
*/
public ObjectLockRetention getObjectLockRetention(String bucketName, String objectName, String versionId)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException, InvalidResponseException {

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("retention", "");

if ( !(versionId == null || versionId.isEmpty())) {
queryParamMap.put("versionId",versionId);
}

HttpResponse response = executeGet(bucketName, objectName, null, queryParamMap);
ObjectLockRetention result = new ObjectLockRetention();
try {
result.parseXml(response.body().charStream());
} finally {
response.body().close();
}
return result;
}


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

import org.xmlpull.v1.XmlPullParserException;

import io.minio.DateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import com.google.api.client.util.Key;

/**
* Helper class to parse Amazon AWS S3 response XML containing ObjectLockRetention information.
*/
@SuppressWarnings("SameParameterValue")
public class ObjectLockRetention extends XmlEntity {
@Key("Mode")
private String mode;
@Key("RetainUntilDate")
private String retainUntilDate;

/**
* Constructs a new ObjectLockRetention object.
*/
public ObjectLockRetention() throws XmlPullParserException {
super();
super.name = "Retention";
}

/**
* Constructs a new CustomRetention object with given retention.
*/
public ObjectLockRetention(RetentionMode mode, String retainUntilDate) throws XmlPullParserException {
super();
super.name = "Retention";
if (mode != null) {
this.mode = mode.toString();
}
if (retainUntilDate != null ) {
this.retainUntilDate = retainUntilDate;
}
}

/**
* Returns mode.
*/
public RetentionMode mode() {
return RetentionMode.fromString(mode);
}

/**
* Returns retain until date.
*/
public DateTime retainUntil() {
return DateFormat.RETENTION_DATE_FORMAT.parseDateTime(retainUntilDate);
}

/**
* Returns retain until date.
*/
public Date getRetentionDate() {
return DateFormat.RETENTION_DATE_FORMAT.parseDateTime(retainUntilDate).toDate();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.messages;

import org.xmlpull.v1.XmlPullParserException;
import io.minio.DateFormat;
import org.joda.time.DateTime;
import com.google.api.client.util.Key;


/**
* Helper class to construct create bucket configuration request XML for Amazon AWS S3.
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "URF_UNREAD_FIELD")
public class ObjectLockRetentionConfiguration extends XmlEntity {
@Key("ObjectRetention")
private ObjectLockRetention objectLockRetention;
@Key("BypassGovernanceRetention")
private boolean bypassGovernanceRetention;
@Key("Version")
private String versionId;

/**
* Constructs a new ObjectLockRetentionConfiguration object with given input parameters.
*/
public ObjectLockRetentionConfiguration(RetentionMode mode, boolean bypassGovernanceRetention,
DateTime retainUntilDate, String versionId) throws XmlPullParserException {
super();
this.bypassGovernanceRetention = bypassGovernanceRetention;
this.versionId = versionId ;
this.objectLockRetention = new ObjectLockRetention(mode,
retainUntilDate.toString(DateFormat.RETENTION_DATE_FORMAT));
}


/**
* Returns mode.
*/
public RetentionMode mode() {
if (objectLockRetention == null) {
return null;
}
return objectLockRetention.mode();
}


/**
* Returns retantion untill date.
*/
public DateTime retainUntil() {
if (objectLockRetention == null) {
return null;
}
return objectLockRetention.retainUntil();
}

/**
* Returns version.
*/
public String version() {
return this.versionId;
}

/**
* Returns boolean for governanceBypass.
*/
public boolean governanceBypass() {
return this.bypassGovernanceRetention;
}
}
Loading

0 comments on commit b5c1c80

Please sign in to comment.