Project

General

Profile

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

    
3
import eu.dnetlib.domain.data.Repository;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.repo.manager.domain.*;
6
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
7
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
8
import io.swagger.annotations.Api;
9
import org.apache.log4j.Logger;
10
import org.json.JSONException;
11
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.MediaType;
14
import org.springframework.security.access.prepost.PostAuthorize;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.security.core.Authentication;
17
import org.springframework.security.core.context.SecurityContextHolder;
18
import org.springframework.web.bind.annotation.*;
19

    
20
import java.io.IOException;
21
import java.util.List;
22
import java.util.Map;
23

    
24
@RestController
25
@RequestMapping(value = "/repository")
26
@Api(description = "Repository API", tags = {"repository"})
27
public class RepositoryController {
28

    
29
    private static final Logger logger = Logger.getLogger(RepositoryController.class);
30

    
31
    @Autowired
32
    private RepositoryServiceImpl repositoryService;
33

    
34

    
35
    @RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
36
    @ResponseBody
37
    public Country[] getCountries() {
38
        return repositoryService.getCountries();
39
    }
40

    
41
    @RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET,
42
            produces = MediaType.APPLICATION_JSON_VALUE)
43
    @ResponseBody
44
    public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
45
                                                            @PathVariable("mode") String mode,
46
                                                            @RequestParam(value = "managed", required = false) Boolean managed) throws JSONException, IOException {
47
        return repositoryService.getRepositoriesByCountry(country, mode, managed);
48
    }
49

    
50
    @RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}", method = RequestMethod.GET,
51
            produces = MediaType.APPLICATION_JSON_VALUE)
52
    @ResponseBody
53
    @PreAuthorize("hasRole('ROLE_USER')")
54
    public List<RepositorySnippet> getRepositoriesSnippetOfUser(
55
            @PathVariable("page") String page,
56
            @PathVariable("size") String size) throws JSONException, IOException {
57
        return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
58
    }
59

    
60
    @RequestMapping(value = "/user/repositories/{page}/{size}", method = RequestMethod.GET,
61
            produces = MediaType.APPLICATION_JSON_VALUE)
62
    @ResponseBody
63
    @PreAuthorize("hasRole('ROLE_USER')")
64
    public List<Repository> getRepositoriesOfUser(
65
            @PathVariable("page") String page,
66
            @PathVariable("size") String size) throws JSONException, IOException {
67
        return repositoryService.getRepositoriesOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
68
    }
69

    
70
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,
71
            produces = MediaType.APPLICATION_JSON_VALUE)
72
    @ResponseBody
73
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
74
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name = "country", required = false) String country,
75
                                                                @RequestParam(name = "typology", required = false) String typology,
76
                                                                @RequestParam(name = "englishName", required = false) String englishName,
77
                                                                @RequestParam(name = "officialName", required = false) String officialName,
78
                                                                @RequestParam("requestSortBy") String requestSortBy,
79
                                                                @RequestParam("order") String order,
80
                                                                @PathVariable("page") int page,
81
                                                                @PathVariable("size") int pageSize) throws Exception {
82

    
83
        return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
84
    }
85

    
86

    
87
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
88
            produces = MediaType.APPLICATION_JSON_VALUE)
89
    @ResponseBody
90
    @PostAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id) or (returnObject.registeredBy=='null' and hasRole('ROLE_USER'))")
91
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException, ResourceNotFoundException {
92
        Repository repo = repositoryService.getRepositoryById(id);
93

    
94
        if (repo != null)
95
            logger.info("Returning repository " + repo.getId() + " registered by " + repo.getRegisteredBy());
96
        else
97
            logger.info("Requested repository " + id + " not found");
98
        return repo;
99
    }
100

    
101
    @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET,
102
            produces = MediaType.APPLICATION_JSON_VALUE)
103
    @ResponseBody
104
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
105
        return repositoryService.getRepositoryAggregations(id, 0, 20);
106
    }
107

    
108
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
109
            produces = MediaType.APPLICATION_JSON_VALUE)
110
    @ResponseBody
111
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
112
        return repositoryService.getRepositoryAggregationsByYear(id);
113
    }
114

    
115
    @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
116
            produces = MediaType.APPLICATION_JSON_VALUE)
117
    @ResponseBody
118
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
119
                                                  @PathVariable("page") String page,
120
                                                  @PathVariable("size") String size) throws JSONException {
121
        return repositoryService.getRepositoriesByName(name, page, size);
122
    }
123

    
124
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
125
            produces = MediaType.APPLICATION_JSON_VALUE)
126
    @ResponseBody
