From b0d663efa5339594750980f70ddd0827329f4432 Mon Sep 17 00:00:00 2001 From: Daniel Radeau Date: Thu, 4 May 2023 11:16:01 +0200 Subject: [PATCH] refactor: code update --- .../config/PastisConfiguration.java | 20 +- .../controller/PastisController.java | 196 ++++++++++++------ 2 files changed, 137 insertions(+), 79 deletions(-) diff --git a/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/config/PastisConfiguration.java b/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/config/PastisConfiguration.java index 0d405eeba15..14e5c50837c 100644 --- a/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/config/PastisConfiguration.java +++ b/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/config/PastisConfiguration.java @@ -47,20 +47,18 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ResourceLoader; -import org.springframework.http.HttpStatus; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import java.security.NoSuchAlgorithmException; -import java.util.Collections; +import static java.util.Collections.emptyMap; +import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.OK; @Configuration public class PastisConfiguration { - private ResourceLoader resourceLoader; - @Value("${cors.allowed-origins}") private String origins; @@ -69,18 +67,15 @@ public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(@NotNull CorsRegistry registry) { - registry.addMapping("/**") - .allowedOrigins(origins.split(",")) - .allowCredentials(true); + registry.addMapping("/**").allowedOrigins(origins.split(",")).allowCredentials(true); } }; } @Bean public ErrorViewResolver customErrorViewResolver() { - final ModelAndView redirectToIndexHtml = - new ModelAndView("forward:/index.html", Collections.emptyMap(), HttpStatus.OK); - return (request, status, model) -> status == HttpStatus.NOT_FOUND ? redirectToIndexHtml : null; + final ModelAndView redirectToIndexHtml = new ModelAndView("forward:/index.html", emptyMap(), OK); + return (request, status, model) -> status == NOT_FOUND ? redirectToIndexHtml : null; } @Bean @@ -95,12 +90,11 @@ public PuaFromJSON puaFromJSON() { @Bean public PastisService pastisService() { - return new PastisService(this.resourceLoader, puaPastisValidator(),jsonFromPUA(), puaFromJSON()); + return new PastisService(this.resourceLoader, puaPastisValidator(), jsonFromPUA(), puaFromJSON()); } @Bean public PuaPastisValidator puaPastisValidator() { return new PuaPastisValidator(); } - } diff --git a/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/controller/PastisController.java b/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/controller/PastisController.java index ec910da566a..22bb6a40958 100644 --- a/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/controller/PastisController.java +++ b/api/api-pastis/pastis-standalone/src/main/java/fr/gouv/vitamui/pastis/standalone/controller/PastisController.java @@ -38,7 +38,7 @@ package fr.gouv.vitamui.pastis.standalone.controller; -import fr.gouv.vitam.common.StringUtils; +import fr.gouv.vitamui.commons.utils.VitamUIStringUtils; import fr.gouv.vitamui.pastis.common.dto.ElementProperties; import fr.gouv.vitamui.pastis.common.dto.profiles.Notice; import fr.gouv.vitamui.pastis.common.dto.profiles.ProfileNotice; @@ -54,27 +54,41 @@ import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.server.ResponseStatusException; import java.security.NoSuchAlgorithmException; import java.util.List; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; -@OpenAPIDefinition(tags = {@Tag(name = "pastis")}, + +@OpenAPIDefinition( + tags = { + @Tag(name = "pastis") + }, info = @Info(title = "Pastis Rest Api"), - servers = {@Server(url = "localhost", - variables = {@ServerVariable(name = "scheme", allowableValues = {"https", "http"}, defaultValue = "http"), - @ServerVariable(name = "port", description = "Api port", defaultValue = "8096")})}) + servers = { + @Server(url = "localhost", variables = { + @ServerVariable( + name = "scheme", + allowableValues = {"https", "http"}, + defaultValue = "http" + ), + @ServerVariable( + name = "port", + description = "Api port", + defaultValue = "8096" + ) + }) + } +) @RestController class PastisController { + private static final boolean IS_STANDALONE = true; private static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8"; @@ -85,103 +99,153 @@ public PastisController(final PastisService profileService) { this.profileService = profileService; } - @Operation(summary = "Retrieve RNG representation of the JSON structure", + @Operation( + summary = "Retrieve RNG representation of the JSON structure", description = "Retrieve RNG representation of the JSON structure of archive profile", - tags = {"pastis"}) - @PostMapping(value = RestApi.PASTIS_DOWNLOAD_PA, consumes = APPLICATION_JSON_UTF8, produces = MediaType.APPLICATION_XML_VALUE) + tags = {"pastis"} + ) + @PostMapping( + value = RestApi.PASTIS_DOWNLOAD_PA, + consumes = APPLICATION_JSON_UTF8, + produces = MediaType.APPLICATION_XML_VALUE + ) ResponseEntity getArchiveProfile(@RequestBody final ElementProperties json) throws TechnicalException { - String pa = profileService.getArchiveProfile(json); - if (pa != null) { - return ResponseEntity.ok(pa); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + final String pa = profileService.getArchiveProfile(json); + + if (pa == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.NO_ARCHIVE_PROFILE.toString()); } + + return ResponseEntity.ok(pa); } - @Operation(summary = "Retrieve JSON representation of archive unit profile", + @Operation( + summary = "Retrieve JSON representation of archive unit profile", description = "Retrieve JSON representation of archive unit profile", - tags = {"pastis"}) - @PostMapping(value = RestApi.PASTIS_DOWNLOAD_PUA, consumes = APPLICATION_JSON_UTF8, produces = MediaType.APPLICATION_JSON_VALUE) - ResponseEntity getArchiveUnitProfile(@RequestBody final ProfileNotice json) throws TechnicalException { - String pua = profileService.getArchiveUnitProfile(json, true); - if (pua != null) { - return ResponseEntity.ok(pua); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + tags = {"pastis"} + ) + @PostMapping( + value = RestApi.PASTIS_DOWNLOAD_PUA, + consumes = APPLICATION_JSON_UTF8, + produces = MediaType.APPLICATION_JSON_VALUE + ) + ResponseEntity getArchiveUnitProfile(@RequestBody final ProfileNotice json) throws TechnicalException { + final String pua = profileService.getArchiveUnitProfile(json, IS_STANDALONE); + + if (pua == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.NO_ARCHIVE_UNIT_PROFILE.toString()); } + return ResponseEntity.ok(pua); } - @Operation(summary = "Retrieve JSON representation of the RNG structure", + @Operation( + summary = "Retrieve JSON representation of the RNG structure", description = "Retrieve JSON representation of the RNG structure", - tags = {"pastis"}) + tags = {"pastis"} + ) @GetMapping(value = RestApi.PASTIS_CREATE_PROFILE) ResponseEntity createProfile(@RequestParam(name = "type") String profileType) throws NoSuchAlgorithmException, TechnicalException { - ProfileResponse profileResponse = profileService.createProfile(profileType, true); - if (profileResponse != null) { - return ResponseEntity.ok(profileResponse); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + final ProfileResponse profileResponse = profileService.createProfile(profileType, IS_STANDALONE); + + if (profileResponse == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.NO_PROFILE_RESPONSE.toString()); } + + return ResponseEntity.ok(profileResponse); } @GetMapping(value = RestApi.PASTIS_GET_PROFILE_FILE) ResponseEntity getFile(@RequestParam(name = "name") String filename) { - Resource resource = profileService.getFile(filename); - if(!isValidFileName(filename)) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + final Resource resource = profileService.getFile(filename); + + if (isInvalidFilename(filename)) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.INVALID_FILE_NAME.toString()); } - if (resource != null) { - return ResponseEntity.ok(resource); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + if (resource == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.RESOURCE_NOT_FOUND.toString()); } + + return ResponseEntity.ok(resource); } - @Operation(summary = "Transform profile JSON representation from Notice", + @Operation( + summary = "Transform profile JSON representation from Notice", description = "Transform profile JSON representation from Notice", - tags = {"pastis"}) + tags = {"pastis"} + ) @PostMapping(value = RestApi.PASTIS_TRANSFORM_PROFILE) ResponseEntity loadProfile(@RequestBody final Notice notice) throws TechnicalException { - ProfileResponse profileResponse = profileService.loadProfile(notice); - if (profileResponse != null) { - return ResponseEntity.ok(profileResponse); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + final ProfileResponse profileResponse = profileService.loadProfile(notice); + + if (profileResponse == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.NO_PROFILE_RESPONSE.toString()); } + + return ResponseEntity.ok(profileResponse); } - @Operation(summary = "Upload profile PA or PUA", + @Operation( + summary = "Upload profile PA or PUA", description = "Upload profile PA or PUA", - tags = {"pastis"}) - @PostMapping(value = RestApi.PASTIS_UPLOAD_PROFILE, - consumes = "multipart/form-data", produces = "application/json") - ResponseEntity loadProfileFromFile(@RequestParam MultipartFile file) throws NoSuchAlgorithmException, TechnicalException { - String originalFileName = file.getOriginalFilename(); - isValidFileName(originalFileName); - ProfileResponse profileResponse = profileService.loadProfileFromFile(file, originalFileName,true); - if (profileResponse != null) { - return ResponseEntity.ok(profileResponse); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + tags = {"pastis"} + ) + @PostMapping( + value = RestApi.PASTIS_UPLOAD_PROFILE, + consumes = "multipart/form-data", + produces = "application/json" + ) + ResponseEntity loadProfileFromFile(@RequestParam MultipartFile multipartFile) throws NoSuchAlgorithmException, TechnicalException { + final String originalFileName = multipartFile.getOriginalFilename(); + + if (isInvalidFilename(originalFileName)) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.INVALID_FILE_NAME.toString()); } + + final ProfileResponse profileResponse = profileService.loadProfileFromFile(multipartFile, originalFileName, IS_STANDALONE); + + if (profileResponse == null) { + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ErrorMessage.NO_PROFILE_RESPONSE.toString()); + } + + return ResponseEntity.ok(profileResponse); } - @Operation(summary = "Retrieve all profiles PA and PUA", + @Operation( + summary = "Retrieve all profiles PA and PUA", description = "Retrieve all profiles PA and PUA", - tags = {"pastis"}) + tags = {"pastis"} + ) @GetMapping(value = RestApi.PASTIS_GET_ALL_PROFILES) ResponseEntity> getFiles() throws TechnicalException { List notices = profileService.getFiles(); if (!notices.isEmpty()) { return ResponseEntity.ok(notices); } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(INTERNAL_SERVER_ERROR); } } - private static boolean isValidFileName(String fileName) { - return !StringUtils.HTML_PATTERN.matcher(fileName).find(); + private static boolean isInvalidFilename(String fileName) { + return VitamUIStringUtils.HTML_PATTERN.matcher(fileName).find(); } + enum ErrorMessage { + INVALID_FILE_NAME("Invalid file name"), + NO_PROFILE_RESPONSE("No profile response"), + RESOURCE_NOT_FOUND("Resource not found"), + NO_ARCHIVE_PROFILE("No archive profile"), + NO_ARCHIVE_UNIT_PROFILE("No archive unit profile"); + + private final String name; + + ErrorMessage(String name) { + this.name = name; + } + + @Override + public String toString() { + return this.name; + } + } }