Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 39 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@
<apiPackage>it.pagopa.pn.workflowmanager.generated.openapi.msclient.ioconnector.api</apiPackage>
</configuration>
</execution>

<!-- OPEN API CLIENT PN-EXTERNAL-CHANNELS -->
<execution>
<id>generate-client-pn-external-channels</id>
Expand All @@ -434,7 +433,6 @@
<useOneOfInterfaces>true</useOneOfInterfaces>
<useJakartaEe>true</useJakartaEe>
<swaggerAnnotations>false</swaggerAnnotations>
<openApiNullable>false</openApiNullable>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<annotationLibrary>none</annotationLibrary>
Expand All @@ -446,9 +444,45 @@
<modelPackage>it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.model</modelPackage>
<apiPackage>it.pagopa.pn.workflowmanager.generated.openapi.msclient.externalchannels.api</apiPackage>
</configuration>
</execution>

</executions>
</execution>
<!-- OPEN API CLIENT PN-SAFESTORAGE -->
<execution>
<id>generate-client-pn-safestorage</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<inputSpec>
https://raw.githubusercontent.com/pagopa/pn-ss/5fefc1970a9747d08e2ee8989900e0e90dc5532d/docs/openapi/pn-safestorage-v1.1-api.yaml
</inputSpec>
<templateDirectory>${project.build.directory}/dependency-resources/scripts/openapi/templates/7.4.0/client</templateDirectory>
<generatorName>java</generatorName>
<library>resttemplate</library>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<additionalProperties>removeEnumValuePrefix=false</additionalProperties>
<typeMappings>
<typeMapping>OffsetDateTime=java.time.Instant</typeMapping>
</typeMappings>
<configOptions>
<useOneOfInterfaces>true</useOneOfInterfaces>
<useJakartaEe>true</useJakartaEe>
<swaggerAnnotations>false</swaggerAnnotations>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<annotationLibrary>none</annotationLibrary>
<documentationProvider>source</documentationProvider>
<openApiNullable>false</openApiNullable>
<skipDefaultInterface>false</skipDefaultInterface>
<useTags>true</useTags>
</configOptions>
<modelPackage>it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model</modelPackage>
<apiPackage>it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.api</apiPackage>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class PnWorkflowManagerConfigs {
private String deliveryBaseUrl;
private String templateEngineBaseUrl;
private String ioConnectorBaseUrl;
private String safeStorageBaseUrl;
private String paperMessagesClientBaseUrl;
private String externalChannelsBaseUrl;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.pagopa.pn.workflowmanager.config.msclient;

import it.pagopa.pn.workflowmanager.config.PnWorkflowManagerConfigs;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.ApiClient;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.api.FileDownloadApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.api.FileUploadApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

@Configuration
public class SafeStorageApiConfigurator {
@Bean
@Primary
public FileUploadApi fileUploadApi(@Qualifier("withTracing") RestTemplate restTemplate, PnWorkflowManagerConfigs cfg){
ApiClient newApiClient = new ApiClient(restTemplate);
newApiClient.setBasePath(cfg.getSafeStorageBaseUrl());
return new FileUploadApi( newApiClient );
}

@Bean
@Primary
public FileDownloadApi fileDownloadApi(@Qualifier("withTracing") RestTemplate restTemplate, PnWorkflowManagerConfigs cfg){
ApiClient newApiClient = new ApiClient(restTemplate);
newApiClient.setBasePath(cfg.getSafeStorageBaseUrl());
return new FileDownloadApi( newApiClient );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;

public enum DocumentType {
COVERPAGE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;
import lombok.*;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder(toBuilder = true)
@EqualsAndHashCode
public class FileCreationResponseInt {
private String key;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;

import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileCreationRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class FileCreationWithContentRequest extends FileCreationRequest {
private byte[] content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;

import lombok.*;

import java.math.BigDecimal;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder(toBuilder = true)
@EqualsAndHashCode
public class FileDownloadInfoInt {
private String url;
private BigDecimal retryAfter;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;

import lombok.*;

import java.math.BigDecimal;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder(toBuilder = true)
@EqualsAndHashCode
public class FileDownloadResponseInt {
private String key;
private String checksum;
private BigDecimal contentLength;
private String contentType;
private FileDownloadInfoInt download;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;

import lombok.Getter;

@Getter
public enum FileTagEnumInt {
DOCUMENT("DOCUMENT"),

ATTACHMENT_PAGOPA("ATTACHMENT_PAGOPA");

private final String value;

FileTagEnumInt(String value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.pagopa.pn.workflowmanager.dto.safestorage;
import lombok.*;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder(toBuilder = true)
@EqualsAndHashCode
@ToString
public class UpdateFileMetadataResponseInt {
private String resultCode;

private String resultDescription;

private List<String> errorList = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class WorkflowManagerExceptionCodes extends PnExceptionsCodes {
public static final String ERROR_CODE_WORKFLOWMANAGER_ROUTER_HANDLER_NOT_FOUND = "PN_WORKFLOWMANAGER_ROUTER_NOT_FOUND";
public static final String ERROR_CODE_WORKFLOWMANAGER_HANDLEEVENTFAILED = "PN_WORKFLOWMANAGER_HANDLEEVENTFAILED";
public static final String ERROR_CODE_DELIVERYPUSH_ACTION_CONFLICT = "PN_ACTIONMANAGER_ERROR_CODE_ACTION_CONFLICT";
public static final String ERROR_CODE_WORKFLOWMANAGER_SAVELEGALFACTSFAILED = "PN_WORKFLOWMANAGER_SAVELEGALFACTSFAILED";
public static final String ERROR_CODE_WORKFLOWMANAGER_UPLOADFILEERROR = "PN_WORKFLOWMANAGER_UPLOADFILEERROR";
public static final String ERROR_CODE_WORKFLOWMANAGER_PAPERCHANNELSENDCOSTCHANGED = "PN_WORKFLOWMANAGER_PAPERCHANNELSENDCOSTCHANGED";
public static final String ERROR_CODE_WORKFLOWMANAGER_SENDPECNOTIFICATIONFAILED = "PN_WORKFLOWMANAGER_SENDPECNOTIFICATIONFAILED";
public static final String ERROR_CODE_WORKFLOWMANAGER_CONFIGURATION_NOT_FOUND = "PN_WORKFLOWMANAGER_CONFIGURATIONNOTFOUND";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.pagopa.pn.workflowmanager.middleware.externalclient.pnclient.safestorage;

import it.pagopa.pn.commons.log.PnLogger;
import it.pagopa.pn.workflowmanager.dto.safestorage.FileCreationWithContentRequest;
import it.pagopa.pn.workflowmanager.dto.safestorage.UpdateFileMetadataResponseInt;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileCreationRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileCreationResponse;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileDownloadResponse;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.UpdateFileMetadataRequest;

public interface PnSafeStorageClient {
String CLIENT_NAME = PnLogger.EXTERNAL_SERVICES.PN_SAFE_STORAGE;

String CREATE_FILE = "CREATE FILE";
String GET_FILE = "GET FILE";
String SAFE_STORAGE_URL_PREFIX = "safestorage://";
String UPLOAD_FILE_CONTENT = "UPLOAD FILE CONTENT";

FileCreationResponse createFile(String checksumValue, String checksum, FileCreationRequest fileCreationRequest);

FileDownloadResponse getFile(String fileKey, Boolean metadataOnly, Boolean tags);

UpdateFileMetadataResponseInt updateFileMetadata(String fileKey, UpdateFileMetadataRequest updateFileMetadataRequest);

void uploadContent(FileCreationWithContentRequest fileCreationRequest, FileCreationResponse fileCreationResponse, String sha256);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package it.pagopa.pn.workflowmanager.middleware.externalclient.pnclient.safestorage;

import it.pagopa.pn.commons.exceptions.PnInternalException;
import it.pagopa.pn.workflowmanager.config.PnWorkflowManagerConfigs;
import it.pagopa.pn.workflowmanager.dto.safestorage.FileCreationWithContentRequest;
import it.pagopa.pn.workflowmanager.dto.safestorage.UpdateFileMetadataResponseInt;
import it.pagopa.pn.workflowmanager.exceptions.WorkflowManagerExceptionCodes;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.api.FileDownloadApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.api.FileUploadApi;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileCreationRequest;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileCreationResponse;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.FileDownloadResponse;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.safestorage.model.UpdateFileMetadataRequest;
import lombok.CustomLog;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@CustomLog
@RequiredArgsConstructor
@Component
public class PnSafeStorageClientImpl implements PnSafeStorageClient {
private final FileUploadApi fileUploadApi;
private final FileDownloadApi fileDownloadApi;
private final PnWorkflowManagerConfigs cfg;
private final RestTemplate restTemplate;

@Override
public FileCreationResponse createFile(String checksumValue, String checksum, FileCreationRequest fileCreationRequest) {
log.logInvokingExternalService(CLIENT_NAME, CREATE_FILE);
return fileUploadApi.createFile(cfg.getCxId(), checksumValue, checksum, fileCreationRequest);
}

@Override
public FileDownloadResponse getFile(String fileKey, Boolean metadataOnly, Boolean tags) {
log.logInvokingExternalService(CLIENT_NAME, GET_FILE);
return fileDownloadApi.getFile(fileKey, cfg.getCxId(), metadataOnly, tags);
}

@Override
public UpdateFileMetadataResponseInt updateFileMetadata(String fileKey, UpdateFileMetadataRequest updateFileMetadataRequest) {
log.logInvokingExternalService(CLIENT_NAME, "UPDATE FILE METADATA");

HttpHeaders headers = new HttpHeaders();
headers.add("x-pagopa-safestorage-cx-id", cfg.getCxId());
HttpEntity<UpdateFileMetadataRequest> requestEntity = new HttpEntity<>(updateFileMetadataRequest, headers);
String url = cfg.getSafeStorageBaseUrl() + "/safe-storage/v1/files/{fileKey}";

try {
ResponseEntity<UpdateFileMetadataResponseInt> response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
UpdateFileMetadataResponseInt.class,
fileKey
);
if (response.getBody() == null) {
throw new PnInternalException("Empty response while updating metadata", WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_UPLOADFILEERROR);
}
return response.getBody();
} catch (RestClientException exception) {
throw new PnInternalException("Cannot update file metadata", WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_UPLOADFILEERROR, exception);
}
}

@Override
public void uploadContent(FileCreationWithContentRequest fileCreationRequest, FileCreationResponse fileCreationResponse, String sha256) {
try {
log.logInvokingAsyncExternalService(CLIENT_NAME, UPLOAD_FILE_CONTENT, fileCreationResponse.getKey());

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-type", fileCreationRequest.getContentType());
headers.add("x-amz-checksum-sha256", sha256);
headers.add("x-amz-meta-secret", fileCreationResponse.getSecret());

HttpEntity<Resource> req = new HttpEntity<>(new ByteArrayResource(fileCreationRequest.getContent()), headers);

URI url = URI.create(fileCreationResponse.getUploadUrl());
HttpMethod method = fileCreationResponse.getUploadMethod() == FileCreationResponse.UploadMethodEnum.POST ? HttpMethod.POST : HttpMethod.PUT;

ResponseEntity<String> res = restTemplate.exchange(url, method, req, String.class);

if (res.getStatusCode().value() != HttpStatus.OK.value())
{
throw new PnInternalException("File upload failed", WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_UPLOADFILEERROR);
}

} catch (PnInternalException ee)
{
log.error("uploadContent PnInternalException uploading file", ee);
throw ee;
}
catch (Exception ee)
{
log.error("uploadContent Exception uploading file", ee);
throw new PnInternalException("Exception uploading file", WorkflowManagerExceptionCodes.ERROR_CODE_WORKFLOWMANAGER_UPLOADFILEERROR, ee);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.templateengine.model.InformalCommunication;
import it.pagopa.pn.workflowmanager.generated.openapi.msclient.templateengine.model.LanguageEnum;

import java.io.File;

public interface TemplateEngineClient {
String CLIENT_NAME = PnLogger.EXTERNAL_SERVICES.PN_TEMPLATE_ENGINE;

Expand All @@ -12,7 +14,9 @@ public interface TemplateEngineClient {
String INFORMAL_IO_COMMUNICATION = "IO INFORMAL COMMUNICATION";

String ioMessageTemplate(LanguageEnum language, InformalCommunication informalCommunication);


File informalAnalogCommunication(LanguageEnum xLanguage, InformalCommunication informalCommunication);

String pecTemplate(LanguageEnum language, InformalCommunication informalCommunication);

String informalIoCommunication(LanguageEnum xLanguage, InformalCommunication informalCommunication);
Expand Down
Loading
Loading