diff --git a/myconext-server/src/main/java/myconext/manage/ManageServiceProviderConfiguration.java b/myconext-server/src/main/java/myconext/manage/ManageServiceProviderConfiguration.java index c1c33a7b..c0f35437 100644 --- a/myconext-server/src/main/java/myconext/manage/ManageServiceProviderConfiguration.java +++ b/myconext-server/src/main/java/myconext/manage/ManageServiceProviderConfiguration.java @@ -1,9 +1,11 @@ package myconext.manage; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import org.springframework.core.io.Resource; import java.util.Arrays; @@ -14,10 +16,17 @@ public class ManageServiceProviderConfiguration { public ServiceProviderResolver serviceProviderResolver(@Value("${manage.username}") String userName, @Value("${manage.password}") String password, @Value("${manage.base_url}") String baseUrl, + @Value("${manage.enabled}") boolean enabled, + @Value("${manage.enabled}") Resource resource, + ObjectMapper objectMapper, Environment environment) { String[] activeProfiles = environment.getActiveProfiles(); - return activeProfiles.length > 0 ? new MockServiceProviderResolver() : - new ManageServiceProviderResolver(userName, password, baseUrl); + //We can't use the @Bean(profile) annotation as we need more flexibility + if (activeProfiles.length > 0) { + return new MockServiceProviderResolver(); + } + return enabled ? new ManageServiceProviderResolver(userName, password, baseUrl) : + new ResourceServiceProviderResolver(resource, objectMapper) ; } } diff --git a/myconext-server/src/main/java/myconext/manage/ResourceServiceProviderResolver.java b/myconext-server/src/main/java/myconext/manage/ResourceServiceProviderResolver.java new file mode 100644 index 00000000..9913ce8a --- /dev/null +++ b/myconext-server/src/main/java/myconext/manage/ResourceServiceProviderResolver.java @@ -0,0 +1,44 @@ +package myconext.manage; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; +import myconext.model.ServiceProvider; +import org.apache.commons.io.IOUtil; +import org.springframework.core.io.Resource; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class ResourceServiceProviderResolver implements ServiceProviderResolver { + + private final List>> spNames; + + @SneakyThrows + public ResourceServiceProviderResolver(Resource resource, ObjectMapper objectMapper) { + spNames = objectMapper.readValue(IOUtil.toString(resource.getInputStream()), new TypeReference<>() { + }); + } + + @Override + public void refresh() { + //noop + } + + @Override + public Optional resolve(String entityId) { + return spNames.stream().filter(m -> m.containsKey(entityId)) + .findFirst() + .map(entry -> { + Map data = entry.get(entityId); + return new ServiceProvider( + data.get("en"), + data.get("nl"), + data.get("logo_url"), + data.get("home_url"), + data.get("institution_guid")); + }); + + } +} diff --git a/myconext-server/src/main/resources/application.yml b/myconext-server/src/main/resources/application.yml index 2bf07db2..6238b086 100644 --- a/myconext-server/src/main/resources/application.yml +++ b/myconext-server/src/main/resources/application.yml @@ -46,6 +46,9 @@ manage: username: myconext password: secret base_url: https://manage.test2.surfconext.nl + enabled: False + # Can also be a https url + fallback_url: "classpath:metadata/sp-names.json" mongodb_db: surf_id_test base_domain: test2.surfconext.nl diff --git a/myconext-server/src/test/java/myconext/manage/ManageServiceProviderConfigurationTest.java b/myconext-server/src/test/java/myconext/manage/ManageServiceProviderConfigurationTest.java new file mode 100644 index 00000000..75030bd0 --- /dev/null +++ b/myconext-server/src/test/java/myconext/manage/ManageServiceProviderConfigurationTest.java @@ -0,0 +1,29 @@ +package myconext.manage; + +import com.fasterxml.jackson.databind.ObjectMapper; +import myconext.model.ServiceProvider; +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.mock.env.MockEnvironment; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class ManageServiceProviderConfigurationTest { + + @Test + void serviceProviderResolver() { + ManageServiceProviderConfiguration configuration = new ManageServiceProviderConfiguration(); + ServiceProviderResolver serviceProviderResolver = configuration.serviceProviderResolver(null, null, null, false, + new ClassPathResource("metadata/sp-names.json"), + new ObjectMapper(), + new MockEnvironment()); + ServiceProvider serviceProvider = serviceProviderResolver.resolve("http://mock-sp").get(); + assertEquals("SURFconext Mujina SP", serviceProvider.getName()); + + Optional optionalServiceProvider = serviceProviderResolver.resolve("nope"); + assertFalse(optionalServiceProvider.isPresent()); + + } +} \ No newline at end of file