public void AddLifecycleRule()

in src/main/java/com/aliyun/oss/model/SetBucketLifecycleRequest.java [55:123]


    public void AddLifecycleRule(LifecycleRule lifecycleRule) {
        if (lifecycleRule == null) {
            throw new IllegalArgumentException("lifecycleRule should not be null or empty.");
        }

        if (lifecycleRule.getId() != null && lifecycleRule.getId().length() > MAX_RULE_ID_LENGTH) {
            throw new IllegalArgumentException("Length of lifecycle rule id exceeds max limit " + MAX_RULE_ID_LENGTH);
        }

        int expirationTimeFlag = lifecycleRule.hasExpirationTime() ? 1 : 0;
        int expirationDaysFlag = lifecycleRule.hasExpirationDays() ? 1 : 0;
        int createdBeforeDateFlag = lifecycleRule.hasCreatedBeforeDate() ? 1 : 0;
        int expiredDeleteMarkerFlag = lifecycleRule.hasExpiredDeleteMarker() ? 1: 0;
        int flagSum = expirationTimeFlag + expirationDaysFlag + createdBeforeDateFlag + expiredDeleteMarkerFlag;
        if (flagSum > 1 ) {
            throw new IllegalArgumentException("Only one expiration property should be specified.");
        }

        if (flagSum == 0 && !lifecycleRule.hasAbortMultipartUpload()
                && !lifecycleRule.hasStorageTransition() && !lifecycleRule.hasNoncurrentVersionStorageTransitions()
                && !lifecycleRule.hasNoncurrentVersionExpiration()) {
            throw new IllegalArgumentException("Rule has none expiration or transition specified.");
        }

        if (lifecycleRule.getStatus() == LifecycleRule.RuleStatus.Unknown) {
            throw new IllegalArgumentException("RuleStatus property should be specified with 'Enabled' or 'Disabled'.");
        }

        if (lifecycleRule.hasAbortMultipartUpload()) {
            LifecycleRule.AbortMultipartUpload abortMultipartUpload = lifecycleRule.getAbortMultipartUpload();
            expirationDaysFlag = abortMultipartUpload.hasExpirationDays() ? 1 : 0;
            createdBeforeDateFlag = abortMultipartUpload.hasCreatedBeforeDate() ? 1 : 0;
            flagSum = expirationDaysFlag + createdBeforeDateFlag;
            if (flagSum != 1) {
                throw new IllegalArgumentException(
                        "Only one expiration property for AbortMultipartUpload should be specified.");
            }
        }

        if (lifecycleRule.hasStorageTransition()) {
            for (StorageTransition storageTransition : lifecycleRule.getStorageTransition()) {
                expirationDaysFlag = storageTransition.hasExpirationDays() ? 1 : 0;
                createdBeforeDateFlag = storageTransition.hasCreatedBeforeDate() ? 1 : 0;
                flagSum = expirationDaysFlag + createdBeforeDateFlag;
                if (flagSum != 1) {
                    throw new IllegalArgumentException(
                            "Only one expiration property for StorageTransition should be specified.");
                }
            }
        }

        if (lifecycleRule.getFilter() != null) {
            for(LifecycleNot lifecycleNot : lifecycleRule.getFilter().getNotList()){
                if(lifecycleNot == null){
                    throw new IllegalArgumentException("Not node cannot be empty.");
                }
                if(lifecycleNot.getPrefix() == null){
                    throw new IllegalArgumentException("The prefix node under the not node cannot be empty.");
                }
                if(lifecycleNot.getTag() == null && lifecycleNot.getPrefix().equals(lifecycleRule.getPrefix())){
                    throw new IllegalArgumentException("If there is no tag node under the not node, the prefix of the not node cannot be the same as that of the rule.");
                }
                if(!lifecycleNot.getPrefix().contains(lifecycleRule.getPrefix())){
                    lifecycleNot.setPrefix(lifecycleRule.getPrefix() + lifecycleNot.getPrefix());
                }
            }
        }
        this.lifecycleRules.add(lifecycleRule);
    }