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