Skip to content

Commit

Permalink
Return unique groups and members based on the urn's
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Nov 27, 2023
1 parent 60ecc96 commit 5e432d4
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 109 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openconext</groupId>
<artifactId>voot-service</artifactId>
<version>5.0.0</version>
<version>5.0.1</version>
<packaging>jar</packaging>

<name>voot-service</name>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<version>2.7.17</version>
<relativePath />
</parent>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/voot/ExternalGroupsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Set<Group> getMyGroups(String uid, String schacHomeOrganization) {
.map(group -> group.id)
.collect(toList());
if (!externalGroupIds.isEmpty()) {
List<Group> linkedGroups = teamsProvider.linkedLocalTeamsGroup(externalGroupIds);
Set<Group> linkedGroups = teamsProvider.linkedLocalTeamsGroup(externalGroupIds);
groups.addAll(linkedGroups);
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ private Optional<Group> doGetMyGroupById(String uid, String groupId, boolean try

if (teamsProvider.isTeamsGroup(groupId)) {
String localGroupId = extractLocalGroupId(groupId);
List<String> externalGroupIds = teamsProvider.linkedExternalGroupIds(localGroupId);
Set<String> externalGroupIds = teamsProvider.linkedExternalGroupIds(localGroupId);
List<Optional<Group>> externalGroups = externalGroupIds.stream()
.map(externalGroupId -> doGetMyGroupById(uid, externalGroupId, false))
.collect(toList());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/voot/api/VootController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
@RestController
public class VootController {

private static Logger LOG = LoggerFactory.getLogger(VootController.class);
private static final Logger LOG = LoggerFactory.getLogger(VootController.class);

private ExternalGroupsService externalGroupsService;
private final ExternalGroupsService externalGroupsService;

@Autowired
public VootController(ExternalGroupsService externalGroupsService) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/voot/model/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.ToString;

@ToString
@EqualsAndHashCode
@EqualsAndHashCode(of = {"id"})
public class Group {

public final String id;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/voot/model/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.ToString;

@ToString
@EqualsAndHashCode
@EqualsAndHashCode(of = {"id"})
public class Member {

public final String id;
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/voot/provider/EduIDGuestProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import voot.model.Membership;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

public class EduIDGuestProvider extends AbstractProvider {
Expand All @@ -30,11 +27,11 @@ public boolean shouldBeQueriedForMemberships(String schacHomeOrganization) {
}

@Override
public List<Group> getGroupMemberships(String uid) {
public Set<Group> getGroupMemberships(String uid) {
RequestEntity requestEntity = new RequestEntity(HttpMethod.GET, URI.create(String.format(groupMembershipsUrlTemplate, configuration.url, uid)));
ResponseEntity<List<Map<String, String>>> responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<Map<String, String>>>() {
});
return responseEntity.getBody().stream().map(this::parseGroup).collect(Collectors.toList());
return responseEntity.getBody().stream().map(this::parseGroup).collect(Collectors.toSet());
}

private Group parseGroup(Map<String, String> map) {
Expand All @@ -43,8 +40,8 @@ private Group parseGroup(Map<String, String> map) {
}

@Override
public List<Group> getAllGroups() {
return Collections.emptyList();
public Set<Group> getAllGroups() {
return Collections.emptySet();
}

@Override
Expand All @@ -53,12 +50,12 @@ public Optional<Group> getGroupMembership(String uid, String groupId) {
}

@Override
public List<Member> getMembers(String groupId) {
return Collections.emptyList();
public Set<Member> getMembers(String groupId) {
return Collections.emptySet();
}

@Override
public List<Member> getMembers(String personId, String groupId) {
return Collections.emptyList();
public Set<Member> getMembers(String personId, String groupId) {
return Collections.emptySet();
}
}
15 changes: 6 additions & 9 deletions src/main/java/voot/provider/OpenSocialClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import voot.model.Group;
import voot.model.Membership;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

public class OpenSocialClient extends Voot2Provider {
Expand All @@ -19,11 +16,11 @@ public OpenSocialClient(Configuration configuration) {
}

@Override
protected List<Group> parseGroups(String response) {
protected Set<Group> parseGroups(String response) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> groups = (List) parseJson(response, Map.class).get("entry");
if (CollectionUtils.isEmpty(groups)) {
return new ArrayList<>();
return Collections.emptySet();
}
return groups.stream().map(map -> {
Object idHolder = map.get("id");
Expand All @@ -35,13 +32,13 @@ protected List<Group> parseGroups(String response) {
(String) map.get("description"),
configuration.name,
new Membership((String) map.getOrDefault("voot_membership_role", "member")));
}).collect(Collectors.toList());
}).collect(Collectors.toSet());
}

@Override
protected Optional<Group> parseSingleGroup(String response) {
List<Group> groups = parseGroups(response);
return groups.isEmpty() ? Optional.empty() : Optional.of(groups.get(0));
Set<Group> groups = parseGroups(response);
return groups.isEmpty() ? Optional.empty() : Optional.of(groups.iterator().next());
}

}
11 changes: 6 additions & 5 deletions src/main/java/voot/provider/OpenSocialMembersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;

@SuppressWarnings("unchecked")
public class OpenSocialMembersClient extends OpenSocialClient {
Expand All @@ -26,7 +27,7 @@ public boolean shouldBeQueriedForMembers(String groupId) {
}

@Override
public List<Member> getMembers(String personId, String groupId) {
public Set<Member> getMembers(String personId, String groupId) {
LOG.debug("Querying getMembers for personId: {} and groupId: {}", personId, groupId);

String localGroupId = UrnUtils.extractLocalGroupId(groupId);
Expand All @@ -35,17 +36,17 @@ public List<Member> getMembers(String personId, String groupId) {
ResponseEntity<String> response = restTemplate.getForEntity(url,
String.class, localPersonId, localGroupId);

return handleResponse(response, this::parseMembers, "getMembers", emptyList());
return handleResponse(response, this::parseMembers, "getMembers", emptySet());
}

private List<Member> parseMembers(String body) {
private Set<Member> parseMembers(String body) {
List<Map<String, Object>> entries = (List<Map<String, Object>>) ((Map) parseJson(body, Map.class).get("result")).get("entry");
return entries.stream().map(map -> {
String id = (String) map.get("id");
String name = ((Map<String, String>) map.get("name")).get("formatted");
List<String> emails = (List<String>) map.get("emails");
return new Member(id, name, emails != null && !emails.isEmpty() ? emails.get(0) : null);
}).collect(Collectors.toList());
}).collect(Collectors.toSet());
}


Expand Down
10 changes: 5 additions & 5 deletions src/main/java/voot/provider/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import voot.model.Group;
import voot.model.Member;

import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface Provider {

Expand Down Expand Up @@ -32,12 +32,12 @@ public interface Provider {
/**
* @param uid the fully qualified uid
*/
List<Group> getGroupMemberships(String uid);
Set<Group> getGroupMemberships(String uid);

/**
* All groups
*/
List<Group> getAllGroups();
Set<Group> getAllGroups();

/**
* @param uid the fully qualified uid
Expand All @@ -52,7 +52,7 @@ public interface Provider {
* @param groupId the fully qualified uid groupId
* @return all Members
*/
List<Member> getMembers(String groupId);
Set<Member> getMembers(String groupId);

/**
* Get all members of a group
Expand All @@ -61,7 +61,7 @@ public interface Provider {
* @param groupId the fully qualified uid groupId
* @return all Members
*/
List<Member> getMembers(String personId, String groupId);
Set<Member> getMembers(String personId, String groupId);

/**
* Tells us if it is worthwhile calling this client when returning all members of a group
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/voot/provider/TeamsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import voot.model.Group;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface TeamsProvider extends Provider {

boolean isTeamsGroup(String groupId);

Optional<Group> findByLocalGroupId(String localGroupId);

List<Group> linkedLocalTeamsGroup(Collection<String> fullyQualifiedExternalGroupIds);
Set<Group> linkedLocalTeamsGroup(Collection<String> fullyQualifiedExternalGroupIds);

List<String> linkedExternalGroupIds(String localGroupId);
Set<String> linkedExternalGroupIds(String localGroupId);

}
37 changes: 18 additions & 19 deletions src/main/java/voot/provider/TeamsProviderClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import voot.util.UrnUtils;

import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static java.util.Collections.emptySet;

public class TeamsProviderClient extends Voot2Provider implements TeamsProvider {

Expand Down Expand Up @@ -42,41 +39,43 @@ public Optional<Group> findByLocalGroupId(String localGroupId) {
}

@Override
public List<Group> linkedLocalTeamsGroup(Collection<String> fullyQualifiedExternalGroupIds) {
public Set<Group> linkedLocalTeamsGroup(Collection<String> fullyQualifiedExternalGroupIds) {
//Make the externalGroupIdentifiers unique
HashSet<String> uniqueGroupIds = new HashSet<>(fullyQualifiedExternalGroupIds);
URI uri = UriComponentsBuilder.fromHttpUrl(String.format("%s/linked-locals", configuration.url))
.queryParam("externalGroupIds", String.join(",", fullyQualifiedExternalGroupIds))
.queryParam("externalGroupIds", String.join(",", uniqueGroupIds))
.build().encode().toUri();
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
return handleResponse(response, this::parseGroups, "findByLocalGroupId", emptyList());
return handleResponse(response, this::parseGroups, "findByLocalGroupId", emptySet());
}

@Override
@SuppressWarnings("unchecked")
public List<String> linkedExternalGroupIds(String localGroupId) {
public Set<String> linkedExternalGroupIds(String localGroupId) {
URI uri = UriComponentsBuilder.fromHttpUrl(String.format("%s/linked-externals", configuration.url))
.queryParam("teamId", localGroupId)
.build().encode().toUri();
ResponseEntity<List> response = restTemplate.getForEntity(uri, List.class);
return handleResponse(response, s -> s, "findByLocalGroupId", emptyList());
ResponseEntity<Set> response = restTemplate.getForEntity(uri, Set.class);
return handleResponse(response, s -> s, "findByLocalGroupId", emptySet());
}

@Override
public List<Member> getMembers(String personId, String groupId) {
public Set<Member> getMembers(String personId, String groupId) {
return getMembers(groupId);
}

@Override
public List<Member> getMembers(String groupId) {
public Set<Member> getMembers(String groupId) {
String uri = String.format("%s/members/{groupId}", configuration.url);
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class, groupId);
return handleResponse(response, this::parseMembers, "getMembers", emptyList());
return handleResponse(response, this::parseMembers, "getMembers", emptySet());
}

@Override
public List<Group> getAllGroups() {
public Set<Group> getAllGroups() {
String uri = String.format("%s/groups", configuration.url);
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
return handleResponse(response, this::parseGroups, "getAllGroups", emptyList());
return handleResponse(response, this::parseGroups, "getAllGroups", emptySet());
}

/**
Expand All @@ -88,9 +87,9 @@ protected String personUrnFromFullyQualifiedUrn(String uid) {
}

@SuppressWarnings("unchecked")
private List<Member> parseMembers(String response) {
private Set<Member> parseMembers(String response) {
List<Map<String, String>> maps = parseJson(response, List.class);
return maps.stream().map(map -> new Member(map.get("id"), map.get("name"), map.get("email"))).collect(toList());
return maps.stream().map(map -> new Member(map.get("id"), map.get("name"), map.get("email"))).collect(Collectors.toSet());
}

}
Loading

0 comments on commit 5e432d4

Please sign in to comment.