2828import com .redhat .exhort .image .ImageUtils ;
2929import com .redhat .exhort .logging .LoggersFactory ;
3030import com .redhat .exhort .tools .Ecosystem ;
31+ import com .redhat .exhort .utils .Environment ;
3132import jakarta .mail .MessagingException ;
3233import jakarta .mail .internet .MimeMultipart ;
3334import jakarta .mail .util .ByteArrayDataSource ;
6162/** Concrete implementation of the Exhort {@link Api} Service. */
6263public final class ExhortApi implements Api {
6364
65+ private static final String DEV_EXHORT_BACKEND_URL = "DEV_EXHORT_BACKEND_URL" ;
66+
67+ private static final String EXHORT_DEV_MODE = "EXHORT_DEV_MODE" ;
68+
69+ private static final String HTTP_VERSION_EXHORT_CLIENT = "HTTP_VERSION_EXHORT_CLIENT" ;
70+
6471 private static final Logger LOG = LoggersFactory .getLogger (ExhortApi .class .getName ());
6572
6673 public static final String DEFAULT_ENDPOINT = "https://rhda.rhcloud.com" ;
@@ -126,8 +133,8 @@ public ExhortApi() {
126133 * @return i.e. HttpClient.Version.HTTP_1.1
127134 */
128135 static HttpClient .Version getHttpVersion () {
129- return ( System . getenv ( " HTTP_VERSION_EXHORT_CLIENT" ) != null
130- && System . getenv ( "HTTP_VERSION_EXHORT_CLIENT" ) .contains ("2" ))
136+ var version = Environment . get ( HTTP_VERSION_EXHORT_CLIENT );
137+ return ( version != null && version .contains ("2" ))
131138 ? HttpClient .Version .HTTP_2
132139 : HttpClient .Version .HTTP_1_1 ;
133140 }
@@ -141,27 +148,27 @@ static HttpClient.Version getHttpVersion() {
141148 this .client = client ;
142149 this .mapper = new ObjectMapper ().disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
143150 // Take default from config.properties in case client didn't override DEV MODE
144- if (System . getProperty ( " EXHORT_DEV_MODE" ) == null ) {
151+ if (Environment . get ( EXHORT_DEV_MODE ) == null ) {
145152 try {
146153 InputStream exhortConfig =
147154 this .getClass ().getClassLoader ().getResourceAsStream ("config.properties" );
148155 if (exhortConfig == null ) {
149156 LOG .info (
150157 "config.properties not found on the class path, fallback to default DEV MODE ="
151158 + " false" );
152- System .setProperty (" EXHORT_DEV_MODE" , "false" );
159+ System .setProperty (EXHORT_DEV_MODE , "false" );
153160 } else {
154161 Properties properties = new Properties ();
155162 properties .load (exhortConfig );
156- System .setProperty (" EXHORT_DEV_MODE" , (String ) properties .get (" EXHORT_DEV_MODE" ));
163+ System .setProperty (EXHORT_DEV_MODE , (String ) properties .get (EXHORT_DEV_MODE ));
157164 }
158165 } catch (IOException e ) {
159166 LOG .info (
160167 String .format (
161168 "Error loading config.properties , fallback to set default property DEV MODE ="
162169 + " false, Error message = %s" ,
163170 e .getMessage ()));
164- System .setProperty (" EXHORT_DEV_MODE" , "false" );
171+ System .setProperty (EXHORT_DEV_MODE , "false" );
165172 }
166173 }
167174
@@ -198,8 +205,8 @@ private static String getClientRequestId() {
198205
199206 public String getExhortUrl () {
200207 String endpoint ;
201- if (getBooleanValueEnvironment ( " EXHORT_DEV_MODE" , " false" )) {
202- endpoint = getStringValueEnvironment ( " DEV_EXHORT_BACKEND_URL" , DEFAULT_ENDPOINT_DEV );
208+ if (Environment . getBoolean ( EXHORT_DEV_MODE , false )) {
209+ endpoint = Environment . get ( DEV_EXHORT_BACKEND_URL , DEFAULT_ENDPOINT_DEV );
203210
204211 } else {
205212 endpoint = DEFAULT_ENDPOINT ;
@@ -209,29 +216,15 @@ public String getExhortUrl() {
209216 String .format (
210217 "EXHORT_DEV_MODE=%s,DEV_EXHORT_BACKEND_URL=%s, Chosen Backend URL=%s ,"
211218 + " DEFAULT_ENDPOINT_DEV=%s , DEFAULT_ENDPOINT=%s" ,
212- getBooleanValueEnvironment ( " EXHORT_DEV_MODE" , " false" ),
213- getStringValueEnvironment ( " DEV_EXHORT_BACKEND_URL" , DEFAULT_ENDPOINT_DEV ),
219+ Environment . getBoolean ( EXHORT_DEV_MODE , false ),
220+ Environment . get ( DEV_EXHORT_BACKEND_URL , DEFAULT_ENDPOINT_DEV ),
214221 endpoint ,
215222 DEFAULT_ENDPOINT_DEV ,
216223 DEFAULT_ENDPOINT ));
217224 }
218225 return endpoint ;
219226 }
220227
221- public static boolean getBooleanValueEnvironment (String key , String defaultValue ) {
222- String result =
223- Objects .requireNonNullElse (
224- System .getenv (key ), Objects .requireNonNullElse (System .getProperty (key ), defaultValue ));
225- return Boolean .parseBoolean (result .trim ().toLowerCase ());
226- }
227-
228- public static String getStringValueEnvironment (String key , String defaultValue ) {
229- String result =
230- Objects .requireNonNullElse (
231- System .getenv (key ), Objects .requireNonNullElse (System .getProperty (key ), defaultValue ));
232- return result ;
233- }
234-
235228 @ Override
236229 public CompletableFuture <MixedReport > stackAnalysisMixed (final String manifestFile )
237230 throws IOException {
@@ -390,7 +383,7 @@ private static void logExhortRequestId(HttpResponse<?> response) {
390383 }
391384
392385 public static boolean debugLoggingIsNeeded () {
393- return Boolean . parseBoolean ( getStringValueEnvironment ( "EXHORT_DEBUG" , " false" ) );
386+ return Environment . getBoolean ( "EXHORT_DEBUG" , false );
394387 }
395388
396389 @ Override
@@ -639,20 +632,20 @@ private HttpRequest buildRequest(
639632 Stream .of (ExhortApi .TokenProvider .values ())
640633 .forEach (
641634 p -> {
642- var envToken = System . getenv (p .getVarName ());
635+ var envToken = Environment . get (p .getVarName ());
643636 if (Objects .nonNull (envToken )) {
644637 request .setHeader (p .getHeaderName (), envToken );
645638 } else {
646- var propToken = System . getProperty (p .getVarName ());
639+ var propToken = Environment . get (p .getVarName ());
647640 if (Objects .nonNull (propToken )) {
648641 request .setHeader (p .getHeaderName (), propToken );
649642 }
650643 }
651- var envUser = System . getenv (p .getUserHeaderName ());
644+ var envUser = Environment . get (p .getUserHeaderName ());
652645 if (Objects .nonNull (envUser )) {
653646 request .setHeader (p .getUserHeaderName (), envUser );
654647 } else {
655- var propUser = System . getProperty (p .getUserVarName ());
648+ var propUser = Environment . get (p .getUserVarName ());
656649 if (Objects .nonNull (propUser )) {
657650 request .setHeader (p .getUserHeaderName (), propUser );
658651 }
@@ -685,11 +678,6 @@ private String calculateHeaderValue(String headerName) {
685678 }
686679
687680 private String calculateHeaderValueActual (String headerName ) {
688- String result = null ;
689- result = System .getenv (headerName );
690- if (result == null ) {
691- result = System .getProperty (headerName );
692- }
693- return result ;
681+ return Environment .get (headerName );
694682 }
695683}
0 commit comments