1
|
package eu.dnetlib.repo.manager.server.services;
|
2
|
|
3
|
import eu.dnetlib.domain.data.Repository;
|
4
|
import eu.dnetlib.gwt.server.service.SpringGwtRemoteServiceServlet;
|
5
|
import eu.dnetlib.repo.manager.client.services.BrokerService;
|
6
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
7
|
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
|
8
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
9
|
import eu.dnetlib.repo.manager.shared.broker.*;
|
10
|
import eu.dnetlib.repos.RepoApi;
|
11
|
import org.apache.log4j.Logger;
|
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.HttpMethod;
|
17
|
import org.springframework.http.ResponseEntity;
|
18
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
19
|
import org.springframework.stereotype.Service;
|
20
|
import org.springframework.util.LinkedMultiValueMap;
|
21
|
import org.springframework.util.MultiValueMap;
|
22
|
import org.springframework.web.client.RestClientException;
|
23
|
import org.springframework.web.client.RestTemplate;
|
24
|
import org.springframework.web.util.UriComponentsBuilder;
|
25
|
|
26
|
import javax.servlet.ServletConfig;
|
27
|
import javax.servlet.ServletException;
|
28
|
import java.util.*;
|
29
|
|
30
|
/**
|
31
|
* Created by stefanos on 10/26/16.
|
32
|
*/
|
33
|
@SuppressWarnings("serial")
|
34
|
@Service("brokerService")
|
35
|
public class BrokerServiceImpl extends SpringGwtRemoteServiceServlet implements BrokerService {
|
36
|
|
37
|
private static final Logger LOGGER = Logger
|
38
|
.getLogger(BrokerServiceImpl.class);
|
39
|
|
40
|
@Autowired
|
41
|
private RepoApi repoAPI;
|
42
|
|
43
|
@Override
|
44
|
public void init(ServletConfig config) throws ServletException {
|
45
|
super.init(config);
|
46
|
LOGGER.info("broker service init");
|
47
|
}
|
48
|
|
49
|
@Value("${services.broker.url}:${services.broker.port}/${services.broker.path}")
|
50
|
private String apiPath;
|
51
|
|
52
|
/**
|
53
|
* @param datasourceName the name of the data source
|
54
|
* @return a list of BrowseEvent entries for that datasource
|
55
|
* @throws BrokerException containing the error code from the server
|
56
|
* @author stefanos
|
57
|
*/
|
58
|
@Override
|
59
|
public List<BrowseEntry> getTopicsForDatasource(String datasourceName) throws BrokerException {
|
60
|
final String service = "/topicsForDatasource";
|
61
|
|
62
|
//build the uri params
|
63
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service)
|
64
|
.queryParam("ds", datasourceName);
|
65
|
|
66
|
//create new template engine
|
67
|
RestTemplate template = new RestTemplate();
|
68
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
69
|
ResponseEntity<List<BrowseEntry>> resp;
|
70
|
try {
|
71
|
//communicate with endpoint
|
72
|
resp = template.exchange(
|
73
|
builder.build().encode().toUri(),
|
74
|
HttpMethod.GET,
|
75
|
null,
|
76
|
new ParameterizedTypeReference<List<BrowseEntry>>() {
|
77
|
});
|
78
|
} catch (RestClientException e) {
|
79
|
throw new BrokerException(e);
|
80
|
}
|
81
|
|
82
|
return resp.getBody();
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @param datasourceName the name of the datasource
|
87
|
* @param topic the name of the topic to filter
|
88
|
* @param page the page number
|
89
|
* @return an Events page with a constant 50 max number of entires
|
90
|
* @throws BrokerException containing the error code from the server
|
91
|
*/
|
92
|
@Override
|
93
|
public EventsPage showEvents(String datasourceName, String topic, long page) throws BrokerException {
|
94
|
final String service = "/showEvents";
|
95
|
|
96
|
//build the uri params
|
97
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service)
|
98
|
.queryParam("ds", datasourceName)
|
99
|
.queryParam("topic", topic)
|
100
|
.queryParam("page", page);
|
101
|
|
102
|
//create new template engine
|
103
|
RestTemplate template = new RestTemplate();
|
104
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
105
|
ResponseEntity<EventsPage> resp;
|
106
|
try {
|
107
|
//communicate with endpoint
|
108
|
resp = template.exchange(
|
109
|
builder.build().encode().toUri(),
|
110
|
HttpMethod.GET,
|
111
|
null,
|
112
|
new ParameterizedTypeReference<EventsPage>() {
|
113
|
});
|
114
|
} catch (RestClientException e) {
|
115
|
throw new BrokerException(e);
|
116
|
}
|
117
|
return resp.getBody();
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* @param advQueryObject a pojo class containing the filter parameters
|
122
|
* @param page the number of the page
|
123
|
* @param pageSize the page size
|
124
|
* @return
|
125
|
* @throws BrokerException
|
126
|
*/
|
127
|
@Override
|
128
|
public EventsPage advancedShowEvents(AdvQueryObject advQueryObject, long page, long pageSize) throws BrokerException {
|
129
|
final String service = "/events/{page}/{pageSize}";
|
130
|
|
131
|
// URI (URL) parameters
|
132
|
Map<String, Long> uriParams = new HashMap<>();
|
133
|
uriParams.put("page", page);
|
134
|
uriParams.put("pageSize", pageSize);
|
135
|
|
136
|
//build the uri params
|
137
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
138
|
|
139
|
//Header info
|
140
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
141
|
headers.add("Content-Type", "application/json");
|
142
|
|
143
|
advQueryObject.setPage(page);
|
144
|
|
145
|
HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, headers);
|
146
|
|
147
|
//create new template engine
|
148
|
RestTemplate template = new RestTemplate();
|
149
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
150
|
ResponseEntity<EventsPage> resp;
|
151
|
try {
|
152
|
//communicate with endpoint
|
153
|
resp = template.exchange(
|
154
|
builder.buildAndExpand(uriParams).encode().toUri(),
|
155
|
HttpMethod.POST,
|
156
|
entity,
|
157
|
new ParameterizedTypeReference<EventsPage>() {
|
158
|
}
|
159
|
);
|
160
|
} catch (RestClientException e) {
|
161
|
throw new BrokerException(e);
|
162
|
}
|
163
|
return resp.getBody();
|
164
|
}
|
165
|
|
166
|
@Override
|
167
|
public DatasourcesBroker getDatasourcesOfUser(String userEmail, boolean includeShared, boolean includeByOthers) throws BrokerException {
|
168
|
DatasourcesBroker ret = new DatasourcesBroker();
|
169
|
try {
|
170
|
LOGGER.debug("In getDatasourcesOfUser");
|
171
|
|
172
|
//set for user
|
173
|
ret.setDatasourcesOfUser(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, false)));
|
174
|
|
175
|
//set for shared
|
176
|
if (includeShared) {
|
177
|
//TODO whatever nikonas was saying
|
178
|
List<String> sharedDatasourceIds = new ArrayList<String>();
|
179
|
ret.setSharedDatasources(getDatasourcesOfUserType(this.repoAPI.getReposByIds(sharedDatasourceIds)));
|
180
|
}
|
181
|
|
182
|
//set others
|
183
|
if (includeByOthers) {
|
184
|
ret.setDatasourcesOfOthers(getDatasourcesOfUserType(this.repoAPI.getRepositoriesOfUser(userEmail, true)));
|
185
|
}
|
186
|
|
187
|
} catch (RepositoryServiceException e) {
|
188
|
e.printStackTrace();
|
189
|
throw new BrokerException(e);
|
190
|
} catch (Exception e) {
|
191
|
e.printStackTrace();
|
192
|
}
|
193
|
return ret;
|
194
|
}
|
195
|
|
196
|
@Override
|
197
|
public Map<String, List<SimpleSubscriptionDesc>> getSubscriptionsOfUser(String userEmail) throws BrokerException {
|
198
|
final String service = "/subscriptions";
|
199
|
|
200
|
//build the uri params
|
201
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service)
|
202
|
.queryParam("email", userEmail);
|
203
|
|
204
|
//create new template engine
|
205
|
RestTemplate template = new RestTemplate();
|
206
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
207
|
ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
|
208
|
try {
|
209
|
//communicate with endpoint
|
210
|
resp = template.exchange(
|
211
|
builder.build().encode().toUri(),
|
212
|
HttpMethod.GET,
|
213
|
null,
|
214
|
new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
|
215
|
});
|
216
|
} catch (RestClientException e) {
|
217
|
throw new BrokerException(e);
|
218
|
}
|
219
|
|
220
|
return resp.getBody();
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
|
225
|
final String service = "/subscribe";
|
226
|
|
227
|
//build the uri params
|
228
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
|
229
|
|
230
|
//Header info
|
231
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
232
|
headers.add("Content-Type", "application/json");
|
233
|
HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, headers);
|
234
|
|
235
|
//create new template engine
|
236
|
RestTemplate template = new RestTemplate();
|
237
|
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
238
|
ResponseEntity<Subscription> resp;
|
239
|
try {
|
240
|
//communicate with endpoint
|
241
|
resp = template.exchange(
|
242
|
builder.build().encode().toUri(),
|
243
|
HttpMethod.GET,
|
244
|
entity,
|
245
|
new ParameterizedTypeReference<Subscription>() {
|
246
|
});
|
247
|
} catch (RestClientException e) {
|
248
|
throw new BrokerException(e);
|
249
|
}
|
250
|
|
251
|
return resp.getBody();
|
252
|
}
|
253
|
|
254
|
@Override
|
255
|
public void unsubscribe(String subscriptionId) throws BrokerException {
|
256
|
throw new BrokerException(new Exception("Not implemented"));
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Helper class to aggregate the datasources topic sizes by datasource name.
|
261
|
*
|
262
|
* @param repositories to be aggregated
|
263
|
* @return a List of BrowseEntry with the official name and the number of topics for each repo and logo url
|
264
|
* @throws BrokerException
|
265
|
*/
|
266
|
private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
|
267
|
//get user entries
|
268
|
LOGGER.debug("getDatasourcesOfUserType : " + repositories.size());
|
269
|
List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
|
270
|
for (Repository repo : repositories) {
|
271
|
BrowseEntry temp = new BrowseEntry();
|
272
|
temp.setValue(repo.getOfficialName());
|
273
|
temp.setSize(new Long(0));
|
274
|
for (BrowseEntry e : this.getTopicsForDatasource(repo.getOfficialName())) {
|
275
|
temp.setSize(temp.getSize() + e.getSize());
|
276
|
}
|
277
|
Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
|
278
|
entries.add(tup);
|
279
|
}
|
280
|
|
281
|
// sort the collection by the second field of the tuple which is size
|
282
|
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
|
283
|
@Override
|
284
|
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
|
285
|
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
|
286
|
}
|
287
|
});
|
288
|
|
289
|
return entries;
|
290
|
}
|
291
|
}
|