Skip to content

Commit

Permalink
Allow fallback for not using manage
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 11, 2023
1 parent ef45957 commit 648bd5d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) ;
}

}
Original file line number Diff line number Diff line change
@@ -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<Map<String, Map<String, String>>> 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<ServiceProvider> resolve(String entityId) {
return spNames.stream().filter(m -> m.containsKey(entityId))
.findFirst()
.map(entry -> {
Map<String, String> 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"));
});

}
}
3 changes: 3 additions & 0 deletions myconext-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServiceProvider> optionalServiceProvider = serviceProviderResolver.resolve("nope");
assertFalse(optionalServiceProvider.isPresent());

}
}

0 comments on commit 648bd5d

Please sign in to comment.