Skip to content

Commit

Permalink
Fix regex Resource and id extraction from Form
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Nov 10, 2023
1 parent 8e63cc8 commit 202921e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ResourceCreationDto {
private NoDeployableResourceType resourceType;
@FormParam("path")
@Nullable
@Pattern(regexp = "^(?!/).*(?<!/)$", message = "String must not start or end with '/'")
@Pattern(regexp = "^(?!/)[a-zA-Z0-9/]+(?<!/)$", message = "String must not start or end with '/' and must not contain white spaces and special characters")
@DefaultValue("")
private String path;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Uni<ResourceDTO> createResource(
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = resourceEntityMapper.toEntityCreation(resourceCreationDto);
return resourceEntityService.createResource(resourceEntity, resourceCreationDto.getFile(),
resourceCreationDto.getFilename(),resourceCreationDto.getPath().trim().replaceAll(" ", "_"))
resourceCreationDto.getFilename(),resourceCreationDto.getPath())
.onItem()
.transformToUni(resource -> Uni.createFrom().item(resourceEntityMapper.toDTO(resource)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package it.gov.pagopa.atmlayer.service.model.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.atmlayer.service.model.enumeration.DeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.core.Response;
Expand All @@ -24,6 +25,18 @@
public class FileUtils {

public static String extractIdValue(File file, DeployableResourceType resourceTypeEnum) {
switch (resourceTypeEnum) {
case BPMN, DMN -> {
return extractIdValueFromXML(file, resourceTypeEnum);
}
case FORM -> {
return extractIdValueFromJson(file, resourceTypeEnum);
}
default -> throw new AtmLayerException("File not supported", Response.Status.NOT_ACCEPTABLE, MALFORMED_FILE);
}
}

public static String extractIdValueFromXML(File file, DeployableResourceType resourceTypeEnum) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
Expand All @@ -36,9 +49,19 @@ public static String extractIdValue(File file, DeployableResourceType resourceTy
}
Element definitionsElement = (Element) document.getElementsByTagName(resourceTypeEnum.getTagName()).item(0);
String definitionKey = definitionsElement.getAttribute(resourceTypeEnum.getAttribute());
if (definitionKey.isBlank()) {
throw new AtmLayerException("Failed to find definition key in the BPMN file", Response.Status.NOT_ACCEPTABLE, BPMN_FILE_DOES_NOT_HAVE_DEFINITION_KEY);
}
if (definitionKey.isBlank()) {
throw new AtmLayerException("Failed to find definition key file", Response.Status.NOT_ACCEPTABLE, BPMN_FILE_DOES_NOT_HAVE_DEFINITION_KEY);
}
return definitionKey;
}

public static String extractIdValueFromJson(File file, DeployableResourceType resourceTypeEnum) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(file);
return jsonNode.get(resourceTypeEnum.getTagName()).asText();
} catch (Exception e) {
throw new AtmLayerException("Malformed File", Response.Status.BAD_REQUEST, MALFORMED_FILE);
}
}
}

0 comments on commit 202921e

Please sign in to comment.