Project

General

Profile

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 io.swagger.annotations.ApiParam;
11
import org.json.JSONException;
12
import org.json.JSONObject;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Value;
15
import org.springframework.core.ParameterizedTypeReference;
16
import org.springframework.http.HttpEntity;
17
import org.springframework.http.HttpHeaders;
18
import org.springframework.http.HttpMethod;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
21
import org.springframework.security.access.prepost.PreAuthorize;
22
import org.springframework.stereotype.Component;
23
import org.springframework.util.LinkedMultiValueMap;
24
import org.springframework.util.MultiValueMap;
25
import org.springframework.web.bind.annotation.PathVariable;
26
import org.springframework.web.bind.annotation.RequestBody;
27
import org.springframework.web.bind.annotation.RequestParam;
28
import org.springframework.web.client.RestClientException;
29
import org.springframework.web.client.RestTemplate;
30
import org.springframework.web.util.UriComponents;
31
import org.springframework.web.util.UriComponentsBuilder;
32

    
33
import javax.annotation.PostConstruct;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.net.URL;
37
import java.util.*;
38

    
39
@Component
40
public class BrokerApiImpl implements BrokerApi {
41

    
42
    @Autowired
43
    private RepositoryApiImpl repoAPI;
44
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
45
    private String openairePath;
46
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
47
    private String apiPath;
48
    @Value("${topic_types.url}")
49
    private String topicsURL;
50

    
51
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
52
            .getLogger(BrokerApiImpl.class);
53

    
54
    private RestTemplate restTemplate = null;
55

    
56
    private HttpHeaders httpHeaders;
57

    
58
    private HashMap<String,Term> topics = new HashMap<String, Term>();
59

    
60
    @PostConstruct
61
    private void initDnetTopicsMap() {
62

    
63
        restTemplate = new RestTemplate();
64
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
65

    
66
        httpHeaders = new HttpHeaders();
67
        httpHeaders.set("Content-Type", "application/json");
68

    
69
        LOGGER.debug("Init dnet topics!");
70
        InputStream is = null;
71
        try {
72
            is = new URL(topicsURL).openStream();
73
            ObjectMapper mapper = new ObjectMapper();
74
            JsonNode root = mapper.readTree(is);
75
            for (JsonNode term : root.path("terms") )
76
                topics.put(term.path("code").textValue(), parseTerm(term));
77
        } catch (IOException e) {
78
            LOGGER.debug(e);
79
            e.printStackTrace();
80
        }
81
    }
82

    
83
    private Term parseTerm(JsonNode term) {
84
        return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
85
                term.path("encoding").textValue(),term.path("code").textValue());
86
    }
87

    
88

    
89
    @Override
90
    @PreAuthorize("hasRole('ROLE_USER')")
91
    public DatasourcesBroker getDatasourcesOfUser(@RequestParam("user")
92
                                                    @ApiParam(value = "User email", required = true) String user,
93
                                                  @RequestParam("includeShared")
94
                                                    @ApiParam(value = "Include shared datasources", required = true , defaultValue = "false") String includeShared,
95
                                                  @RequestParam("includeByOthers")
96
                                                    @ApiParam(value = "Include datasources of other", required = true,defaultValue = "false") String includeByOthers) throws JSONException {
97

    
98
        DatasourcesBroker ret = new DatasourcesBroker();
99
        try {
100
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
101
            //TODO fix bug when values are true
102
            if (Boolean.parseBoolean(includeShared)) {
103
                List<String> sharedDatasourceIds = new ArrayList<String>();
104
                ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
105
            }
106

    
107
            if (Boolean.parseBoolean(includeByOthers)) {
108
                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
109
            }
110
        } catch (BrokerException e) {
111
            e.printStackTrace();
112
        }
113

    
114
        return ret;
115
    }
116

    
117
    @Override
118
    public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName")  String datasourceName) throws BrokerException {
119
        final String service = "/topicsForDatasource";
120

    
121
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
122
                .queryParam("ds", datasourceName);
123

    
124
        ResponseEntity<List<BrowseEntry>> resp;
125
        try {
126
            resp = restTemplate.exchange(
127
                    builder.build().encode().toUri(),
128
                    HttpMethod.GET,
129
                    null,
130
                    new ParameterizedTypeReference<List<BrowseEntry>>() {
131
                    });
132
        } catch (RestClientException e) {
133
            throw new BrokerException(e);
134
        }
135

    
136
        return resp.getBody();
137
    }
138

    
139
    @Override
140
    @PreAuthorize("hasRole('ROLE_USER')")
141
    public EventsPage advancedShowEvents(@PathVariable("page") String page,
142
                                         @PathVariable("size") String size,
143
                                         @RequestBody AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
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(size));
150

    
151
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
152

    
153
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
154
        advQueryObject.setPage(Long.parseLong(page));
155
        HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, httpHeaders);
156
        ResponseEntity<EventsPage> resp;
