Project

General

Profile

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