This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MongoConnectionRepository.java
233 lines (202 loc) · 8 KB
/
MongoConnectionRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.connect.mongo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.DuplicateConnectionException;
import org.springframework.social.connect.NoSuchConnectionException;
import org.springframework.social.connect.NotConnectedException;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
class MongoConnectionRepository implements ConnectionRepository {
private final String userId;
private final ConnectionService connService;
private final ConnectionFactoryLocator connectionFactoryLocator;
//private final TextEncryptor textEncryptor;
public MongoConnectionRepository(String userId,
ConnectionService connectionService,
ConnectionFactoryLocator connectionFactoryLocator,
TextEncryptor textEncryptor) {
this.userId = userId;
this.connService = connectionService;
this.connectionFactoryLocator = connectionFactoryLocator;
//this.textEncryptor = textEncryptor;
//this.connectionMapper = new ConnectionMapper(connectionFactoryLocator, textEncryptor);
}
// private String encrypt(String text) {
// return text != null ? textEncryptor.encrypt(text) : text;
// }
/**
* Add a new connection to this repository for the current user.
*/
@Override
public void addConnection(Connection<?> connection) {
try {
ConnectionData data = connection.createData();
int rank = connService.getMaxRank(userId, data.getProviderId());
connService.create(userId, connection, rank);
} catch (DuplicateKeyException e) {
throw new DuplicateConnectionException(connection.getKey());
}
}
/**
* Find all connections the current user has across all providers
*/
@Override
public MultiValueMap<String, Connection<?>> findAllConnections() {
List<Connection<?>> resultList = connService.getConnections(this.userId);
MultiValueMap<String, Connection<?>> connections = new LinkedMultiValueMap<String, Connection<?>>();
Set<String> registeredProviderIds = this.connectionFactoryLocator.registeredProviderIds();
for (String registeredProviderId : registeredProviderIds) {
connections.put(registeredProviderId, Collections.<Connection<?>>emptyList());
}
for (Connection<?> connection : resultList) {
String providerId = connection.getKey().getProviderId();
if (connections.get(providerId).size() == 0) {
connections.put(providerId, new LinkedList<Connection<?>>());
}
connections.add(providerId, connection);
}
return connections;
}
/**
* Find the connections the current user has to the provider of the given API
*/
@SuppressWarnings("unchecked")
@Override
public <A> List<Connection<A>> findConnections(Class<A> apiType) {
List<?> connections = findConnections(getProviderId(apiType));
return (List<Connection<A>>) connections;
}
/**
* Find the connections the current user has to the provider registered by the given id
*/
@Override
public List<Connection<?>> findConnections(String providerId) {
return connService.getConnections(this.userId, providerId);
}
/**
* Find the connections the current user has to the given provider users.
*/
@Override
public MultiValueMap<String, Connection<?>> findConnectionsToUsers(MultiValueMap<String, String> providerUsers) {
if (providerUsers == null || providerUsers.isEmpty()) {
throw new IllegalArgumentException("Unable to execute find: no providerUsers provided");
}
List<Connection<?>> resultList = connService.getConnections(userId, providerUsers);
MultiValueMap<String, Connection<?>> connectionsForUsers = new LinkedMultiValueMap<String, Connection<?>>();
for (Connection<?> connection : resultList) {
String providerId = connection.getKey().getProviderId();
List<String> userIds = providerUsers.get(providerId);
List<Connection<?>> connections = connectionsForUsers.get(providerId);
if (connections == null) {
connections = new ArrayList<Connection<?>>(userIds.size());
for (int i = 0; i < userIds.size(); i++) {
connections.add(null);
}
connectionsForUsers.put(providerId, connections);
}
String providerUserId = connection.getKey().getProviderUserId();
int connectionIndex = userIds.indexOf(providerUserId);
connections.set(connectionIndex, connection);
}
return connectionsForUsers;
}
/**
* Get a connection for the current user by its key
*/
@Override
public Connection<?> getConnection(ConnectionKey connectionKey) {
try {
return connService.getConnection(userId,
connectionKey.getProviderId(),
connectionKey.getProviderUserId());
} catch (EmptyResultDataAccessException e) {
throw new NoSuchConnectionException(connectionKey);
}
}
/**
* Get a connection between the current user and the given provider user.
*/
@Override
@SuppressWarnings("unchecked")
public <A> Connection<A> getConnection(Class<A> apiType, String providerUserId) {
String providerId = getProviderId(apiType);
return (Connection<A>) getConnection(new ConnectionKey(providerId, providerUserId));
}
/**
* Get the "primary" connection the current user has to the provider of the given API.
*/
@Override
@SuppressWarnings("unchecked")
public <A> Connection<A> getPrimaryConnection(Class<A> apiType) {
String providerId = getProviderId(apiType);
Connection<A> connection = (Connection<A>) findPrimaryConnection(providerId);
if (connection == null) {
throw new NotConnectedException(providerId);
}
return connection;
}
/**
* Find the "primary" connection the current user has to the provider of the given API
*/
@Override
@SuppressWarnings("unchecked")
public <A> Connection<A> findPrimaryConnection(Class<A> apiType) {
String providerId = getProviderId(apiType);
return (Connection<A>) findPrimaryConnection(providerId);
}
/**
* Update a Connection already added to this repository.
*/
@Override
public void updateConnection(Connection<?> connection) {
connService.update(userId, connection);
}
/**
* Remove all Connections between the current user and the provider from this repository.
*/
@Override
public void removeConnections(String providerId) {
connService.remove(userId, providerId);
}
/**
* Remove a single Connection for the current user from this repository.
*/
@Override
public void removeConnection(ConnectionKey connectionKey) {
connService.remove(userId, connectionKey);
}
// helper methods
private <A> String getProviderId(Class<A> apiType) {
return connectionFactoryLocator.getConnectionFactory(apiType).getProviderId();
}
private Connection<?> findPrimaryConnection(String providerId) {
// where userId = ? and providerId = ? and rank = 1
return connService.getPrimaryConnection(userId, providerId);
}
}