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