|
11 | 11 |
|
12 | 12 | package org.eclipse.sw360.rest.resourceserver.importexport; |
13 | 13 |
|
14 | | -import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; |
15 | | - |
16 | | -import java.io.IOException; |
17 | | - |
| 14 | +import io.swagger.v3.oas.annotations.Operation; |
| 15 | +import io.swagger.v3.oas.annotations.Parameter; |
| 16 | +import io.swagger.v3.oas.annotations.enums.ParameterIn; |
| 17 | +import io.swagger.v3.oas.annotations.media.Content; |
| 18 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 19 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 20 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| 21 | +import jakarta.servlet.ServletException; |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | +import jakarta.servlet.http.HttpServletResponse; |
| 24 | +import lombok.NonNull; |
| 25 | +import lombok.RequiredArgsConstructor; |
18 | 26 | import org.apache.thrift.TException; |
19 | 27 | import org.apache.thrift.transport.TTransportException; |
| 28 | +import org.eclipse.sw360.datahandler.thrift.RequestStatus; |
| 29 | +import org.eclipse.sw360.datahandler.thrift.RequestSummary; |
20 | 30 | import org.eclipse.sw360.datahandler.thrift.SW360Exception; |
21 | 31 | import org.eclipse.sw360.datahandler.thrift.users.User; |
22 | 32 | import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper; |
23 | 33 | import org.slf4j.Logger; |
24 | 34 | import org.slf4j.LoggerFactory; |
25 | | -import org.eclipse.sw360.datahandler.thrift.RequestSummary; |
26 | 35 | import org.springframework.beans.factory.annotation.Autowired; |
27 | 36 | import org.springframework.data.rest.webmvc.BasePathAwareController; |
28 | 37 | import org.springframework.data.rest.webmvc.RepositoryLinksResource; |
29 | 38 | import org.springframework.hateoas.server.RepresentationModelProcessor; |
| 39 | +import org.springframework.http.HttpStatus; |
| 40 | +import org.springframework.http.MediaType; |
30 | 41 | import org.springframework.http.ResponseEntity; |
31 | 42 | import org.springframework.security.access.prepost.PreAuthorize; |
32 | 43 | import org.springframework.web.bind.annotation.GetMapping; |
33 | 44 | import org.springframework.web.bind.annotation.RequestMapping; |
34 | 45 | import org.springframework.web.bind.annotation.RequestMethod; |
35 | | -import org.springframework.web.bind.annotation.RestController; |
36 | | - |
37 | | -import io.swagger.v3.oas.annotations.Operation; |
38 | | -import io.swagger.v3.oas.annotations.Parameter; |
39 | | -import io.swagger.v3.oas.annotations.enums.ParameterIn; |
40 | | -import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
41 | | -import org.springframework.http.MediaType; |
42 | 46 | import org.springframework.web.bind.annotation.RequestParam; |
| 47 | +import org.springframework.web.bind.annotation.RestController; |
43 | 48 | import org.springframework.web.multipart.MultipartFile; |
44 | 49 |
|
45 | | -import jakarta.servlet.ServletException; |
46 | | -import jakarta.servlet.http.HttpServletRequest; |
47 | | -import jakarta.servlet.http.HttpServletResponse; |
48 | | -import lombok.NonNull; |
49 | | -import lombok.RequiredArgsConstructor; |
| 50 | +import java.io.IOException; |
| 51 | + |
| 52 | +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; |
50 | 53 |
|
51 | 54 | @BasePathAwareController |
52 | 55 | @RequiredArgsConstructor(onConstructor = @__(@Autowired)) |
@@ -283,21 +286,58 @@ public void downloadUsers(HttpServletResponse response) throws SW360Exception { |
283 | 286 | description = "Upload a users CSV file to import users into the system. Requires ADMIN authority.", |
284 | 287 | tags = {"ImportExport"}, |
285 | 288 | parameters = { |
286 | | - @Parameter(name = "Content-Type", in = ParameterIn.HEADER, required = true, description = "The content type of the request. Supported values: multipart/mixed or multipart/form-data.") |
| 289 | + @Parameter( |
| 290 | + name = "Content-Type", in = ParameterIn.HEADER, required = true, |
| 291 | + description = "The content type of the request. " + |
| 292 | + "Supported values: " + MediaType.MULTIPART_MIXED_VALUE + " or " + |
| 293 | + MediaType.MULTIPART_FORM_DATA_VALUE + "." |
| 294 | + ) |
| 295 | + }, |
| 296 | + responses = { |
| 297 | + @ApiResponse( |
| 298 | + responseCode = "200", description = "Users imported successfully.", |
| 299 | + content = { |
| 300 | + @Content(mediaType = "application/json", |
| 301 | + schema = @Schema(implementation = RequestSummary.class)) |
| 302 | + } |
| 303 | + ), |
| 304 | + @ApiResponse( |
| 305 | + responseCode = "207", description = "Partial import done, some users failed to import. More " + |
| 306 | + "information in the `message` field of response.", |
| 307 | + content = { |
| 308 | + @Content(mediaType = "application/json", |
| 309 | + schema = @Schema(implementation = RequestSummary.class)) |
| 310 | + } |
| 311 | + ), |
| 312 | + @ApiResponse( |
| 313 | + responseCode = "400", description = "The uploaded CSV is in bad format." |
| 314 | + ), |
| 315 | + @ApiResponse( |
| 316 | + responseCode = "403", description = "User is not an admin." |
| 317 | + ), |
| 318 | + @ApiResponse( |
| 319 | + responseCode = "500", description = "Failed to upload the file." |
| 320 | + ) |
287 | 321 | } |
288 | 322 | ) |
289 | 323 | @PreAuthorize("hasAuthority('ADMIN')") |
290 | | - @RequestMapping(value = IMPORTEXPORT_URL + "/uploadUsers", method = RequestMethod.POST, consumes = { |
291 | | - MediaType.MULTIPART_MIXED_VALUE, |
292 | | - MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE }) |
293 | | - public ResponseEntity<RequestSummary> uploadUsers( |
294 | | - @Parameter(description = "The users CSV file to be uploaded. Expected columns: GivenName, Lastname, Email, Department, UserGroup (optional), GID (optional), PasswdHash (optional), wantsMailNotification (optional)") |
295 | | - @RequestParam("usersFile") MultipartFile file, |
296 | | - HttpServletRequest request, HttpServletResponse response |
297 | | - ) throws TException, IOException, ServletException { |
298 | | - |
| 324 | + @RequestMapping(value = IMPORTEXPORT_URL + "/usersCsv", method = RequestMethod.POST, |
| 325 | + consumes = {MediaType.MULTIPART_MIXED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, |
| 326 | + produces = {MediaType.APPLICATION_JSON_VALUE}) |
| 327 | + public ResponseEntity<RequestSummary> uploadUsersCsv( |
| 328 | + @Parameter( |
| 329 | + description = "The users CSV file to be uploaded. " + |
| 330 | + "Expected columns: " + |
| 331 | + "GivenName, Lastname, Email, Department, UserGroup, GID, PasswdHash, wantsMailNotification (optional)" |
| 332 | + ) |
| 333 | + @RequestParam("usersCsv") MultipartFile file |
| 334 | + ) throws IOException { |
299 | 335 | User sw360User = restControllerHelper.getSw360UserFromAuthentication(); |
300 | | - RequestSummary requestSummary = importExportService.uploadUsers(sw360User, file, request); |
301 | | - return ResponseEntity.ok(requestSummary); |
| 336 | + RequestSummary requestSummary = importExportService.uploadUsers(sw360User, file); |
| 337 | + if (requestSummary.getRequestStatus() == RequestStatus.SUCCESS) { |
| 338 | + return ResponseEntity.ok(requestSummary); |
| 339 | + } else { |
| 340 | + return new ResponseEntity<>(requestSummary, HttpStatus.MULTI_STATUS); |
| 341 | + } |
302 | 342 | } |
303 | 343 | } |
0 commit comments