Skip to content

Commit de9ca1c

Browse files
authored
Add more validation in lifecycle configuration class (#1106)
1 parent 6523abe commit de9ca1c

File tree

8 files changed

+51
-25
lines changed

8 files changed

+51
-25
lines changed

api/src/main/java/io/minio/messages/AbortIncompleteMultipartUpload.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.minio.messages;
1818

19-
import javax.annotation.Nullable;
2019
import org.simpleframework.xml.Element;
2120
import org.simpleframework.xml.Root;
2221

@@ -25,16 +24,15 @@
2524
*/
2625
@Root(name = "AbortIncompleteMultipartUpload")
2726
public class AbortIncompleteMultipartUpload {
28-
@Element(name = "DaysAfterInitiation", required = false)
29-
private Integer daysAfterInitiation;
27+
@Element(name = "DaysAfterInitiation")
28+
private int daysAfterInitiation;
3029

3130
public AbortIncompleteMultipartUpload(
32-
@Nullable @Element(name = "DaysAfterInitiation", required = false)
33-
Integer daysAfterInitiation) {
31+
@Element(name = "DaysAfterInitiation") int daysAfterInitiation) {
3432
this.daysAfterInitiation = daysAfterInitiation;
3533
}
3634

37-
public Integer daysAfterInitiation() {
35+
public int daysAfterInitiation() {
3836
return daysAfterInitiation;
3937
}
4038
}

api/src/main/java/io/minio/messages/DateDays.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ public abstract class DateDays {
2828
private Integer days;
2929

3030
public DateDays(ResponseDate date, Integer days) {
31-
this.date = date;
32-
this.days = days;
31+
if (date != null ^ days != null) {
32+
this.date = date;
33+
this.days = days;
34+
} else {
35+
throw new IllegalArgumentException("Only one of date or days must be set");
36+
}
3337
}
3438

3539
public ZonedDateTime date() {

api/src/main/java/io/minio/messages/LifecycleRule.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ public LifecycleRule(
5555
@Nullable @Element(name = "AbortIncompleteMultipartUpload", required = false)
5656
AbortIncompleteMultipartUpload abortIncompleteMultipartUpload,
5757
@Nullable @Element(name = "Expiration", required = false) Expiration expiration,
58-
@Nullable @Element(name = "Filter", required = false) RuleFilter filter,
58+
@Nonnull @Element(name = "Filter", required = false) RuleFilter filter,
5959
@Nullable @Element(name = "ID", required = false) String id,
6060
@Nullable @Element(name = "NoncurrentVersionExpiration", required = false)
6161
NoncurrentVersionExpiration noncurrentVersionExpiration,
6262
@Nullable @Element(name = "NoncurrentVersionTransition", required = false)
6363
NoncurrentVersionTransition noncurrentVersionTransition,
6464
@Nullable @Element(name = "Transition", required = false) Transition transition) {
65+
if (abortIncompleteMultipartUpload == null
66+
&& expiration == null
67+
&& noncurrentVersionExpiration == null
68+
&& noncurrentVersionTransition == null
69+
&& transition == null) {
70+
throw new IllegalArgumentException(
71+
"At least one of action (AbortIncompleteMultipartUpload, Expiration, "
72+
+ "NoncurrentVersionExpiration, NoncurrentVersionTransition or Transition) must be "
73+
+ "specified in a rule");
74+
}
75+
6576
if (id != null) {
6677
id = id.trim();
6778
if (id.isEmpty()) throw new IllegalArgumentException("ID must be non-empty string");
@@ -70,7 +81,7 @@ public LifecycleRule(
7081

7182
this.abortIncompleteMultipartUpload = abortIncompleteMultipartUpload;
7283
this.expiration = expiration;
73-
this.filter = filter;
84+
this.filter = Objects.requireNonNull(filter, "Filter must not be null");
7485
this.id = id;
7586
this.noncurrentVersionExpiration = noncurrentVersionExpiration;
7687
this.noncurrentVersionTransition = noncurrentVersionTransition;

api/src/main/java/io/minio/messages/NoncurrentVersionExpiration.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616

1717
package io.minio.messages;
1818

19-
import javax.annotation.Nullable;
2019
import org.simpleframework.xml.Element;
2120
import org.simpleframework.xml.Root;
2221

2322
/** Helper class to denote noncurrent version expiration information for {@link LifecycleRule}. */
2423
@Root(name = "NoncurrentVersionExpiration")
2524
public class NoncurrentVersionExpiration {
26-
@Element(name = "NoncurrentDays", required = false)
27-
private Integer noncurrentDays;
25+
@Element(name = "NoncurrentDays")
26+
private int noncurrentDays;
2827

2928
public NoncurrentVersionExpiration(
30-
@Nullable @Element(name = "NoncurrentDays", required = false) Integer noncurrentDays) {
29+
@Element(name = "NoncurrentDays", required = false) int noncurrentDays) {
3130
this.noncurrentDays = noncurrentDays;
3231
}
3332

34-
public Integer noncurrentDays() {
33+
public int noncurrentDays() {
3534
return noncurrentDays;
3635
}
3736
}

api/src/main/java/io/minio/messages/NoncurrentVersionTransition.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616

1717
package io.minio.messages;
1818

19-
import javax.annotation.Nullable;
19+
import javax.annotation.Nonnull;
2020
import org.simpleframework.xml.Element;
2121
import org.simpleframework.xml.Root;
2222

2323
/** Helper class to denote noncurrent version transition information for {@link LifecycleRule}. */
2424
@Root(name = "NoncurrentVersionTransition")
2525
public class NoncurrentVersionTransition extends NoncurrentVersionExpiration {
26-
@Element(name = "StorageClass", required = false)
26+
@Element(name = "StorageClass")
2727
private String storageClass;
2828

2929
public NoncurrentVersionTransition(
30-
@Nullable @Element(name = "NoncurrentDays", required = false) Integer noncurrentDays,
31-
@Nullable @Element(name = "StorageClass", required = false) String storageClass) {
30+
@Element(name = "NoncurrentDays", required = false) int noncurrentDays,
31+
@Nonnull @Element(name = "StorageClass", required = false) String storageClass) {
3232
super(noncurrentDays);
33+
if (storageClass == null || storageClass.isEmpty()) {
34+
throw new IllegalArgumentException("StorageClass must be provided");
35+
}
3336
this.storageClass = storageClass;
3437
}
3538

api/src/main/java/io/minio/messages/Transition.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717
package io.minio.messages;
1818

1919
import java.time.ZonedDateTime;
20+
import javax.annotation.Nonnull;
2021
import javax.annotation.Nullable;
2122
import org.simpleframework.xml.Element;
2223
import org.simpleframework.xml.Root;
2324

2425
/** Helper class to denote transition information for {@link LifecycleRule}. */
2526
@Root(name = "Transition")
2627
public class Transition extends DateDays {
27-
@Element(name = "StorageClass", required = false)
28+
@Element(name = "StorageClass")
2829
private String storageClass;
2930

3031
public Transition(
3132
@Nullable @Element(name = "Date", required = false) ResponseDate date,
3233
@Nullable @Element(name = "Days", required = false) Integer days,
33-
@Nullable @Element(name = "StorageClass", required = false) String storageClass) {
34+
@Nonnull @Element(name = "StorageClass", required = false) String storageClass) {
3435
super(date, days);
36+
if (storageClass == null || storageClass.isEmpty()) {
37+
throw new IllegalArgumentException("StorageClass must be provided");
38+
}
3539
this.storageClass = storageClass;
3640
}
3741

docs/API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,18 +857,18 @@ rules.add(
857857
"rule1",
858858
null,
859859
null,
860-
new Transition(null, 30, "GLACIER")));
860+
new Transition((ZonedDateTime) null, 30, "GLACIER")));
861861
rules.add(
862862
new LifecycleRule(
863863
Status.ENABLED,
864864
null,
865-
new Expiration(null, 365, null),
865+
new Expiration((ZonedDateTime) null, 365, null),
866866
new RuleFilter("logs/"),
867867
"rule2",
868868
null,
869869
null,
870870
null));
871-
config = new LifecycleConfiguraton(rules);
871+
LifecycleConfiguration config = new LifecycleConfiguration(rules);
872872
minioClient.setBucketLifecycle(
873873
SetBucketLifecycleArgs.builder().bucket("my-bucketname").config(config).build());
874874
```

functional/FunctionalTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,14 @@ public static void getBucketLifecycle() throws Exception {
29052905
testSetBucketLifecycle(
29062906
bucketName,
29072907
new LifecycleRule(
2908-
Status.ENABLED, null, null, new RuleFilter(""), null, null, null, null));
2908+
Status.ENABLED,
2909+
null,
2910+
new Expiration((ZonedDateTime) null, 365, null),
2911+
new RuleFilter(""),
2912+
null,
2913+
null,
2914+
null,
2915+
null));
29092916
config =
29102917
client.getBucketLifecycle(GetBucketLifecycleArgs.builder().bucket(bucketName).build());
29112918
if (config == null) {

0 commit comments

Comments
 (0)