1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth2.provider.approval;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
25
26
27
28
29 public class InMemoryApprovalStore implements ApprovalStore {
30
31 private ConcurrentMap<Key, Collection<Approval>> map = new ConcurrentHashMap<Key, Collection<Approval>>();
32
33 @Override
34 public boolean addApprovals(Collection<Approval> approvals) {
35 for (Approval approval : approvals) {
36 Collection<Approval> collection = getApprovals(approval);
37 collection.add(approval);
38 }
39 return true;
40 }
41
42 @Override
43 public boolean revokeApprovals(Collection<Approval> approvals) {
44 boolean success = true;
45 for (Approval approval : approvals) {
46 Collection<Approval> collection = getApprovals(approval);
47 boolean removed = collection.remove(approval);
48 if (!removed) {
49 success = false;
50 }
51 }
52 return success;
53 }
54
55 private Collection<Approval> getApprovals(Approval approval) {
56 Key key = new Key(approval.getUserId(), approval.getClientId());
57 if (!map.containsKey(key)) {
58 map.putIfAbsent(key, new HashSet<Approval>());
59 }
60 return map.get(key);
61 }
62
63 @Override
64 public Collection<Approval> getApprovals(String userId, String clientId) {
65 Approval approval = new Approval();
66 approval.setUserId(userId);
67 approval.setClientId(clientId);
68 return Collections.unmodifiableCollection(getApprovals(approval));
69 }
70
71 public void clear() {
72 map.clear();
73 }
74
75 private static class Key {
76
77 String userId;
78
79 String clientId;
80
81 public Key(String userId, String clientId) {
82 this.userId = userId;
83 this.clientId = clientId;
84 }
85
86 @Override
87 public int hashCode() {
88 final int prime = 31;
89 int result = 1;
90 result = prime * result + ((clientId == null) ? 0 : clientId.hashCode());
91 result = prime * result + ((userId == null) ? 0 : userId.hashCode());
92 return result;
93 }
94
95 @Override
96 public boolean equals(Object obj) {
97 if (this == obj)
98 return true;
99 if (obj == null)
100 return false;
101 if (getClass() != obj.getClass())
102 return false;
103 Key other = (Key) obj;
104 if (clientId == null) {
105 if (other.clientId != null)
106 return false;
107 }
108 else if (!clientId.equals(other.clientId))
109 return false;
110 if (userId == null) {
111 if (other.userId != null)
112 return false;
113 }
114 else if (!userId.equals(other.userId))
115 return false;
116 return true;
117 }
118
119 }
120
121 }