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.stereotype.Component;
22
import org.springframework.util.LinkedMultiValueMap;
23
import org.springframework.util.MultiValueMap;
24
import org.springframework.web.bind.annotation.PathVariable;
25
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestParam;
27
import org.springframework.web.client.RestClientException;
28
import org.springframework.web.client.RestTemplate;
29
import org.springframework.web.util.UriComponents;
30
import org.springframework.web.util.UriComponentsBuilder;
31

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

    
38
@Component
39
public class BrokerApiImpl implements BrokerApi {
40

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

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

    
53
    private RestTemplate restTemplate = null;
54

    
55
    private HttpHeaders httpHeaders;
56

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

    
59
    @PostConstruct
60
    private void initDnetTopicsMap() {
61

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

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

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

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

    
87

    
88
    @Override
89
    public DatasourcesBroker getDatasourcesOfUser(@RequestParam("user") @ApiParam(value = "User email", required = true) String user,
90
                                                  @RequestParam("includeShared")
91
                                                  @ApiParam(value = "Include shared datasources", required = true) String includeShared,
92
                                                  @RequestParam("includeByOthers") @ApiParam(value = "Include datasources of other", required = true) String includeByOthers) throws JSONException {
93

    
94
        DatasourcesBroker ret = new DatasourcesBroker();
95
        try {
96
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
97
            if (Boolean.parseBoolean(includeShared)) {
98
                //TODO whatever nikonas was saying
99
                List<String> sharedDatasourceIds = new ArrayList<String>();
100
                ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
101
            }
102

    
103
            if (Boolean.parseBoolean(includeByOthers)) {
104
                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user)));
105
            }
106
        } catch (BrokerException e) {
107
            e.printStackTrace();
108
        }
109

    
110
        return ret;
111
    }
112

    
113
    @Override
114
    public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName")  String datasourceName) throws BrokerException {
115
        final String service = "/topicsForDatasource";
116

    
117
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
118
                .queryParam("ds", datasourceName);
119

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

    
132
        return resp.getBody();
133
    }
134

    
135
    @Override
136
    public EventsPage advancedShowEvents(@PathVariable("page") String page,
137
                                         @PathVariable("size") String size,
138
                                         @RequestBody AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException {
139

    
140
        final String service = "/events/{page}/{pageSize}";
141

    
142
        Map<String, Long> uriParams = new HashMap<>();
143
        uriParams.put("page", Long.parseLong(page));
144
        uriParams.put("pageSize", Long.parseLong(size));
145

    
146
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
147

    
148
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
149
        advQueryObject.setPage(Long.parseLong(page));
150
        HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, httpHeaders);
151
        ResponseEntity<EventsPage> resp;
152
        try {
153
            resp = restTemplate.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
    }
167

    
168

    
169
    private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
170

    
171
        List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
172
        for (Repository repo : repositories) {
173
            BrowseEntry temp = new BrowseEntry();
174
            temp.setValue(repo.getOfficialName());
175
            temp.setSize(new Long(0));
176
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
177
                temp.setSize(temp.getSize() + e.getSize());
178
            }
179
            Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
180
            entries.add(tup);
181
        }
182

    
183
        // sort the collection by the second field of the tuple which is size
184
        Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
185
            @Override
186
            public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
187
                return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
188
            }
189
        });
190

    
191
        return entries;
192
    }
193

    
194

    
195

    
196
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
197

    
198
        int page = 0;
199
        int size = 50;
200
        List<Repository> rs ;
201
        List<Repository> resultSet = new ArrayList<>();
202

    
203
        while (true){
204
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
205
            resultSet.addAll(rs);
206
            page+=1;
207
            if(rs.size() == 0) break;
208
        }
209
        return resultSet;
210
    }
211

    
212
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
213
        return null;
214
    }
215

    
216
    @Override
217
    public EventsPage showEvents(@PathVariable("datasourceName") String datasourceName,
218
                                 @PathVariable("topic") String topic,
219
                                 @PathVariable("page") String page) throws BrokerException, JSONException {
220

    
221
        /*JSONObject json_params = new JSONObject(params);
222

    
223
        String datasourceName = json_params.getString("datasourceName");
224
        String topic = json_params.getString("topic");
225
        String page = json_params.getString("page");*/
226

    
227
        final String service = "/showEvents";
228

    
229
        //build the uri params
230
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
231
                .queryParam("ds", datasourceName)
232
                .queryParam("topic", topic)
233
                .queryParam("page", page);
234

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

    
250
    @Override
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
    public Subscription subscribe(@RequestBody  OpenaireSubscription obj) throws BrokerException {
277
        final String service = "/subscribe";
278

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

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

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

    
300
        return resp.getBody();
301
    }
302

    
303
    @Override
304
    public void unsubscribe(@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
        try {
311
            //communicate with endpoint
312
            restTemplate.exchange(
313
                    builder.build().encode().toUri(),
314
                    HttpMethod.DELETE,
315
                    null,
316
                    new ParameterizedTypeReference<Void>() {
317
                    });
318
        } catch (RestClientException e) {
319
            throw new BrokerException(e);
320
        }
321
    }
322

    
323
    @Override
324
    public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
325
        final String service = "/subscriptions/" + subscriptionId;
326

    
327
        //build the uri params
328
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
329

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

    
345
    @Override
346
    public Map<String, Term> getDnetTopics() throws BrokerException {
347
        return topics;
348
    }
349

    
350
    @Override
351
    public EventsPage getNotificationsBySubscriptionId(@PathVariable("subscriptionId") String subscriptionId,
352
                                                       @PathVariable("page") String page,
353
                                                       @PathVariable("size") String size
354
                                                       ) throws BrokerException {
355

    
356
        UriComponents uriComponents = UriComponentsBuilder
357
                .fromHttpUrl(openairePath + "/notifications/")
358
                .path("/{id}/{page}/{size}/")
359
                .build().expand(subscriptionId,page, size).encode();
360

    
361
        ResponseEntity<EventsPage> resp;
362
        try {
363
            resp = restTemplate.exchange(
364
                    uriComponents.toUri(),
365
                    HttpMethod.GET,
366
                    null,
367
                    new ParameterizedTypeReference<EventsPage>() {
368
                    });
369
        } catch (RestClientException e) {
370
            throw new BrokerException(e);
371
        }
372
        return resp.getBody();
373
    }
374

    
375
    @Override
376
    public Map<String, List<Subscription>> getSubscriptionsOfUser(@PathVariable("userEmail") String userEmail)
377
            throws BrokerException {
378

    
379
        Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
380
        Map<String,List<Subscription>> subs = new HashMap<>();
381
        List<Subscription> subscriptions = null;
382

    
383
        for(String s:simpleSubs.keySet()){
384
            List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
385
            for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
386
                subscriptions = new ArrayList<>();
387
                subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
388
            }
389
            subs.put(s,subscriptions);
390
        }
391
        return subs;
392
    }
393

    
394

    
395
}
(2-2/12)