View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package dev.aherscu.qa.s3.publisher.maven.plugin.config;
18  
19  import java.util.*;
20  
21  import com.amazonaws.services.s3.model.*;
22  
23  public class ParametersValidator {
24  
25      private static final String       CONTENT_ENCODING_GZIP    = "gzip";
26      private static final String       CONTENT_ENCODING_PLAIN   = "plain";
27      private static final List<String> CONTENT_ENCODING_OPTIONS = Arrays
28          .asList(CONTENT_ENCODING_PLAIN, CONTENT_ENCODING_GZIP);
29  
30      private final List<Include>       includes;
31      private final List<Metadata>      metadatas;
32  
33      public ParametersValidator(final List<Include> includes,
34          final List<Metadata> metadatas) {
35          this.includes = includes;
36          this.metadatas = metadatas;
37      }
38  
39      private static boolean isEmptyMetadataId(final String metadataId) {
40          return metadataId == null || "".equals(metadataId);
41      }
42  
43      private static CannedAccessControlList validateCannedAcl(
44          final Metadata metadata) {
45          return CannedAccessControlList.valueOf(metadata.getCannedAcl());
46      }
47  
48      private static void validateContentEncoding(final Metadata metadata) {
49          if (!CONTENT_ENCODING_OPTIONS.contains(metadata.getContentEncoding()))
50              throw new IllegalStateException("The metadata " + metadata.getId()
51                  + " has an invalid contentType");
52      }
53  
54      private static void validateSecondsToExpire(final Metadata metadata) {
55          if (metadata.getSecondsToExpire() < 0)
56              throw new IllegalStateException("The metadata " + metadata.getId()
57                  + " has an invalid secondsToExpire");
58      }
59  
60      public void validate() {
61          validateIncludes();
62          validateMetadatas();
63      }
64  
65      private boolean existsDefaultMetadata() {
66          boolean existsDefaultMetadata = false;
67          for (final Metadata metadata : metadatas) {
68              if (metadata.isDefault()) {
69                  existsDefaultMetadata = true;
70                  break;
71              }
72          }
73          return existsDefaultMetadata;
74      }
75  
76      private void validateIncludes() {
77          final boolean existsDefaultMetadata = existsDefaultMetadata();
78          for (final Include include : includes) {
79              if (!existsDefaultMetadata
80                  && isEmptyMetadataId(include.getBind().getMetadataId()))
81                  throw new IllegalStateException("The include "
82                      + include.getBind().getPattern()
83                      + " does not define a metadataId and there isn't a default one");
84          }
85      }
86  
87      private void validateMetadatas() {
88          for (final Metadata metadata : metadatas) {
89              validateContentEncoding(metadata);
90              validateSecondsToExpire(metadata);
91              validateCannedAcl(metadata);
92          }
93      }
94  }