|
5 | 5 | package io.securecodebox.persistence.defectdojo.config; |
6 | 6 |
|
7 | 7 | import io.securecodebox.persistence.defectdojo.exception.ConfigException; |
8 | | -import lombok.*; |
| 8 | +import lombok.EqualsAndHashCode; |
| 9 | +import lombok.Getter; |
| 10 | +import lombok.NonNull; |
| 11 | +import lombok.ToString; |
9 | 12 |
|
10 | 13 | /** |
11 | 14 | * Configures the DefectDojo client |
|
14 | 17 | @ToString |
15 | 18 | @EqualsAndHashCode |
16 | 19 | public final class Config { |
17 | | - /** |
18 | | - * Default for {@link #maxPageCountForGets} |
19 | | - */ |
20 | | - static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100; |
21 | | - /** |
22 | | - * Null pattern object. |
23 | | - */ |
24 | | - public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS); |
25 | | - |
26 | | - /** |
27 | | - * URL of the host which serves the DefectDojo API. |
28 | | - * <p> |
29 | | - * It is only allowed to configure the base URL (e.g. {@literal "https://defectdojo.securecodebox.io/"} without |
30 | | - * any path. The path to the concrete API endpoints are maintained by this client library itself. |
31 | | - * </p> |
32 | | - */ |
33 | | - @NonNull |
34 | | - private final String url; |
35 | | - |
36 | | - /** |
37 | | - * API key to authorize against the DefectDojo API. |
38 | | - */ |
39 | | - @NonNull |
40 | | - private final String apiKey; |
41 | | - |
42 | | - /** |
43 | | - * How many pages of objects are fetched from the DefectDojo API |
44 | | - * <p> |
45 | | - * This setting is to avoid out of memory scenarios. |
46 | | - * </p> |
47 | | - * <p> |
48 | | - * Defaults to {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS}. |
49 | | - * </p> |
50 | | - */ |
51 | | - private final int maxPageCountForGets; |
52 | | - |
53 | | - /** |
54 | | - * Convenience constructor which sets {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS} |
55 | | - * |
56 | | - * @param url not {@code null} |
57 | | - * @param apiKey not {@code null} |
58 | | - */ |
59 | | - public Config(final @NonNull String url, final @NonNull String apiKey) { |
60 | | - this(url, apiKey, DEFAULT_MAX_PAGE_COUNT_FOR_GETS); |
| 20 | + /** |
| 21 | + * Default for {@link #maxPageCountForGets} |
| 22 | + */ |
| 23 | + static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100; |
| 24 | + /** |
| 25 | + * Null pattern object. |
| 26 | + */ |
| 27 | + public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS); |
| 28 | + |
| 29 | + /** |
| 30 | + * URL of the host which serves the DefectDojo API. |
| 31 | + * <p> |
| 32 | + * It is only allowed to configure the base URL (e.g. {@literal "https://defectdojo.securecodebox.io/"} without |
| 33 | + * any path. The path to the concrete API endpoints are maintained by this client library itself. |
| 34 | + * </p> |
| 35 | + */ |
| 36 | + @NonNull |
| 37 | + private final String url; |
| 38 | + |
| 39 | + /** |
| 40 | + * API key to authorize against the DefectDojo API. |
| 41 | + */ |
| 42 | + @NonNull |
| 43 | + private final String apiKey; |
| 44 | + |
| 45 | + /** |
| 46 | + * How many pages of objects are fetched from the DefectDojo API |
| 47 | + * <p> |
| 48 | + * This setting is to avoid out of memory scenarios. |
| 49 | + * </p> |
| 50 | + * <p> |
| 51 | + * Defaults to {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS}. |
| 52 | + * </p> |
| 53 | + */ |
| 54 | + private final int maxPageCountForGets; |
| 55 | + |
| 56 | + /** |
| 57 | + * Convenience constructor which sets {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS} |
| 58 | + * |
| 59 | + * @param url not {@code null} |
| 60 | + * @param apiKey not {@code null} |
| 61 | + */ |
| 62 | + public Config(final @NonNull String url, final @NonNull String apiKey) { |
| 63 | + this(url, apiKey, DEFAULT_MAX_PAGE_COUNT_FOR_GETS); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Dedicated constructor |
| 68 | + * |
| 69 | + * @param url not {@code null} |
| 70 | + * @param apiKey not {@code null} |
| 71 | + * @param maxPageCountForGets not less than 1 |
| 72 | + */ |
| 73 | + public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) { |
| 74 | + super(); |
| 75 | + this.url = url; |
| 76 | + this.apiKey = apiKey; |
| 77 | + this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets"); |
| 78 | + } |
| 79 | + |
| 80 | + private static int validateIsGreaterZero(final int number, final String name) { |
| 81 | + if (number < 1) { |
| 82 | + throw new IllegalArgumentException(String.format("%s must be greater than 0!", name)); |
61 | 83 | } |
62 | 84 |
|
63 | | - /** |
64 | | - * Dedicated constructor |
65 | | - * |
66 | | - * @param url not {@code null} |
67 | | - * @param apiKey not {@code null} |
68 | | - * @param maxPageCountForGets not less than 1 |
69 | | - */ |
70 | | - public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) { |
71 | | - super(); |
72 | | - this.url = url; |
73 | | - this.apiKey = apiKey; |
74 | | - this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets"); |
75 | | - } |
76 | | - |
77 | | - private static int validateIsGreaterZero(final int number, final String name) { |
78 | | - if (number < 1) { |
79 | | - throw new IllegalArgumentException(String.format("%s must be greater than 0!", name)); |
80 | | - } |
81 | | - |
82 | | - return number; |
| 85 | + return number; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Creates config from environment variables |
| 90 | + * |
| 91 | + * @return never {@code null} |
| 92 | + */ |
| 93 | + public static Config fromEnv() { |
| 94 | + final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL); |
| 95 | + final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY); |
| 96 | + final int maxPageCountForGets; |
| 97 | + |
| 98 | + if (hasEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)) { |
| 99 | + try { |
| 100 | + maxPageCountForGets = Integer.parseInt(findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)); |
| 101 | + } catch (final NumberFormatException e) { |
| 102 | + throw new ConfigException(String.format("Given value for environment variable '%s' is not a valid number! Given was '%s'.", EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS.literal, findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)), |
| 103 | + e); |
| 104 | + } |
| 105 | + } else { |
| 106 | + maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS; |
83 | 107 | } |
84 | 108 |
|
85 | | - /** |
86 | | - * Creates config from environment variables |
87 | | - * |
88 | | - * @return never {@code null} |
89 | | - */ |
90 | | - public static Config fromEnv() { |
91 | | - final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL); |
92 | | - final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY); |
93 | | - final int maxPageCountForGets; |
94 | | - |
95 | | - if (hasEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)) { |
96 | | - try { |
97 | | - maxPageCountForGets = Integer.parseInt(findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)); |
98 | | - } catch (final NumberFormatException e) { |
99 | | - throw new ConfigException(String.format("Given value for environment variable '%s' is not a valid number! Given was '%s'.", EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS.literal, findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)), |
100 | | - e); |
101 | | - } |
102 | | - } else { |
103 | | - maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS; |
104 | | - } |
105 | | - |
106 | | - return new Config(url, apiKey, maxPageCountForGets); |
107 | | - } |
| 109 | + return new Config(url, apiKey, maxPageCountForGets); |
| 110 | + } |
108 | 111 |
|
109 | | - private static boolean hasEnvVar(final @NonNull EnvVars name) { |
110 | | - return findEnvVar(name) != null; |
111 | | - } |
| 112 | + private static boolean hasEnvVar(final @NonNull EnvVars name) { |
| 113 | + return findEnvVar(name) != null; |
| 114 | + } |
112 | 115 |
|
113 | | - private static String findEnvVar(final @NonNull EnvVars name) { |
114 | | - return System.getenv(name.literal); |
115 | | - } |
| 116 | + private static String findEnvVar(final @NonNull EnvVars name) { |
| 117 | + return System.getenv(name.literal); |
| 118 | + } |
116 | 119 |
|
117 | | - private static String findRequiredEnvVar(final @NonNull EnvVars name) { |
118 | | - final var envVar = System.getenv(name.literal); |
| 120 | + private static String findRequiredEnvVar(final @NonNull EnvVars name) { |
| 121 | + final var envVar = System.getenv(name.literal); |
119 | 122 |
|
120 | | - if (envVar == null) { |
121 | | - throw new ConfigException(String.format("Missing environment variable '%s'!", name.literal)); |
122 | | - } |
123 | | - return envVar; |
| 123 | + if (envVar == null) { |
| 124 | + throw new ConfigException(String.format("Missing environment variable '%s'!", name.literal)); |
124 | 125 | } |
125 | | - |
| 126 | + return envVar; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Enumerates the available environment variables to configure the client |
| 131 | + */ |
| 132 | + public enum EnvVars { |
| 133 | + DEFECTDOJO_URL("DEFECTDOJO_URL"), |
| 134 | + DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"), |
| 135 | + DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS"); |
126 | 136 | /** |
127 | | - * Enumerates the available environment variables to configure the client |
| 137 | + * Literal name of configuration environment name |
| 138 | + * <p> |
| 139 | + * We use an own value to hold the actual value, so that refactoring the enum name does |
| 140 | + * not break the published API of env var names. |
| 141 | + * </p> |
128 | 142 | */ |
129 | | - public enum EnvVars { |
130 | | - DEFECTDOJO_URL("DEFECTDOJO_URL"), |
131 | | - DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"), |
132 | | - DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS"); |
133 | | - /** |
134 | | - * Literal name of configuration environment name |
135 | | - * <p> |
136 | | - * We use an own value to hold the actual value, so that refactoring the enum name does |
137 | | - * not break the published API of env var names. |
138 | | - * </p> |
139 | | - */ |
140 | | - private final String literal; |
141 | | - |
142 | | - EnvVars(final String literal) { |
143 | | - this.literal = literal; |
144 | | - } |
| 143 | + private final String literal; |
| 144 | + |
| 145 | + EnvVars(final String literal) { |
| 146 | + this.literal = literal; |
145 | 147 | } |
| 148 | + } |
146 | 149 | } |
0 commit comments