|
16 | 16 |
|
17 | 17 | import org.eclipse.edc.iam.identitytrust.sts.spi.model.StsClient; |
18 | 18 | import org.eclipse.edc.iam.identitytrust.sts.spi.store.StsClientStore; |
| 19 | +import org.eclipse.edc.spi.query.CriterionOperatorRegistry; |
| 20 | +import org.eclipse.edc.spi.query.QueryResolver; |
| 21 | +import org.eclipse.edc.spi.query.QuerySpec; |
19 | 22 | import org.eclipse.edc.spi.result.StoreResult; |
| 23 | +import org.eclipse.edc.store.ReflectionBasedQueryResolver; |
| 24 | +import org.jetbrains.annotations.NotNull; |
20 | 25 |
|
21 | 26 | import java.util.Map; |
22 | 27 | import java.util.Optional; |
23 | 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | +import java.util.stream.Stream; |
24 | 30 |
|
25 | 31 | import static java.lang.String.format; |
26 | 32 |
|
|
29 | 35 | */ |
30 | 36 | public class InMemoryStsClientStore implements StsClientStore { |
31 | 37 |
|
| 38 | + // we store it by clientId |
32 | 39 | private final Map<String, StsClient> clients = new ConcurrentHashMap<>(); |
| 40 | + private final QueryResolver<StsClient> queryResolver; |
| 41 | + |
| 42 | + |
| 43 | + public InMemoryStsClientStore(CriterionOperatorRegistry criterionOperatorRegistry) { |
| 44 | + queryResolver = new ReflectionBasedQueryResolver<>(StsClient.class, criterionOperatorRegistry); |
| 45 | + } |
33 | 46 |
|
34 | 47 | @Override |
35 | 48 | public StoreResult<StsClient> create(StsClient client) { |
36 | 49 | return Optional.ofNullable(clients.putIfAbsent(client.getClientId(), client)) |
37 | | - .map(old -> StoreResult.<StsClient>alreadyExists(format("Client with id %s already exists", client.getClientId()))) |
| 50 | + .map(old -> StoreResult.<StsClient>alreadyExists(format(CLIENT_EXISTS_TEMPLATE, client.getClientId()))) |
38 | 51 | .orElseGet(() -> StoreResult.success(client)); |
39 | 52 | } |
40 | 53 |
|
41 | 54 | @Override |
42 | | - public StoreResult<StsClient> findByClientId(String id) { |
43 | | - return Optional.ofNullable(clients.get(id)) |
| 55 | + public StoreResult<Void> update(StsClient stsClient) { |
| 56 | + var prev = clients.replace(stsClient.getClientId(), stsClient); |
| 57 | + return Optional.ofNullable(prev) |
| 58 | + .map(a -> StoreResult.<Void>success()) |
| 59 | + .orElse(StoreResult.notFound(format(CLIENT_NOT_FOUND_BY_ID_TEMPLATE, stsClient.getId()))); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public @NotNull Stream<StsClient> findAll(QuerySpec spec) { |
| 64 | + return queryResolver.query(clients.values().stream(), spec); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public StoreResult<StsClient> findById(String id) { |
| 69 | + return clients.values().stream() |
| 70 | + .filter(client -> client.getId().equals(id)) |
| 71 | + .findFirst() |
| 72 | + .map(StoreResult::success) |
| 73 | + .orElseGet(() -> StoreResult.notFound(format(CLIENT_NOT_FOUND_BY_ID_TEMPLATE, id))); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public StoreResult<StsClient> findByClientId(String clientId) { |
| 78 | + return Optional.ofNullable(clients.get(clientId)) |
44 | 79 | .map(StoreResult::success) |
45 | | - .orElseGet(() -> StoreResult.notFound(format("Client with id %s not found.", id))); |
| 80 | + .orElseGet(() -> StoreResult.notFound(format(CLIENT_NOT_FOUND_BY_CLIENT_ID_TEMPLATE, clientId))); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public StoreResult<StsClient> deleteById(String id) { |
| 85 | + return findById(id) |
| 86 | + .onSuccess(client -> clients.remove(client.getClientId())); |
46 | 87 | } |
47 | 88 | } |
0 commit comments