View Javadoc
1   /*-
2    * #%L
3    * Secured Properties
4    * ===============================================================
5    * Copyright (C) 2016 - 2019 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package net.brabenetz.lib.securedproperties.config;
21  
22  import net.brabenetz.lib.securedproperties.core.Algorithm;
23  import net.brabenetz.lib.securedproperties.core.SupportedAlgorithm;
24  import org.apache.commons.lang3.StringUtils;
25  
26  import java.io.File;
27  import java.util.Arrays;
28  import java.util.EnumMap;
29  import java.util.Map;
30  import java.util.function.BiConsumer;
31  import java.util.function.Function;
32  
33  /**
34   * Parent helper class to easier implement a custom ConfigInitializer. Only {@link #getValue(ConfigKey)} must be implemented.
35   */
36  public abstract class AbstractConfigInitializer implements ConfigInitializer {
37  
38      private static final Map<ConfigKey, BiConsumer<Config, String>> CONFIG_KEY_TO_INIT_FUNCTION_MAPPING = initConfigMappings();
39  
40      private final Function<ConfigKey, String> keyFactory;
41  
42      protected AbstractConfigInitializer() {
43          this((final ConfigKey key) -> ConfigKey.DEFAULT_PREFIX_KEBAB_CASE + '.' + key.getKebabCase());
44      }
45  
46      protected AbstractConfigInitializer(final Function<ConfigKey, String> keyFactory) {
47          this.keyFactory = keyFactory;
48      }
49  
50      /**
51       * Small convenience Constructor for concrete implementation which can have an optional config key-prefix.
52       */
53      protected AbstractConfigInitializer(final String configKeyPrefix,
54              final Function<ConfigKey, String> keyFactoryWithoutPrefix,
55              final Function<ConfigKey, String> keyFactoryWithPrefix) {
56          if (StringUtils.isEmpty(configKeyPrefix)) {
57              keyFactory = keyFactoryWithoutPrefix;
58          } else {
59              keyFactory = keyFactoryWithPrefix;
60          }
61      }
62  
63      protected Function<ConfigKey, String> getKeyFactory() {
64          return keyFactory;
65      }
66  
67      @Override
68      public void init(final Config config) {
69          for (final ConfigKey configKey : ConfigKey.values()) {
70              final String value = getValue(configKey);
71              if (value != null) {
72                  CONFIG_KEY_TO_INIT_FUNCTION_MAPPING.get(configKey).accept(config, value);
73              }
74          }
75      }
76  
77      private static Map<ConfigKey, BiConsumer<Config, String>> initConfigMappings() {
78          final Map<ConfigKey, BiConsumer<Config, String>> mapping = new EnumMap<>(ConfigKey.class);
79          mapping.put(ConfigKey.SECRET_FILE, AbstractConfigInitializer::initSecretFilePath);
80          mapping.put(ConfigKey.SALT_LENGTH, AbstractConfigInitializer::initSaltLength);
81          mapping.put(ConfigKey.ALLOWED_ALGORITHM, AbstractConfigInitializer::initAllowedAlgorithm);
82          mapping.put(ConfigKey.AUTO_CREATE_SECRET_KEY, AbstractConfigInitializer::initAutoCreateSecretKey);
83          return mapping;
84  
85      }
86  
87      protected static void initSecretFilePath(final Config config, final String value) {
88          config.withSecretFile(new File(value));
89      }
90  
91      protected static void initSaltLength(final Config config, final String value) {
92          config.withSaltLength(Integer.parseInt(value));
93      }
94  
95      protected static void initAllowedAlgorithm(final Config config, final String value) {
96          config.withAllowedAlgorithm(Arrays.stream(value.split("[, ]+")).map(SupportedAlgorithm::valueOf).toArray(Algorithm[]::new));
97      }
98  
99      protected static void initAutoCreateSecretKey(final Config config, final String value) {
100         config.withAutoCreateSecretKey(Boolean.valueOf(value));
101     }
102 
103     /**
104      * @param key The {@link ConfigKey} to search the value for.
105      * @return the Value as simple string.
106      */
107     protected abstract String getValue(ConfigKey key);
108 }