Skip to content

Commit

Permalink
refactor: code update
Browse files Browse the repository at this point in the history
  • Loading branch information
Regzox committed Jun 1, 2023
1 parent cf71ef5 commit b0d663e
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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<String> 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<String> 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<String> 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<ProfileResponse> 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<Resource> 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<ProfileResponse> 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<ProfileResponse> 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<ProfileResponse> 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<List<Notice>> getFiles() throws TechnicalException {
List<Notice> 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;
}
}
}

0 comments on commit b0d663e

Please sign in to comment.