1
|
package eu.dnetlib.repo.manager.service;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.JsonNode;
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import eu.dnetlib.repo.manager.domain.BrokerException;
|
6
|
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
|
7
|
import eu.dnetlib.repo.manager.domain.Term;
|
8
|
import eu.dnetlib.repo.manager.domain.Tuple;
|
9
|
import eu.dnetlib.repo.manager.domain.broker.*;
|
10
|
import org.apache.commons.lang.NotImplementedException;
|
11
|
import org.json.JSONException;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.beans.factory.annotation.Value;
|
14
|
import org.springframework.core.ParameterizedTypeReference;
|
15
|
import org.springframework.http.*;
|
16
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
17
|
import org.springframework.stereotype.Service;
|
18
|
import org.springframework.util.LinkedMultiValueMap;
|
19
|
import org.springframework.util.MultiValueMap;
|
20
|
import org.springframework.web.client.RestClientException;
|
21
|
import org.springframework.web.client.RestTemplate;
|
22
|
import org.springframework.web.util.UriComponents;
|
23
|
import org.springframework.web.util.UriComponentsBuilder;
|
24
|
|
25
|
import javax.annotation.PostConstruct;
|
26
|
import java.io.IOException;
|
27
|
import java.io.InputStream;
|
28
|
import java.net.URL;
|
29
|
import java.util.*;
|
30
|
|
31
|
@Service("brokerService")
|
32
|
public class BrokerServiceImpl implements BrokerService {
|
33
|
|
34
|
@Autowired
|
35
|
private RepositoryServiceImpl repoAPI;
|
36
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
|
37
|
private String openairePath;
|
38
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
|
39
|
private String apiPath;
|
40
|
@Value("${topic_types.url}")
|
41
|
private String topicsURL;
|
42
|
|
43
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
44
|
.getLogger(BrokerServiceImpl.class);
|
45
|
|
46
|
@Autowired
|
47
|
RestTemplate restTemplate;
|
48
|
|
49
|
private HttpHeaders httpHeaders;
|
50
|
|
51
|
private HashMap<String, Term> topics = new HashMap<String, Term>();
|
52
|
|
53
|
@Autowired
|
54
|
private EmailUtils emailUtils;
|
55
|
|
56
|
@PostConstruct
|
57
|
private void initDnetTopicsMap() {
|
58
|
|
59
|
httpHeaders = new HttpHeaders();
|
60
|
httpHeaders.set("Content-Type", "application/json");
|
61
|
|
62
|
LOGGER.debug("Init dnet topics!");
|
63
|
try (InputStream is = new URL(topicsURL).openStream()) {
|
64
|
ObjectMapper mapper = new ObjectMapper();
|
65
|
JsonNode root = mapper.readTree(is);
|
66
|
for (JsonNode term : root.path("terms"))
|
67
|
topics.put(term.path("code").textValue(), parseTerm(term));
|
68
|
} catch (IOException e) {
|
69
|
LOGGER.debug("Exception on initDnetTopicsMap", e);
|
70
|
emailUtils.reportException(e);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
private Term parseTerm(JsonNode term) {
|
75
|
return new Term(term.path("englishName").textValue(), term.path("nativeName").textValue(),
|
76
|
term.path("encoding").textValue(), term.path("code").textValue());
|
77
|
}
|
78
|
|
79
|
|
80
|
@Override
|
81
|
public DatasourcesBroker getDatasourcesOfUser(String user, String includeShared, String includeByOthers) throws JSONException {
|
82
|
long start = System.currentTimeMillis();
|
83
|
DatasourcesBroker ret = new DatasourcesBroker();
|
84
|
try {
|
85
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(repoAPI.getRepositoriesSnippetsOfUser(user, "0", "100")));
|
86
|
//TODO fix bug when values are true
|
87
|
// if (Boolean.parseBoolean(includeShared)) {
|
88
|
// List<String> sharedDatasourceIds = new ArrayList<String>();
|
89
|
// ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
|
90
|
// }
|
91
|
|
92
|
// if (Boolean.parseBoolean(includeByOthers)) {
|
93
|
// ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
|
94
|
// }
|
95
|
} catch (Exception e) {
|
96
|
LOGGER.debug("Exception on getDatasourcesOfUser", e);
|
97
|
emailUtils.reportException(e);
|
98
|
}
|
99
|
long end = System.currentTimeMillis();
|
100
|
System.out.println("Getting datasources of user in " + (end - start) + "ms");
|
101
|
return ret;
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException {
|
106
|
final String service = "/topicsForDatasource";
|
107
|
|
108
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
109
|
.queryParam("ds", datasourceName);
|
110
|
|
111
|
ResponseEntity<List<BrowseEntry>> resp;
|
112
|
try {
|
113
|
resp = restTemplate.exchange(
|
114
|
builder.build().encode().toUri(),
|
115
|
HttpMethod.GET,
|
116
|
null,
|
117
|
new ParameterizedTypeReference<List<BrowseEntry>>() {
|
118
|
});
|
119
|
} catch (RestClientException e) {
|
120
|
LOGGER.debug("Exception on getTopicsForDatasource", e);
|
121
|
emailUtils.reportException(e);
|
122
|
throw new BrokerException(e);
|
123
|
}
|
124
|
|
125
|
return resp.getBody();
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public EventsPage advancedShowEvents(String page,
|
130
|
String size,
|
131
|
AdvQueryObject advQueryObject) throws BrokerException, JSONException, IOException {
|
132
|
|
133
|
final String service = "/events/{page}/{pageSize}";
|
134
|
|
135
|
Map<String, Long> uriParams = new HashMap<>();
|
136
|
uriParams.put("page", Long.parseLong(page));
|
137
|
uriParams.put("pageSize", Long.parseLong(size));
|
138
|
|
139
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
140
|
|
141
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
142
|
advQueryObject.setPage(Long.parseLong(page));
|
143
|
HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, httpHeaders);
|
144
|
ResponseEntity<EventsPage> resp;
|
145
|
try {
|
146
|
resp = restTemplate.exchange(
|
147
|
builder.buildAndExpand(uriParams).encode().toUri(),
|
148
|
HttpMethod.POST,
|
149
|
entity,
|
150
|
new ParameterizedTypeReference<EventsPage>() {
|
151
|
}
|
152
|
);
|
153
|
} catch (RestClientException e) {
|
154
|
LOGGER.debug("Exception on advancedShowEvents", e);
|
155
|
emailUtils.reportException(e);
|
156
|
throw new BrokerException(e);
|
157
|
}
|
158
|
return resp.getBody();
|
159
|
|
160
|
|
161
|
}
|
162
|
|
163
|
|
164
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<RepositorySnippet> repositories) throws BrokerException {
|
165
|
long start = System.currentTimeMillis();
|
166
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
167
|
for (RepositorySnippet repo : repositories) {
|
168
|
BrowseEntry temp = new BrowseEntry();
|
169
|
temp.setValue(repo.getOfficialname());
|
170
|
temp.setSize(new Long(0));
|
171
|
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) {
|
172
|
temp.setSize(temp.getSize() + e.getSize());
|
173
|
}
|
174
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
175
|
entries.add(tup);
|
176
|
}
|
177
|
|
178
|
// sort the collection by the second field of the tuple which is size
|
179
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
180
|
@Override
|
181
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
182
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
183
|
}
|
184
|
});
|
185
|
long stop = System.currentTimeMillis();
|
186
|
System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms ");
|
187
|
|
188
|
return entries;
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public EventsPage showEvents(String datasourceName,
|
193
|
String topic,
|
194
|
String page,
|
195
|
String size) throws BrokerException, JSONException {
|
196
|
|
197
|
final String service = "/events";
|
198
|
|
199
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
200
|
.queryParam("ds", datasourceName)
|
201
|
.queryParam("topic", topic)
|
202
|
.path("/{page}/{size}/");
|
203
|
|
204
|
ResponseEntity<EventsPage> resp;
|
205
|
try {
|
206
|
resp = restTemplate.exchange(
|
207
|
builder.build().expand(page, size).encode().toUri(),
|
208
|
HttpMethod.GET,
|
209
|
null,
|
210
|
new ParameterizedTypeReference<EventsPage>() {
|
211
|
});
|
212
|
} catch (RestClientException e) {
|
213
|
LOGGER.debug("Exception on showEvents", e);
|
214
|
emailUtils.reportException(e);
|
215
|
throw new BrokerException(e);
|
216
|
}
|
217
|
return resp.getBody();
|
218
|
}
|
219
|
|
220
|
@Override
|
221
|
public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(String userEmail)
|
222
|
throws BrokerException {
|
223
|
|
224
|
final String service = "/subscriptions";
|
225
|
|
226
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
|
227
|
.queryParam("email", userEmail);
|
228
|
|
229
|
LOGGER.debug(builder.build().encode().toUri());
|
230
|
ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
|
231
|
try {
|
232
|
resp = restTemplate.exchange(
|
233
|
builder.build().encode().toUri(),
|
234
|
HttpMethod.GET,
|
235
|
null,
|
236
|
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
|
237
|
});
|
238
|
} catch (RestClientException e) {
|
239
|
LOGGER.debug("Exception on getSimpleSubscriptionsOfUser", e);
|
240
|
emailUtils.reportException(e);
|
241
|
throw new BrokerException(e);
|
242
|
}
|
243
|
return resp.getBody();
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUserByRepoId(String userEmail, String repoId) throws BrokerException {
|
248
|
Map<String, List<SimpleSubscriptionDesc>> subscriptionsOfUser = getSimpleSubscriptionsOfUser(userEmail);
|
249
|
throw new NotImplementedException();
|
250
|
// return null;
|
251
|
}
|
252
|
|
253
|
@Override
|
254
|
public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
|
255
|
final String service = "/subscribe";
|
256
|
|
257
|
//build the uri params
|
258
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
|
259
|
|
260
|
HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
|
261
|
|
262
|
//create new template engine
|
263
|
RestTemplate template = new RestTemplate();
|
264
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
265
|
ResponseEntity<Subscription> resp;
|
266
|
try {
|
267
|
//communicate with endpoint
|
268
|
resp = restTemplate.exchange(
|
269
|
builder.build().encode().toUri(),
|
270
|
HttpMethod.POST,
|
271
|
entity,
|
272
|
new ParameterizedTypeReference<Subscription>() {
|
273
|
});
|
274
|
} catch (RestClientException e) {
|
275
|
LOGGER.debug("Exception on OpenaireSubscription", e);
|
276
|
emailUtils.reportException(e);
|
277
|
throw new BrokerException(e);
|
278
|
}
|
279
|
|
280
|
return resp.getBody();
|
281
|
}
|
282
|
|
283
|
@Override
|
284
|
public ResponseEntity<Object> unsubscribe(String subscriptionId) throws BrokerException {
|
285
|
final String service = "/subscriptions/" + subscriptionId;
|
286
|
|
287
|
//build the uri params
|
288
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
289
|
|
290
|
try {
|
291
|
//communicate with endpoint
|
292
|
restTemplate.exchange(
|
293
|
builder.build().encode().toUri(),
|
294
|
HttpMethod.DELETE,
|
295
|
null,
|
296
|
new ParameterizedTypeReference<Void>() {
|
297
|
});
|
298
|
} catch (RestClientException e) {
|
299
|
LOGGER.debug("Exception on unsubscribe", e);
|
300
|
emailUtils.reportException(e);
|
301
|
throw new BrokerException(e);
|
302
|
}
|
303
|
return new ResponseEntity<>("OK", HttpStatus.OK);
|
304
|
}
|
305
|
|
306
|
@Override
|
307
|
public Subscription getSubscription(String subscriptionId) throws BrokerException {
|
308
|
final String service = "/subscriptions/" + subscriptionId;
|
309
|
|
310
|
//build the uri params
|
311
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
312
|
|
313
|
ResponseEntity<Subscription> resp;
|
314
|
try {
|
315
|
//communicate with endpoint
|
316
|
resp = restTemplate.exchange(
|
317
|
builder.build().encode().toUri(),
|
318
|
HttpMethod.GET,
|
319
|
null,
|
320
|
new ParameterizedTypeReference<Subscription>() {
|
321
|
});
|
322
|
} catch (RestClientException e) {
|
323
|
LOGGER.debug("Exception on getSubscription", e);
|
324
|
emailUtils.reportException(e);
|
325
|
throw new BrokerException(e);
|
326
|
}
|
327
|
return resp.getBody();
|
328
|
}
|
329
|
|
330
|
@Override
|
331
|
public Map<String, Term> getDnetTopics() throws BrokerException {
|
332
|
return topics;
|
333
|
}
|
334
|
|
335
|
@Override
|
336
|
public EventsPage getNotificationsBySubscriptionId(String subscriptionId,
|
337
|
String page,
|
338
|
String size) throws BrokerException {
|
339
|
|
340
|
UriComponents uriComponents = UriComponentsBuilder
|
341
|
.fromHttpUrl(openairePath + "/notifications/")
|
342
|
.path("/{id}/{page}/{size}/")
|
343
|
.build().expand(subscriptionId, page, size).encode();
|
344
|
|
345
|
ResponseEntity<EventsPage> resp;
|
346
|
try {
|
347
|
resp = restTemplate.exchange(
|
348
|
uriComponents.toUri(),
|
349
|
HttpMethod.GET,
|
350
|
null,
|
351
|
new ParameterizedTypeReference<EventsPage>() {
|
352
|
});
|
353
|
} catch (RestClientException e) {
|
354
|
LOGGER.debug("Exception on getNotificationsBySubscriptionId", e);
|
355
|
emailUtils.reportException(e);
|
356
|
throw new BrokerException(e);
|
357
|
}
|
358
|
return resp.getBody();
|
359
|
}
|
360
|
|
361
|
//@Override
|
362
|
public Map<String, List<Subscription>> getSubscriptionsOfUser(String userEmail)
|
363
|
throws BrokerException {
|
364
|
|
365
|
Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
|
366
|
Map<String, List<Subscription>> subs = new HashMap<>();
|
367
|
List<Subscription> subscriptions = null;
|
368
|
|
369
|
for (String s : simpleSubs.keySet()) {
|
370
|
List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
|
371
|
for (SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
|
372
|
subscriptions = new ArrayList<>();
|
373
|
subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
|
374
|
}
|
375
|
subs.put(s, subscriptions);
|
376
|
}
|
377
|
return subs;
|
378
|
}
|
379
|
|
380
|
|
381
|
}
|