Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: NOD-642 fix conversion #58

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import it.gov.pagopa.fdr.service.history.model.HistoryBlobBody;
import it.gov.pagopa.fdr.service.history.model.JsonSchemaVersionEnum;
import it.gov.pagopa.fdr.util.FileUtil;
import it.gov.pagopa.fdr.util.StringUtil;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -114,14 +116,15 @@ public HistoryBlobBody saveJsonFile(
fdrHistoryEntity.setPaymentList(fdrHistoryPaymentEntityList);
String fileName =
String.format(
"%s_%s_%s.json",
"%s_%s_%s.json.zip",
fdrEntity.getFdr(), fdrEntity.getSender().getPspId(), fdrEntity.getRevision());

try {
objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String fdrHistoryEntityJson = objMapper.writeValueAsString(fdrHistoryEntity);
isJsonValid(fdrHistoryEntityJson, jsonSchema);
BinaryData jsonFile = BinaryData.fromString(fdrHistoryEntityJson);
String compressedFdrHistoryEntityJson = StringUtil.zip(fdrHistoryEntityJson);
BinaryData jsonFile = BinaryData.fromString(compressedFdrHistoryEntityJson);

uploadBlob(fileName, jsonFile);
return HistoryBlobBody.builder()
Expand All @@ -131,7 +134,7 @@ public HistoryBlobBody saveJsonFile(
.fileLength(jsonFile.getLength())
.jsonSchemaVersion(JsonSchemaVersionEnum.valueOf(jsonSchemaVersion))
.build();
} catch (JsonProcessingException e) {
} catch (IOException e) {
logger.error("Error processing fdrHistoryEntity", e);
throw new AppException(AppErrorCodeMessageEnum.FDR_HISTORY_JSON_PROCESSING_ERROR);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/it/gov/pagopa/fdr/service/re/ReService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import it.gov.pagopa.fdr.service.re.model.ReAbstract;
import it.gov.pagopa.fdr.service.re.model.ReInterface;
import it.gov.pagopa.fdr.util.AppConstant;
import it.gov.pagopa.fdr.util.StringUtil;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -104,17 +106,19 @@ public final <T extends ReAbstract> void sendEvent(T... reList) {
private static final DateTimeFormatter dateFormatter =
DateTimeFormatter.ofPattern(PATTERN_DATE_FORMAT).withZone(ZoneId.systemDefault());

public <T extends ReAbstract> void writeBlobIfExist(T re) {
public <T extends ReAbstract> void writeBlobIfExist(T re) throws IOException {
if (re instanceof ReInterface reInterface) {
String bodyStr = reInterface.getPayload();
if (bodyStr != null && !bodyStr.isBlank()) {
String fileName =
String.format(
"%s_%s_%s", re.getSessionId(), re.getFdrAction(), reInterface.getHttpType().name());
"%s_%s_%s.json.zip",
re.getSessionId(), re.getFdrAction(), reInterface.getHttpType().name());

String compressedBody = StringUtil.zip(bodyStr);
BinaryData body =
BinaryData.fromStream(
new ByteArrayInputStream(bodyStr.getBytes(StandardCharsets.UTF_8)));
new ByteArrayInputStream(compressedBody.getBytes(StandardCharsets.UTF_8)));
BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
blobClient.upload(body);

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/it/gov/pagopa/fdr/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.gov.pagopa.fdr.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;

public class StringUtil {

public static String zip(String str) throws IOException {
byte[] strBytes = str.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream bais = new ByteArrayOutputStream(strBytes.length);
GZIPOutputStream gzipOut = new GZIPOutputStream(bais);
gzipOut.write(strBytes);
gzipOut.close();
String compressed = bais.toString(StandardCharsets.UTF_8);
bais.close();
return compressed;
}
}
Loading