127
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id) or (@repositoryService.getRepositoryById(#id).registeredBy=='null' and hasRole('ROLE_USER'))")
128
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
129
        return repositoryService.getRepositoryInterface(id);
130
    }
131

    
132
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
133
            consumes = MediaType.APPLICATION_JSON_VALUE)
134
    @ResponseBody
135
//    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@aaiUserRoleService.convertRepoIdToRoleId(#repository.id)) or hasRole(@aaiUserRoleService.convertRepoIdToRoleId(returnObject.id)))")
136
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or hasRole('ROLE_USER')")
137
    public Repository addRepository(@RequestParam("datatype") String datatype,
138
                                    @RequestBody Repository repository) throws Exception {
139

    
140
        return repositoryService.addRepository(datatype, repository);
141
    }
142

    
143
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
144
            produces = MediaType.APPLICATION_JSON_VALUE)
145
    @ResponseBody
146
    public List<String> getDnetCountries() {
147
        return repositoryService.getDnetCountries();
148
    }
149

    
150
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
151
            produces = MediaType.APPLICATION_JSON_VALUE)
152
    @ResponseBody
153
    public List<String> getTypologies() {
154
        return repositoryService.getTypologies();
155
    }
156

    
157
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
158
            produces = MediaType.APPLICATION_JSON_VALUE)
159
    @ResponseBody
160
    public List<Timezone> getTimezones() {
161
        return repositoryService.getTimezones();
162
    }
163

    
164
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
165
            consumes = MediaType.APPLICATION_JSON_VALUE)
166
    @ResponseBody
167
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repository.id)")
168
    public Repository updateRepository(@RequestBody Repository repository, Authentication authentication) throws Exception {
169
        return repositoryService.updateRepository(repository, authentication);
170
    }
171

    
172
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
173
    @PreAuthorize("@aaiUserRoleService.isMemberOf(#id)")
174
    public void deleteRepositoryInterface(@RequestParam("id") String id,
175
                                          @RequestParam("registeredBy") String registeredBy) {
176
        repositoryService.deleteRepositoryInterface(id, registeredBy);
177
    }
178

    
179
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
180
            consumes = MediaType.APPLICATION_JSON_VALUE)
181
    @ResponseBody
182
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
183
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
184
                                                      @RequestParam("repoId") String repoId,
185
                                                      @RequestParam("registeredBy") String registeredBy,
186
                                                      @RequestParam(value = "comment", required = false) String comment,
187
                                                      @RequestBody RepositoryInterface repositoryInterface) throws Exception {
188
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, comment, repositoryInterface);
189
    }
190

    
191
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/", method = RequestMethod.GET,
192
            produces = MediaType.APPLICATION_JSON_VALUE)
193
    @ResponseBody
194
    @PreAuthorize("hasRole('ROLE_USER')")
195
    public List<String> getUrlsOfUserRepos(@PathVariable("page") String page, @PathVariable("size") String size) {
196
        return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
197
    }
198

    
199
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}", method = RequestMethod.GET,
200
            produces = MediaType.APPLICATION_JSON_VALUE)
201
    @ResponseBody
202
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
203
        return repositoryService.getDatasourceVocabularies(mode);
204
    }
205

    
206
    @RequestMapping(value = "/getCompatibilityClasses/{mode}", method = RequestMethod.GET,
207
            produces = MediaType.APPLICATION_JSON_VALUE)
208
    @ResponseBody
209
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
210

    
211
        return repositoryService.getCompatibilityClasses(mode);
212
    }
213

    
214
    @RequestMapping(value = "/getDatasourceClasses/{mode}", method = RequestMethod.GET,
215
            produces = MediaType.APPLICATION_JSON_VALUE)
216
    @ResponseBody
217
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
218
        return repositoryService.getDatasourceClasses(mode);
219
    }
220

    
221
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}", method = RequestMethod.GET,
222
            produces = MediaType.APPLICATION_JSON_VALUE)
223
    @ResponseBody
224
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId") String repoId) throws RepositoryServiceException {
225
        return repositoryService.getMetricsInfoForRepository(repoId);
226
    }
227

    
228
    @RequestMapping(value = "/getListLatestUpdate/{mode}", method = RequestMethod.GET,
229
            produces = MediaType.APPLICATION_JSON_VALUE)
230
    @ResponseBody
231
    public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException {
232
        return repositoryService.getListLatestUpdate(mode);
233
    }
234

    
235
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
236
            consumes = MediaType.APPLICATION_JSON_VALUE)
237
    @ResponseBody
238
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
239
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
240
                                                         @RequestParam("registeredBy") String registeredBy,
241
                                                         @RequestParam(value = "comment", required = false) String comment,
242
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
243
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, comment, repositoryInterface);
244
    }
245
}
(7-7/12)