Skip to content

Commit

Permalink
Update fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Nov 14, 2023
1 parent 70b1b5d commit a1eba30
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.atmlayer.service.model.enumeration.StatusEnum;
import it.gov.pagopa.atmlayer.service.model.model.BpmnDTO;
import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils;
import it.gov.pagopa.atmlayer.service.model.utils.FileUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand All @@ -26,7 +27,7 @@ public BpmnVersion toEntityCreation(BpmnCreationDto bpmnCreationDto) throws NoSu
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnVersion.setFunctionType(bpmnCreationDto.getFunctionType());
bpmnVersion.setStatus(StatusEnum.CREATED);
bpmnVersion.setSha256(BpmnUtils.calculateSha256(bpmnCreationDto.getFile()));
bpmnVersion.setSha256(FileUtils.calculateSha256(bpmnCreationDto.getFile()));
bpmnVersion.setDeployedFileName(bpmnCreationDto.getFilename().concat(".").concat(S3ResourceTypeEnum.BPMN.getExtension()));
bpmnVersion.setEnabled(true);
return bpmnVersion;
Expand All @@ -46,7 +47,7 @@ public BpmnVersion toEntityUpgrade(BpmnUpgradeDto bpmnUpgradeDto, Long version,
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnVersion.setFunctionType(bpmnUpgradeDto.getFunctionType());
bpmnVersion.setStatus(StatusEnum.CREATED);
bpmnVersion.setSha256(BpmnUtils.calculateSha256(bpmnUpgradeDto.getFile()));
bpmnVersion.setSha256(FileUtils.calculateSha256(bpmnUpgradeDto.getFile()));
bpmnVersion.setDeployedFileName(bpmnUpgradeDto.getFilename().concat(".").concat(S3ResourceTypeEnum.BPMN.getExtension()));
bpmnVersion.setEnabled(true);
bpmnVersion.setModelVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
import it.gov.pagopa.atmlayer.service.model.dto.WorkflowResourceCreationDto;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceFile;
import it.gov.pagopa.atmlayer.service.model.entity.WorkflowResource;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.mapper.WorkflowResourceMapper;
Expand Down Expand Up @@ -109,18 +110,10 @@ public Uni<Void> delete(@PathParam("uuid") UUID uuid) {
@Path("/update/{uuid}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Uni<WorkflowResource> update(@RequestBody(required = true) @FormParam("file") File file,
@PathParam("uuid") UUID uuid) {
return this.workflowResourceService.findById(uuid)
.onItem()
.transformToUni(Unchecked.function(workflowResource -> {
if (workflowResource.isEmpty()) {
throw new AtmLayerException(Response.Status.NOT_FOUND, WORKFLOW_FILE_DOES_NOT_EXIST);
}
return this.workflowResourceService.update(uuid, file, workflowResource.get());
}))
.onItem()
.transformToUni(workflowUpdated -> this.workflowResourceService.findById(uuid))
.onItem().transformToUni(x -> Uni.createFrom().item(x.get()));
public Uni<ResourceFile> update(@RequestBody(required = true) @FormParam("file") File file,
@PathParam("uuid") UUID uuid) throws NoSuchAlgorithmException, IOException {

return this.workflowResourceService.update(uuid, file);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import it.gov.pagopa.atmlayer.service.model.entity.WorkflowResource;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -29,5 +31,5 @@ public interface WorkflowResourceService {

Uni<List<WorkflowResource>> getAll();

Uni<ResourceFile> update(UUID id, File file, WorkflowResource workflowResource);
Uni<ResourceFile> update(UUID id, File file) throws NoSuchAlgorithmException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import it.gov.pagopa.atmlayer.service.model.enumeration.DeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.StatusEnum;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.repository.ResourceFileRepository;
import it.gov.pagopa.atmlayer.service.model.repository.WorkflowResourceRepository;
import it.gov.pagopa.atmlayer.service.model.service.WorkflowResourceService;
import it.gov.pagopa.atmlayer.service.model.service.WorkflowResourceStorageService;
Expand All @@ -24,6 +25,10 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -37,6 +42,7 @@
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_CANNOT_BE_UPDATED;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_FILE_WITH_SAME_CAMUNDA_DEFINITION_KEY_ALREADY_EXISTS;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_FILE_WITH_SAME_CONTENT_ALREADY_EXIST;
import static it.gov.pagopa.atmlayer.service.model.utils.FileUtils.calculateSha256;
import static it.gov.pagopa.atmlayer.service.model.utils.FileUtils.extractIdValue;

@ApplicationScoped
Expand All @@ -46,6 +52,9 @@ public class WorkflowResourceServiceImpl implements WorkflowResourceService {
@Inject
WorkflowResourceRepository workflowResourceRepository;

@Inject
ResourceFileRepository resourceFileRepository;

@Inject
WorkflowResourceStorageService workflowResourceStorageService;

Expand Down Expand Up @@ -255,15 +264,32 @@ public Uni<List<WorkflowResource>> getAll() {

@Override
@WithTransaction
public Uni<ResourceFile> update(UUID id, File file, WorkflowResource workflowResource) {
log.info("Updating Workflow Resource with id {}", id.toString());
DeployableResourceType deployableResourceType = workflowResource.getResourceType();
String definitionKey = extractIdValue(file, deployableResourceType);
String storageKey = workflowResource.getResourceFile().getStorageKey();
log.info("storage key {}", storageKey);
if (!workflowResource.getDefinitionKey().equals(definitionKey)) {
throw new AtmLayerException(String.format("Workflow Resource with type %s does not match the Workflow Resource you are trying to update", definitionKey), Response.Status.BAD_REQUEST, WORKFLOW_RESOURCE_CANNOT_BE_UPDATED);
}
return workflowResourceStorageService.updateFile(workflowResource, file);
public Uni<ResourceFile> update(UUID id, File file) throws NoSuchAlgorithmException, IOException {
return this.findById(id)
.onItem()
.transformToUni(Unchecked.function(workflow -> {
if (workflow.isEmpty()) {
throw new AtmLayerException(Response.Status.NOT_FOUND, WORKFLOW_FILE_DOES_NOT_EXIST);
}
WorkflowResource workflowFound = workflow.get();
log.info("Updating Workflow Resource with id {}", id.toString());
DeployableResourceType deployableResourceType = workflowFound.getResourceType();
String definitionKey = extractIdValue(file, deployableResourceType);
String storageKey = workflowFound.getResourceFile().getStorageKey();
log.info("storage key {}", storageKey);
if (!workflowFound.getDefinitionKey().equals(definitionKey)) {
throw new AtmLayerException(String.format("Workflow Resource with type %s does not match the Workflow Resource you are trying to update", definitionKey), Response.Status.BAD_REQUEST, WORKFLOW_RESOURCE_CANNOT_BE_UPDATED);
}
String shaUpdateFile = calculateSha256(file);
workflowFound.setSha256(shaUpdateFile);
Date date = new Date();
workflowFound.setLastUpdatedAt(new Timestamp(date.getTime()));
workflowFound.getResourceFile().setLastUpdatedAt(new Timestamp(date.getTime()));
return workflowResourceRepository.persist(workflowFound)
.onItem()
.transformToUni(x -> workflowResourceStorageService.updateFile(workflowFound, file));
}));


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class BpmnDtoMapper {
// }

public static String calculateSha256(File file) throws NoSuchAlgorithmException, IOException {
byte[] array = BpmnUtils.toSha256ByteArray(file);
return BpmnUtils.toHexString(array);
byte[] array = FileUtils.toSha256ByteArray(file);
return FileUtils.toHexString(array);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.gov.pagopa.atmlayer.service.model.utils;

import com.google.common.io.Files;
import it.gov.pagopa.atmlayer.service.model.dto.BpmnAssociationDto;
import it.gov.pagopa.atmlayer.service.model.dto.BranchConfigs;
import it.gov.pagopa.atmlayer.service.model.dto.TerminalConfigs;
Expand All @@ -10,14 +9,7 @@
import it.gov.pagopa.atmlayer.service.model.enumeration.BankConfigUtilityValues;
import jakarta.enterprise.context.ApplicationScoped;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -27,41 +19,6 @@
@ApplicationScoped
public class BpmnUtils {

public static byte[] fileToByteArray(File file) throws IOException {
return Files.toByteArray(file);
}

public static String calculateSha256(File file) throws NoSuchAlgorithmException, IOException {
byte[] array = BpmnUtils.toSha256ByteArray(file);
return BpmnUtils.toHexString(array);
}

public static byte[] encodeToBase64(byte[] array) {
return Base64.getEncoder().encode(array);
}

public static byte[] toSha256ByteArray(File file) throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return digest.digest(Files.toByteArray(file));
}

public static byte[] base64ToByteArray(String base64) {
return Base64.getDecoder().decode(base64);
}

public static String toHexString(byte[] hash) {
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 64) {
hexString.insert(0, '0');
}
return hexString.toString();
}

public static String byteArrayToString(byte[] byteArray) {
return new String(byteArray, StandardCharsets.UTF_8);
}

public static Set<BpmnVersionPK> extractBpmnUUIDFromAssociations(List<BpmnBankConfig> associations) {
return associations.stream().map(association -> BpmnVersionPK.builder()
.bpmnId(association.getBpmnBankConfigPK().getBpmnId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;
import it.gov.pagopa.atmlayer.service.model.enumeration.DeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -16,6 +17,11 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.BPMN_FILE_DOES_NOT_HAVE_DEFINITION_KEY;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.MALFORMED_FILE;
Expand Down Expand Up @@ -64,4 +70,39 @@ public static String extractIdValueFromJson(File file, DeployableResourceType re
throw new AtmLayerException("Malformed File", Response.Status.BAD_REQUEST, MALFORMED_FILE);
}
}

public static byte[] fileToByteArray(File file) throws IOException {
return Files.toByteArray(file);
}

public static String calculateSha256(File file) throws NoSuchAlgorithmException, IOException {
byte[] array = toSha256ByteArray(file);
return toHexString(array);
}

public static byte[] encodeToBase64(byte[] array) {
return Base64.getEncoder().encode(array);
}

public static byte[] toSha256ByteArray(File file) throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return digest.digest(Files.toByteArray(file));
}

public static byte[] base64ToByteArray(String base64) {
return Base64.getDecoder().decode(base64);
}

public static String toHexString(byte[] hash) {
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 64) {
hexString.insert(0, '0');
}
return hexString.toString();
}

public static String byteArrayToString(byte[] byteArray) {
return new String(byteArray, StandardCharsets.UTF_8);
}
}
Loading

0 comments on commit a1eba30

Please sign in to comment.