1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.linecorp.centraldogma.server;
17
18 import java.io.IOException;
19 import java.nio.charset.StandardCharsets;
20 import java.nio.file.Files;
21 import java.nio.file.Paths;
22 import java.util.Base64;
23 import java.util.List;
24
25 import com.google.common.collect.ImmutableList;
26
27 enum DefaultConfigValueConverter implements ConfigValueConverter {
28 INSTANCE;
29
30 private static final String PLAINTEXT = "plaintext";
31 private static final String FILE = "file";
32 private static final String BASE64 = "base64";
33
34
35 private static final List<String> SUPPORTED_PREFIXES = ImmutableList.of(PLAINTEXT, FILE, BASE64);
36
37 @Override
38 public List<String> supportedPrefixes() {
39 return SUPPORTED_PREFIXES;
40 }
41
42 @Override
43 public String convert(String prefix, String value) {
44 switch (prefix) {
45 case PLAINTEXT:
46 return value;
47 case FILE:
48 try {
49 return new String(Files.readAllBytes(Paths.get(value)), StandardCharsets.UTF_8);
50 } catch (IOException e) {
51 throw new RuntimeException("failed to read a file: " + value, e);
52 }
53 case BASE64:
54 return new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8).trim();
55 default:
56
57 throw new Error();
58 }
59 }
60 }