157
        try {
158
            resp = restTemplate.exchange(
159
                    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
    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
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
182
                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
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
202

    
203
        int page = 0;
204
        int size = 50;
205
        List<Repository> rs ;
206
        List<Repository> resultSet = new ArrayList<>();
207

    
208
        while (true){
209
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
210
            resultSet.addAll(rs);
211
            page+=1;
212
            if(rs.size() == 0) break;
213
        }
214
        return resultSet;
215
    }
216

    
217
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
218
        return null;
219
    }
220

    
221
    @Override
222
    @PreAuthorize("hasRole('ROLE_USER')")
223
    public EventsPage showEvents(@RequestParam("datasourceName") String datasourceName,
224
                                 @RequestParam("topic") String topic,
225
                                 @RequestParam("page") String page,
226
                                 @RequestParam("size") String size) throws BrokerException, JSONException {
227

    
228
        final String service = "/events";
229

    
230
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
231
                .queryParam("ds", datasourceName)
232
                .queryParam("topic", topic)
233
                .path("/{page}/{size}/");
234

    
235
        ResponseEntity<EventsPage> resp;
236
        try {
237
            resp = restTemplate.exchange(
238
                    builder.build().expand(page, size).encode().toUri(),
239
                    HttpMethod.GET,
240
                    null,
241
                    new ParameterizedTypeReference<EventsPage>() {
242
                    });
243
        } catch (RestClientException e) {
244
            throw new BrokerException(e);
245
        }
246
        return resp.getBody();
247
    }
248

    
249
    @Override
250
    @PreAuthorize("hasRole('ROLE_USER')")
251
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(@PathVariable("userEmail")  String userEmail)
252
            throws BrokerException {
253

    
254
        final String service = "/subscriptions";
255

    
256
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
257
                .queryParam("email", userEmail);
258

    
259
        LOGGER.debug(builder.build().encode().toUri());
260
        ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
261
        try {
262
            resp = restTemplate.exchange(
263
                    builder.build().encode().toUri(),
264
                    HttpMethod.GET,
265
                    null,
266
                    new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
267
                    });
268
        } catch (RestClientException e) {
269
            LOGGER.debug("Error " , e);
270
            throw new BrokerException(e);
271
        }
272
        return resp.getBody();
273
    }
274

    
275
    @Override
276
    @PreAuthorize("hasRole('ROLE_USER')")
277
    public Subscription subscribe(@RequestBody  OpenaireSubscription obj) throws BrokerException {
278
        final String service = "/subscribe";
279

    
280
        //build the uri params
281
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
282

    
283
        HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
284

    
285
        //create new template engine
286
        RestTemplate template = new RestTemplate();
287
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
288
        ResponseEntity<Subscription> resp;
289
        try {
290
            //communicate with endpoint
291
            resp = restTemplate.exchange(
292
                    builder.build().encode().toUri(),
293
                    HttpMethod.POST,
294
                    entity,
295
                    new ParameterizedTypeReference<Subscription>() {
296
                    });
297
        } catch (RestClientException e) {
298
            throw new BrokerException(e);
299
        }
300

    
301
        return resp.getBody();
302
    }
303

    
304
    @Override
305
    @PreAuthorize("hasRole('ROLE_USER')")
306
    public void unsubscribe(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
307
        final String service = "/subscriptions/" + subscriptionId;
308

    
309
        //build the uri params
310
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
311

    
312
        try {
313
            //communicate with endpoint
314
            restTemplate.exchange(
315
                    builder.build().encode().toUri(),
316
                    HttpMethod.DELETE,
317
                    null,
318
                    new ParameterizedTypeReference<Void>() {
319
                    });
320
        } catch (RestClientException e) {
321
            throw new BrokerException(e);
322
        }
323
    }
324

    
325
    @Override
326
    @PreAuthorize("hasRole('ROLE_USER')")
327
    public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
328
        final String service = "/subscriptions/" + subscriptionId;
329

    
330
        //build the uri params
331
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
332

    
333
        ResponseEntity<Subscription> resp;
334
        try {
335
            //communicate with endpoint
336
            resp = restTemplate.exchange(
337
                    builder.build().encode().toUri(),
338
                    HttpMethod.GET,
339
                    null,
340
                    new ParameterizedTypeReference<Subscription>() {
341
                    });
342
        } catch (RestClientException e) {
343
            throw new BrokerException(e);
344
        }
345
        return resp.getBody();
346
    }
347

    
348
    @Override
349
    public Map<String, Term> getDnetTopics() throws BrokerException {
350
        return topics;
351
    }
352

    
353
    @Override
354
    @PreAuthorize("hasRole('ROLE_USER')")
355
    public EventsPage getNotificationsBySubscriptionId(@PathVariable("subscriptionId") String subscriptionId,
356
                                                       @PathVariable("page") String page,
357
                                                       @PathVariable("size") String size
358
                                                       ) throws BrokerException {
359

    
360
        UriComponents uriComponents = UriComponentsBuilder
361
                .fromHttpUrl(openairePath + "/notifications/")
362
                .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
    //@Override
380
    public Map<String, List<Subscription>> getSubscriptionsOfUser(/*@PathVariable("userEmail")*/ String userEmail)
381
            throws BrokerException {
382

    
383
        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
}
(2-2/14)