diff --git a/charts/prl-dgs/values.preview.template.yaml b/charts/prl-dgs/values.preview.template.yaml index 9cc70731..3020f7c0 100644 --- a/charts/prl-dgs/values.preview.template.yaml +++ b/charts/prl-dgs/values.preview.template.yaml @@ -21,7 +21,7 @@ java: MANAGEMENT_ENDPOINT_HEALTH_CACHE_TIMETOLIVE: "30000" DOCMOSIS_SERVICE_DEV_MODE_FLAG: "true" PDF_TEST_ENABLED: "true" - CCD_CASE_DOCS_AM_API: "http://ccd-case-document-am-api-aat.service.core-compute-aat.internal" + CCD_CASE_DOCS_AM_API: http://prl-ccd-definitions-pr-1730-cdam AUTH_PROVIDER_SERVICE_CLIENT_BASEURL: "http://rpe-service-auth-provider-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal" IDAM_S2S_URL: "http://rpe-service-auth-provider-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal" DOCMOSIS_SERVICE_BASE_URL: "https://docmosis.aat.platform.hmcts.net" diff --git a/config/owasp/suppressions.xml b/config/owasp/suppressions.xml index bbddb65d..5c402b70 100644 --- a/config/owasp/suppressions.xml +++ b/config/owasp/suppressions.xml @@ -1,7 +1,21 @@ - - + + CVE-2022-33915 + + + + CVE-2022-34305 + + + + CVE-2023-41080 + + + CVE-2016-1000027 @@ -11,6 +25,20 @@ CVE-2023-35116 CVE-2022-45688 + + + CVE-2023-42794 + CVE-2023-42795 + CVE-2023-45648 + + + + CVE-2023-42794 + CVE-2023-42795 + CVE-2023-45648 + diff --git a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorController.java b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorController.java index 19c65e3f..6033d72a 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorController.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorController.java @@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; @@ -86,9 +87,27 @@ public GeneratedDocumentInfo generateAndUploadDraftPdf( GenerateDocumentRequest templateData) { //This service is internal to Divorce system. No need to do service authentication here log.info("Document generation requested with templateName [{}], placeholders map of size[{}]", - templateData.getTemplate(), templateData.getValues().size()); + templateData.getTemplate(), templateData.getValues().size() + ); return documentManagementService.generateAndStoreDraftDocument(templateData.getTemplate(), - templateData.getValues(), authorizationToken); + templateData.getValues(), authorizationToken + ); + } + + @PostMapping("/convertDocToPdf/{fileName}") + public GeneratedDocumentInfo convertDocumentToPdf( + @PathVariable("fileName") String fileName, + @RequestHeader(value = "Authorization", required = false) + String authorizationToken, + @Parameter(name = "GenerateDocumentRequest", description = "JSON object containing the " + + "templateName and the placeholder text map", required = true) + @RequestBody + @Valid + GenerateDocumentRequest templateData) { + //This service is internal to Divorce system. No need to do service authentication here + log.info("convertDocumentToPdf is getting called"); + return documentManagementService.converToPdf( + templateData.getValues(), authorizationToken, fileName); } } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/DocumentManagementService.java b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/DocumentManagementService.java index 961dc5ed..745edc15 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/DocumentManagementService.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/DocumentManagementService.java @@ -9,9 +9,11 @@ GeneratedDocumentInfo generateAndStoreDocument(String templateName, Map placeholders, - String authorizationToken); + String authorizationToken); GeneratedDocumentInfo storeDocument(byte[] document, String authorizationToken, String fileName); byte[] generateDocument(String templateName, Map placeholders); + + GeneratedDocumentInfo converToPdf(Map placeholders, String authorizationToken, String filename); } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/PDFGenerationService.java b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/PDFGenerationService.java index 30d5473b..a10d2795 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/PDFGenerationService.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/PDFGenerationService.java @@ -6,4 +6,6 @@ public interface PDFGenerationService { byte[] generate(String templateName, Map placeholders); + byte[] converToPdf(Map placeholders, String fileName); + } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPDFGenerationServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPDFGenerationServiceImpl.java index 13f6b022..9cc11dd6 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPDFGenerationServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPDFGenerationServiceImpl.java @@ -1,20 +1,28 @@ package uk.gov.hmcts.reform.prl.documentgenerator.service.impl; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.io.Files; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import uk.gov.hmcts.reform.prl.documentgenerator.domain.request.PdfDocumentRequest; import uk.gov.hmcts.reform.prl.documentgenerator.exception.PDFGenerationException; import uk.gov.hmcts.reform.prl.documentgenerator.mapper.TemplateDataMapper; import uk.gov.hmcts.reform.prl.documentgenerator.service.PDFGenerationService; +import java.io.File; +import java.io.IOException; import java.util.Map; import static com.google.common.base.Preconditions.checkArgument; @@ -40,6 +48,9 @@ public class DocmosisPDFGenerationServiceImpl implements PDFGenerationService { @Value("${docmosis.service.pdf-service.devMode}") private String docmosisDevMode; + @Value("${docmosis.service.pdf-service.convert}") + private String docmosisPdfConvertEndpoint; + @Override public byte[] generate(String templateName, Map placeholders) { checkArgument(!isNullOrEmpty(templateName), "document generation template cannot be empty"); @@ -76,4 +87,44 @@ private PdfDocumentRequest request(String templateName, Map plac .build(); } + @Override + public byte[] converToPdf(Map placeholders, String fileName) { + + try { + String filename = FilenameUtils.getBaseName(fileName) + ".pdf"; + ObjectMapper objectMapper = new ObjectMapper(); + byte[] docInBytes = objectMapper.convertValue(placeholders.get("fileName"), byte[].class); + File file = new File(fileName); + Files.write(docInBytes, file); + + return restTemplate + .postForObject( + docmosisPdfConvertEndpoint, + createRequest(file, filename), + byte[].class + ); + + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + + private HttpEntity> createRequest( + File file, + String outputFilename + ) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("accessKey", docmosisPdfServiceAccessKey); + body.add("outputName", outputFilename); + body.add("outputFormat", "pdf"); + body.add("file", new FileSystemResource(file)); + + return new HttpEntity<>(body, headers); + } + } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImpl.java index 1500860e..6661f60d 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImpl.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator; @@ -127,7 +128,8 @@ public GeneratedDocumentInfo storeDocument(byte[] document, String authorization @Override public byte[] generateDocument(String templateName, Map placeholders) { log.debug("Generate document requested with templateName [{}], placeholders of size[{}]", - templateName, placeholders.size()); + templateName, placeholders.size() + ); return generatorService.generate(templateName, placeholders); } @@ -136,4 +138,15 @@ private String getCaseId(Map placeholders) { Map caseDetails = (Map) placeholders.getOrDefault("caseDetails", emptyMap()); return (String) caseDetails.get("id"); } + + @Override + public GeneratedDocumentInfo converToPdf(Map placeholders, String authorizationToken, String fileName) { + log.debug( + "Generate document requested with templateName [{}], placeholders of size[{}]", + placeholders.size() + ); + + byte[] generatedDocument = generatorService.converToPdf(placeholders, fileName); + return storeDocument(generatedDocument, authorizationToken, FilenameUtils.getBaseName(fileName) + ".pdf"); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0c469f32..9e9d9a1c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,8 +12,10 @@ docmosis: pdf-service: accessKey: ${DOCMOSIS_SERVICE_ACCESS_KEY:replace_me} renderEndpoint: "/rs/render" + convertEndpoint: "/rs/convert" uri: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}${docmosis.service.pdf-service.renderEndpoint} health: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}/health + convert: ${DOCMOSIS_SERVICE_BASE_URL:http://localhost:5501}${docmosis.service.pdf-service.convertEndpoint} devMode: ${DOCMOSIS_SERVICE_DEV_MODE_FLAG:false} case_document_am: diff --git a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorControllerUTest.java b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorControllerUTest.java index f3360243..99716ed7 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorControllerUTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/controller/DocumentGeneratorControllerUTest.java @@ -10,6 +10,7 @@ import uk.gov.hmcts.reform.prl.documentgenerator.service.DocumentManagementService; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -64,4 +65,23 @@ public void whenGeneratePDF_thenReturnGeneratedDraftPDFDocumentInfo() { .generateAndStoreDraftDocument(templateName, placeholder, "testToken"); } + @Test + public void whenConvertPDF_thenReturnConvertedPDFDocumentInfo() { + final String templateName = "templateName"; + Map placeholder = new HashMap<>(); + + final GeneratedDocumentInfo expected = GeneratedDocumentInfo.builder().build(); + + when(documentManagementService.converToPdf(placeholder, "authToken","fileName")) + .thenReturn(expected); + + GeneratedDocumentInfo actual = classUnderTest + .convertDocumentToPdf("fileName","authToken", new GenerateDocumentRequest(templateName, placeholder)); + + assertEquals(expected, actual); + + verify(documentManagementService, times(1)) + .converToPdf(placeholder, "authToken","fileName"); + } + } diff --git a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPdfGenerationServiceImplUTest.java b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPdfGenerationServiceImplUTest.java index afed78bd..9df07550 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPdfGenerationServiceImplUTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocmosisPdfGenerationServiceImplUTest.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.reform.prl.documentgenerator.service.impl; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Assert; import org.junit.Before; @@ -24,10 +25,12 @@ import uk.gov.hmcts.reform.prl.documentgenerator.util.NullOrEmptyValidator; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class DocmosisPdfGenerationServiceImplUTest { @@ -41,6 +44,9 @@ public class DocmosisPdfGenerationServiceImplUTest { @Mock private TemplateDataMapper templateDataMapper; + @Mock + ObjectMapper objectMapper; + @InjectMocks @Spy @@ -49,6 +55,9 @@ public class DocmosisPdfGenerationServiceImplUTest { @Before public void before() throws IllegalAccessException { FieldUtils.writeField(classUnderTest, "docmosisPdfServiceEndpoint", "test", true); + FieldUtils.writeField(classUnderTest, "docmosisPdfConvertEndpoint", "test", true); + FieldUtils.writeField(classUnderTest, "docmosisPdfServiceAccessKey", "test", true); + } @Test @@ -99,4 +108,23 @@ public void givenHttpRequestGoesThrough_whenGenerateFromHtml_thenReturnProperRes Assert.assertNotNull(expected); } + @Test + public void givenFileNAme_whenConvertPDf_thenReturnProperResponse() throws Exception { + final Map placeholders = new HashMap<>(); + + byte[] test = "Any String you want".getBytes(); + placeholders.put("fileName",test); + + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + Mockito.when(restTemplate.postForObject(ArgumentMatchers.any(String.class), + ArgumentMatchers.any(), + ArgumentMatchers.>any())).thenReturn(test); + + + byte[] expected = classUnderTest.converToPdf(placeholders,"testFile"); + + Assert.assertNotNull(expected); + } + } diff --git a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImplTest.java index bd256530..3b24f7bf 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/documentgenerator/service/impl/DocumentManagementServiceImplTest.java @@ -109,6 +109,25 @@ public void testGenerateAndStoreDraftDocument_WithDraftPrefixMock() { assertThat("Draft value set ", value.get(IS_DRAFT), is(true)); } + @Test + public void testConvertToPdf() { + final Map placeholders = new HashMap<>(); + + byte[] test = "Any String you want".getBytes(); + placeholders.put("fileName",test); + Mockito.when(authTokenGenerator.generate()).thenReturn(s2sToken); + Mockito.when(pdfGenerationService.converToPdf(placeholders,"fileName")).thenReturn(test); + + Mockito.when(caseDocumentClient.uploadDocuments(eq(authToken), + eq(s2sToken), eq("PRLAPPS"), eq("PRIVATELAW"), any())) + .thenReturn(uploadResponse); + + classUnderTest.converToPdf(placeholders, authToken,"fileName"); + + verify(pdfGenerationService).converToPdf(placeholders,"fileName"); + } + + @Test public void testGenerateAndStoreFinalDocument_WithDynamicFileName() { placeholderMap.put("dynamic_fileName","test-file.pdf");