Project

General

Profile

1
package eu.dnetlib.repo.manager.controllers;
2

    
3
import eu.dnetlib.repo.manager.domain.BrokerException;
4
import eu.dnetlib.repo.manager.domain.Term;
5
import eu.dnetlib.repo.manager.domain.broker.*;
6
import eu.dnetlib.repo.manager.service.BrokerServiceImpl;
7
import io.swagger.annotations.Api;
8
import io.swagger.annotations.ApiParam;
9
import org.json.JSONException;
10
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.MediaType;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.security.core.context.SecurityContextHolder;
16
import org.springframework.web.bind.annotation.*;
17

    
18
import java.io.IOException;
19
import java.util.List;
20
import java.util.Map;
21

    
22
@RestController
23
@RequestMapping(value = "/broker")
24
@Api(description = "Broker API",  tags = {"broker"})
25
public class BrokerController{
26

    
27
    @Autowired
28
    private BrokerServiceImpl brokerService;
29

    
30
    @RequestMapping(value = "/getDatasourcesOfUser" , method = RequestMethod.GET,
31
            produces = MediaType.APPLICATION_JSON_VALUE)
32
    @ResponseBody
33
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
34
    public DatasourcesBroker getDatasourcesOfUser(
35
                                           @RequestParam("includeShared")
36
                                           @ApiParam(value = "Include shared datasources", required = true , defaultValue = "false") String includeShared,
37
                                           @RequestParam("includeByOthers")
38
                                           @ApiParam(value = "Include datasources of other", required = true,defaultValue = "false") String includeByOthers) throws JSONException {
39
        return brokerService.getDatasourcesOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), includeShared, includeByOthers);
40
    }
41

    
42
    @RequestMapping(value = "/getTopicsForDatasource/{datasourceName:.+}" ,
43
            method = RequestMethod.GET,
44
            produces = MediaType.APPLICATION_JSON_VALUE)
45
    @ResponseBody
46
    public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName")  String datasourceName) throws BrokerException{
47
        return brokerService.getTopicsForDatasource(datasourceName);
48
    }
49

    
50
    @RequestMapping(value = "/advancedShowEvents/{page}/{size}" ,
51
            method = RequestMethod.POST,
52
            produces = MediaType.APPLICATION_JSON_VALUE)
53
    @ResponseBody
54
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
55
    public EventsPage advancedShowEvents(@PathVariable("page") String page,
56
                                  @PathVariable("size") String size,
57
                                  @RequestBody AdvQueryObject advQueryObject) throws BrokerException, JSONException ,IOException{
58
        return brokerService.advancedShowEvents(page, size, advQueryObject);
59
    }
60

    
61
    @RequestMapping(value = "/showEvents/{datasourceName:.+}/{topic}/{page}" ,
62
            method = RequestMethod.GET,
63
            produces = MediaType.APPLICATION_JSON_VALUE)
64
    @ResponseBody
65
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
66
    public EventsPage showEvents(@RequestParam("datasourceName") String datasourceName,
67
                          @RequestParam("topic") String topic,
68
                          @RequestParam("page") String page,
69
                          @RequestParam("size") String size) throws BrokerException, JSONException{
70
        return brokerService.showEvents(datasourceName, topic, page, size);
71
    }
72

    
73
    @RequestMapping(value = "/getSimpleSubscriptionsOfUser" ,
74
            method = RequestMethod.GET,
75
            produces = MediaType.APPLICATION_JSON_VALUE)
76
    @ResponseBody
77
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
78
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser() throws BrokerException{
79
        return brokerService.getSimpleSubscriptionsOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail());
80
    }
81

    
82
    @RequestMapping(value = "/subscribe" , method = RequestMethod.POST,
83
            consumes = MediaType.APPLICATION_JSON_VALUE,
84
            produces = MediaType.APPLICATION_JSON_VALUE)
85
    @ResponseBody
86
    @PreAuthorize("hasAuthority('REGISTERED_USER') ")
87
    public Subscription subscribe(@RequestBody OpenaireSubscription obj) throws BrokerException{
88
        return brokerService.subscribe(obj);
89
    }
90

    
91
    @RequestMapping(value = "/unsubscribe/{subscriptionId}" , method = RequestMethod.POST,
92
            consumes = MediaType.APPLICATION_JSON_VALUE,
93
            produces = MediaType.APPLICATION_JSON_VALUE)
94
    @ResponseBody
95
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
96
    public ResponseEntity<Object> unsubscribe(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException{
97
        return brokerService.unsubscribe(subscriptionId);
98
    }
99

    
100
    @RequestMapping(value = "/getSubscription/{subscriptionId}" , method = RequestMethod.GET,
101
            produces = MediaType.APPLICATION_JSON_VALUE)
102
    @ResponseBody
103
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
104
    public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException{
105
        return brokerService.getSubscription(subscriptionId);
106
    }
107

    
108

    
109
    @RequestMapping(value = "/getDnetTopics" , method = RequestMethod.GET,
110
            produces = MediaType.APPLICATION_JSON_VALUE)
111
    @ResponseBody
112
    public Map<String, Term> getDnetTopics() throws BrokerException{
113
        return brokerService.getDnetTopics();
114
    }
115

    
116
    @RequestMapping(value = "/getNotificationsBySubscriptionId/{subscriptionId}/{page}/{size}" , method = RequestMethod.GET
117
            ,produces = MediaType.APPLICATION_JSON_VALUE)
118
    @ResponseBody
119
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
120
    public EventsPage getNotificationsBySubscriptionId(@PathVariable("subscriptionId") String subscriptionId,
121
                                                @PathVariable("page") String page,
122
                                                @PathVariable("size") String size) throws BrokerException{
123
        return brokerService.getNotificationsBySubscriptionId(subscriptionId, page, size);
124
    }
125

    
126
}
(1-1/13)