Revision 60747
Added by Antonis Koulalis about 3 years ago
RepositoryController.java | ||
---|---|---|
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 eu.dnetlib.repo.manager.utils.Converter; |
|
9 |
import io.swagger.annotations.Api; |
|
10 |
import org.apache.log4j.Logger; |
|
11 |
import org.json.JSONException; |
|
12 |
import org.mitre.openid.connect.model.OIDCAuthenticationToken; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.http.MediaType; |
|
15 |
import org.springframework.security.access.prepost.PostAuthorize; |
|
16 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
17 |
import org.springframework.security.core.Authentication; |
|
18 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
19 |
import org.springframework.web.bind.annotation.*; |
|
20 |
|
|
21 |
import java.io.IOException; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Map; |
|
24 |
|
|
25 |
@RestController |
|
26 |
@RequestMapping(value = "/repository") |
|
27 |
@Api(description = "Repository API", tags = {"repository"}) |
|
28 |
public class RepositoryController { |
|
29 |
|
|
30 |
private static Logger logger = Logger.getLogger(RepositoryController.class); |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private RepositoryServiceImpl repositoryService; |
|
34 |
|
|
35 |
// @Autowired |
|
36 |
// private Converter converter; |
|
37 |
|
|
38 |
@RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) |
|
39 |
@ResponseBody |
|
40 |
public Country[] getCountries() { |
|
41 |
return repositoryService.getCountries(); |
|
42 |
} |
|
43 |
|
|
44 |
@RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET, |
|
45 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
46 |
@ResponseBody |
|
47 |
public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country, |
|
48 |
@PathVariable("mode") String mode, |
|
49 |
@RequestParam(value = "managed",required=false) Boolean managed) throws JSONException, IOException { |
|
50 |
return repositoryService.getRepositoriesByCountry(country, mode, managed); |
|
51 |
} |
|
52 |
|
|
53 |
@RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}",method = RequestMethod.GET, |
|
54 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
55 |
@ResponseBody |
|
56 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
57 |
public List<RepositorySnippet> getRepositoriesOfUser( |
|
58 |
@PathVariable("page") String page, |
|
59 |
@PathVariable("size") String size) throws JSONException, IOException { |
|
60 |
return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size); |
|
61 |
} |
|
62 |
|
|
63 |
@RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}",method = RequestMethod.GET, |
|
64 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
65 |
@ResponseBody |
|
66 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
|
67 |
public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name="country", required=false) String country, |
|
68 |
@RequestParam(name="typology", required=false) String typology, |
|
69 |
@RequestParam(name="englishName", required=false) String englishName, |
|
70 |
@RequestParam(name="officialName", required=false) String officialName, |
|
71 |
@RequestParam("requestSortBy") String requestSortBy, |
|
72 |
@RequestParam("order") String order, |
|
73 |
@PathVariable("page") int page, |
|
74 |
@PathVariable("size") int pageSize) throws Exception { |
|
75 |
|
|
76 |
return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize); |
|
77 |
} |
|
78 |
|
|
79 |
// TODO: Antonis K - Replace here the registeredBy |
|
80 |
|
|
81 |
@RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET, |
|
82 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
83 |
@ResponseBody |
|
84 |
// @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER'))") |
|
85 |
@PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(returnObject.id)))") |
|
86 |
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException { |
|
87 |
Repository repo = repositoryService.getRepositoryById(id); |
|
88 |
|
|
89 |
if (repo != null) |
|
90 |
logger.info("Returning repository " + repo.getId() + " registered by " + repo.getRegisteredBy()); |
|
91 |
else |
|
92 |
logger.info("Requested repository " + id + " not found"); |
|
93 |
return repo; |
|
94 |
} |
|
95 |
|
|
96 |
@RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET, |
|
97 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
98 |
@ResponseBody |
|
99 |
public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException { |
|
100 |
return repositoryService.getRepositoryAggregations(id,0,20); |
|
101 |
} |
|
102 |
|
|
103 |
@RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET, |
|
104 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
105 |
@ResponseBody |
|
106 |
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException { |
|
107 |
return repositoryService.getRepositoryAggregationsByYear(id); |
|
108 |
} |
|
109 |
|
|
110 |
@RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET, |
|
111 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
112 |
@ResponseBody |
|
113 |
public List<Repository> getRepositoriesByName(@PathVariable("name") String name, |
|
114 |
@PathVariable("page") String page, |
|
115 |
@PathVariable("size") String size) throws JSONException { |
|
116 |
return repositoryService.getRepositoriesByName(name, page, size); |
|
117 |
} |
|
118 |
|
|
119 |
@RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET, |
|
120 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
121 |
@ResponseBody |
|
122 |
// @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#id).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#id).registeredBy=='null' ) and hasRole('ROLE_USER'))") |
|
123 |
@PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or hasRole(@Converter.convertRepoIdToRoleId(@RepositoryService.getRepositoryById(#id).id))") |
|
124 |
public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException { |
|
125 |
return repositoryService.getRepositoryInterface(id); |
|
126 |
} |
|
127 |
|
|
128 |
@RequestMapping(value = "/addRepository", method = RequestMethod.POST, |
|
129 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
130 |
@ResponseBody |
|
131 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(#repository.id)) or hasRole(@Converter.convertRepoIdToRoleId(returnObject.id)))") |
|
132 |
public Repository addRepository(@RequestParam("datatype") String datatype, |
|
133 |
@RequestBody Repository repository) throws Exception { |
|
134 |
|
|
135 |
return repositoryService.addRepository(datatype, repository); |
|
136 |
} |
|
137 |
|
|
138 |
@RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET, |
|
139 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
140 |
@ResponseBody |
|
141 |
public List<String> getDnetCountries(){ |
|
142 |
return repositoryService.getDnetCountries(); |
|
143 |
} |
|
144 |
|
|
145 |
@RequestMapping(value = "/getTypologies", method = RequestMethod.GET, |
|
146 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
147 |
@ResponseBody |
|
148 |
public List<String> getTypologies(){ |
|
149 |
return repositoryService.getTypologies(); |
|
150 |
} |
|
151 |
|
|
152 |
@RequestMapping(value = "/getTimezones", method = RequestMethod.GET, |
|
153 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
154 |
@ResponseBody |
|
155 |
public List<Timezone> getTimezones(){ |
|
156 |
return repositoryService.getTimezones(); |
|
157 |
} |
|
158 |
|
|
159 |
@RequestMapping(value = "/updateRepository", method = RequestMethod.POST, |
|
160 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
161 |
@ResponseBody |
|
162 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(#repository.id)) or hasRole(@Converter.convertRepoIdToRoleId(returnObject.id)))") |
|
163 |
public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception { |
|
164 |
return repositoryService.updateRepository(repository, authentication); |
|
165 |
} |
|
166 |
|
|
167 |
@RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE) |
|
168 |
@PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email") |
|
169 |
public void deleteRepositoryInterface(@RequestParam("id") String id , |
|
170 |
@RequestParam("registeredBy") String registeredBy){ |
|
171 |
repositoryService.deleteRepositoryInterface(id, registeredBy); |
|
172 |
} |
|
173 |
|
|
174 |
@RequestMapping(value = "/addInterface", method = RequestMethod.POST, |
|
175 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
176 |
@ResponseBody |
|
177 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(@RepositoryService.getRepositoryById(#repoId).id)))") |
|
178 |
public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype, |
|
179 |
@RequestParam("repoId") String repoId, |
|
180 |
@RequestParam("registeredBy") String registeredBy, |
|
181 |
@RequestParam(value = "comment", required = false) String comment, |
|
182 |
@RequestBody RepositoryInterface repositoryInterface) throws Exception { |
|
183 |
return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, comment, repositoryInterface); |
|
184 |
} |
|
185 |
|
|
186 |
@RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/",method = RequestMethod.GET, |
|
187 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
188 |
@ResponseBody |
|
189 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
190 |
public List<String> getUrlsOfUserRepos( |
|
191 |
@PathVariable("page") String page, |
|
192 |
@PathVariable("size") String size) throws JSONException { |
|
193 |
return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size); |
|
194 |
} |
|
195 |
|
|
196 |
@RequestMapping(value = "/getDatasourceVocabularies/{mode}",method = RequestMethod.GET, |
|
197 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
198 |
@ResponseBody |
|
199 |
public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) { |
|
200 |
return repositoryService.getDatasourceVocabularies(mode); |
|
201 |
} |
|
202 |
|
|
203 |
@RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET, |
|
204 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
205 |
@ResponseBody |
|
206 |
public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) { |
|
207 |
|
|
208 |
return repositoryService.getCompatibilityClasses(mode); |
|
209 |
} |
|
210 |
|
|
211 |
@RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET, |
|
212 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
213 |
@ResponseBody |
|
214 |
public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) { |
|
215 |
return repositoryService.getDatasourceClasses(mode); |
|
216 |
} |
|
217 |
|
|
218 |
@RequestMapping(value = "/getMetricsInfoForRepository/{repoId}",method = RequestMethod.GET, |
|
219 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
220 |
@ResponseBody |
|
221 |
public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId") String repoId) throws RepositoryServiceException { |
|
222 |
return repositoryService.getMetricsInfoForRepository(repoId); |
|
223 |
} |
|
224 |
|
|
225 |
@RequestMapping(value = "/getListLatestUpdate/{mode}",method = RequestMethod.GET, |
|
226 |
produces = MediaType.APPLICATION_JSON_VALUE) |
|
227 |
@ResponseBody |
|
228 |
public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException { |
|
229 |
return repositoryService.getListLatestUpdate(mode); |
|
230 |
} |
|
231 |
|
|
232 |
@RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST, |
|
233 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
234 |
@ResponseBody |
|
235 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or hasRole(@Converter.convertRepoIdToRoleId(@RepositoryService.getRepositoryById(#repoId).id))") |
|
236 |
public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId, |
|
237 |
@RequestParam("registeredBy") String registeredBy, |
|
238 |
@RequestParam(value = "comment", required = false) String comment, |
|
239 |
@RequestBody RepositoryInterface repositoryInterface) throws Exception { |
|
240 |
return repositoryService.updateRepositoryInterface(repoId, registeredBy, comment, repositoryInterface); |
|
241 |
} |
|
242 |
} |
Also available in: Unified diff