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