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