Skip to content

Commit

Permalink
Fix: Active Https in production
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeMBourgeois committed Feb 27, 2024
1 parent 91713c7 commit dacc2f8
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/main/java/fr/insee/rmes/config/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.sql.DataSource;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
Expand All @@ -22,15 +26,15 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContext {

@Value("${fr.insee.rmes.api.host}")
private String serverHost;

@Value("${fr.insee.ntlm.user}")
private String ntlmUser;
Expand All @@ -47,6 +51,12 @@ public class ApplicationContext {
@Value("#{'${fr.insee.rmes.search.root.resource-package.ids}'.split(',')}")
private List<String> ressourcePackageIds;

@Autowired
private final Environment environment;

public ApplicationContext(Environment environment) {
this.environment = environment;
}
@Bean
public HttpClientBuilder httpClientBuilder()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
Expand All @@ -66,8 +76,24 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Bean
public OpenAPI customOpenAPI() {
if (isProductionProfileActive()) {
return new OpenAPI().servers(List.of(
new Server().url("https://" + serverHost ).description("HTTPS Server")));
} else {
return new OpenAPI();
}
}



private boolean isProductionProfileActive() {
String[] activeProfiles = environment.getActiveProfiles();
for (String profile : activeProfiles) {
if (profile.equalsIgnoreCase("prod") || profile.equalsIgnoreCase("production")) {
return true;
}
}
return false;
}

}

0 comments on commit dacc2f8

Please sign in to comment.