From 30c84de19a4ddf9e07be4b8e5d5c099fc288faf5 Mon Sep 17 00:00:00 2001 From: gkolokythas Date: Fri, 5 Apr 2019 12:20:06 +0300 Subject: [PATCH] Fixes Pdf export for DMP and Dataset. --- .../main/java/eu/eudat/controllers/DMPs.java | 26 ++--------- .../controllers/DatasetWizardController.java | 12 +++-- .../managers/DataManagementPlanManager.java | 19 ++++++-- .../eudat/logic/managers/DatasetManager.java | 45 ++++++------------- .../eudat/logic/managers/DocumentManager.java | 2 +- .../resources/application-devel.properties | 3 +- 6 files changed, 43 insertions(+), 64 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java index d3841eff2..c47cd5371 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java @@ -82,8 +82,8 @@ public class DMPs extends BaseController { @RequestMapping(method = RequestMethod.GET, value = {"{id}"}) public @ResponseBody - ResponseEntity getSingle(@PathVariable String id,@RequestHeader("Content-Type") String contentType, Principal principal) throws IllegalAccessException, InstantiationException, IOException { - if(contentType.equals("application/xml") || contentType.equals("application/msword")){ + ResponseEntity getSingle(@PathVariable String id,@RequestHeader("Content-Type") String contentType, Principal principal) throws IllegalAccessException,InterruptedException, InstantiationException, IOException { + if(contentType.equals("application/xml") || contentType.equals("application/msword")){ //|| contentType.equals("application/pdf") DMPDao dmpDao = this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(); VisibilityRuleService visibilityRuleService = this.getApiContext().getUtilitiesService().getVisibilityRuleService(); ResponseEntity document = this.dataManagementPlanManager.getDocument(id, contentType); @@ -140,26 +140,6 @@ public class DMPs extends BaseController { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>>().status(ApiMessageCode.NO_MESSAGE).payload(dataTable)); } - /*@RequestMapping(method = RequestMethod.GET, value = {"/getWord/{id}"}) - public @ResponseBody - ResponseEntity getWordDocument(@PathVariable String id) throws IOException, IllegalAccessException, InstantiationException { - File file = this.dataManagementPlanManager.getWordDocument(this.environment, this.getApiContext().getOperationsContext().getDatabaseRepository().getDmpDao(), id, this.getApiContext().getUtilitiesService().getVisibilityRuleService()); - InputStream resource = new FileInputStream(file); - System.out.println("Mime Type of " + file.getName() + " is " + - new MimetypesFileTypeMap().getContentType(file)); - HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.setContentLength(file.length()); - responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); - responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getName()); - responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); - responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); - - byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource); - return new ResponseEntity<>(content, - responseHeaders, - HttpStatus.OK); - }*/ - @RequestMapping(method = RequestMethod.GET, value = {"/getPDF/{id}"}) public @ResponseBody ResponseEntity getPDFDocument(@PathVariable String id, @RequestHeader("Content-Type") String contentType) throws IllegalAccessException, IOException, InstantiationException, InterruptedException { @@ -186,7 +166,7 @@ public class DMPs extends BaseController { public ResponseEntity dmpXmlUpload(@RequestParam("file") MultipartFile[] files, Principal principal) throws IOException, JAXBException, Exception { this.dataManagementPlanManager.createDmpFromXml(this.getApiContext(), files, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem() - .status(ApiMessageCode.SUCCESS_MESSAGE).message("Bravo Giorgo")); + .status(ApiMessageCode.SUCCESS_MESSAGE)); } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DatasetWizardController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DatasetWizardController.java index 71ebc9a11..974bad7e9 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DatasetWizardController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DatasetWizardController.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.List; import java.util.UUID; @@ -133,10 +134,13 @@ public class DatasetWizardController extends BaseController { public @ResponseBody ResponseEntity getPDFDocument(@PathVariable String id) throws IllegalAccessException, IOException, InstantiationException, InterruptedException { File file = datasetManager.getWordDocument(this.environment, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService()); - File pdffile = datasetManager.convertToPDF(file, environment, file.getName()); + String fileName = file.getName(); + if (fileName.endsWith(".docx")){ + fileName = fileName.substring(0, fileName.length() - 5); + } + File pdffile = datasetManager.convertToPDF(file, environment, fileName); InputStream resource = new FileInputStream(pdffile); - System.out.println("Mime Type of " + file.getName() + " is " + - new MimetypesFileTypeMap().getContentType(file)); + HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(pdffile.length()); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); @@ -145,6 +149,8 @@ public class DatasetWizardController extends BaseController { responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); byte[] content = IOUtils.toByteArray(resource); + //Files.deleteIfExists(pdffile.toPath()); + //Files.deleteIfExists(file.toPath()); return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK); diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java index 52671a89c..c92e34255 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java @@ -173,13 +173,23 @@ public class DataManagementPlanManager { }); String fileName = dmpEntity.getLabel(); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); - File exportFile = new File( fileName + ".docx"); + File exportFile = new File(environment.getProperty("configuration.exportUrl") + fileName + ".docx"); FileOutputStream out = new FileOutputStream(exportFile); document.write(out); out.close(); + return exportFile; } + /*public File getPdfDocument(String id) throws InstantiationException, IllegalAccessException, InterruptedException, IOException { + File file = this.getWordDocument(id); + String fileName = file.getName(); + if (fileName.endsWith(".docx")){ + fileName = fileName.substring(0, fileName.length() - 5); + } + return this.datasetManager.convertToPDF(file, environment, fileName); + }*/ + public eu.eudat.models.data.dmp.DataManagementPlan getSingle(String id, Principal principal, DynamicProjectConfiguration dynamicProjectConfiguration) throws InstantiationException, IllegalAccessException { DMP dataManagementPlanEntity = databaseRepository.getDmpDao().find(UUID.fromString(id)); if (dataManagementPlanEntity.getCreator().getId() != principal.getId() && dataManagementPlanEntity.getUsers().stream().filter(userInfo -> userInfo.getId() == principal.getId()).collect(Collectors.toList()).size() == 0) @@ -555,7 +565,7 @@ public class DataManagementPlanManager { return fileEnvelope; } - public ResponseEntity getDocument(String id, String contentType) throws InstantiationException, IllegalAccessException, IOException{ + public ResponseEntity getDocument(String id, String contentType) throws InstantiationException, InterruptedException, IllegalAccessException, IOException{ File file; VisibilityRuleService visibilityRuleService = this.utilitiesService.getVisibilityRuleService(); DMPDao dmpDao = databaseRepository.getDmpDao(); @@ -566,12 +576,13 @@ public class DataManagementPlanManager { case "application/msword": file = getWordDocument(id); break; + /*case "application/pdf": + file = getPdfDocument(id); + break;*/ default: file = getXmlDocument(id).getFile(); } InputStream resource = new FileInputStream(file); - System.out.println("Mime Type of " + file.getName() + " is " + - new MimetypesFileTypeMap().getContentType(file)); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(file.length()); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java index 5d09b8594..a7b043b60 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java @@ -34,6 +34,8 @@ import org.springframework.core.env.Environment; import org.springframework.core.io.FileSystemResource; import org.springframework.http.*; import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.client.RestTemplate; @@ -207,7 +209,7 @@ public class DatasetManager { visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules()); wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService); String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", ""); - File exportFile = new File(label + ".docx"); + File exportFile = new File(environment.getProperty("configuration.exportUrl") + label + ".docx"); FileOutputStream out = new FileOutputStream(exportFile); document.write(out); out.close(); @@ -235,45 +237,24 @@ public class DatasetManager { public File convertToPDF(File file, Environment environment, String label) throws IOException, InterruptedException { LinkedMultiValueMap map = new LinkedMultiValueMap<>(); - map.add("file", new FileSystemResource(file)); + map.add("files", new FileSystemResource(file)); + map.add("filename", label + ".pdf"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); - headers.add("Content-disposition", "attachment; filename=" + label + ".docx"); - - headers.add("Content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + headers.add("Content-disposition", "attachment; filename=" + label + ".pdf"); + headers.add("Content-type", "application/pdf"); HttpEntity> requestEntity = new HttpEntity>( map, headers); - Map queueResult = new RestTemplate().postForObject( - environment.getProperty("pdf.converter.url") + - "api/v1/", requestEntity, Map.class); + byte[] queueResult = new RestTemplate().postForObject("http://localhost:3000/convert/office" + , requestEntity, byte[].class); - Map mediaResult = new RestTemplate().getForObject(environment.getProperty("pdf.converter.url") + - "/api/v1/" + queueResult.get("id"), Map.class); - System.out.println("Status: " + mediaResult.get("status")); - while (!mediaResult.get("status").equals("finished")) { - Thread.sleep(500); - mediaResult = new RestTemplate().getForObject(environment.getProperty("pdf.converter.url") + - "api/v1/" + queueResult.get("id"), Map.class); - System.out.println("Polling"); - } - RestTemplate restTemplate = new RestTemplate(); - restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); - HttpHeaders headers2 = new HttpHeaders(); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); - HttpEntity entity = new HttpEntity(headers2); + File resultPdf = new File(environment.getProperty("configuration.exportUrl") + label + ".pdf"); + FileOutputStream output = new FileOutputStream(resultPdf); + IOUtils.write(queueResult, output); - ResponseEntity response = restTemplate.exchange(environment.getProperty("pdf.converter.url") + - mediaResult.get("result_url"), HttpMethod.GET, entity, byte[].class, "1"); - - UUID uuid = UUID.randomUUID(); - File zip = new File(uuid + ".zip"); - if (response.getStatusCode().equals(HttpStatus.OK)) { - FileOutputStream output = new FileOutputStream(zip); - IOUtils.write(response.getBody(), output); - } - return extractFromZip(zip, label + ".pdf"); + return resultPdf; } private File extractFromZip(File file, String filename) throws IOException { diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DocumentManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DocumentManager.java index 87ba688ca..f3320640f 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DocumentManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DocumentManager.java @@ -60,7 +60,7 @@ public class DocumentManager { visibilityRuleService.setProperties(properties); visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules()); wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService); - File exportFile = new File(dataset.getLabel() + ".docx"); + File exportFile = new File(environment.getProperty("configuration.exportUrl") + dataset.getLabel() + ".docx"); FileOutputStream out = new FileOutputStream(exportFile); document.write(out); out.close(); diff --git a/dmp-backend/web/src/main/resources/application-devel.properties b/dmp-backend/web/src/main/resources/application-devel.properties index 4826cfea8..03641f5cd 100644 --- a/dmp-backend/web/src/main/resources/application-devel.properties +++ b/dmp-backend/web/src/main/resources/application-devel.properties @@ -17,7 +17,8 @@ pdf.converter.url=http://localhost:88/ ####################CONFIGURATION FILES OVERRIDES CONFIGURATIONS########## configuration.externalUrls=/tmp/ExternalUrls.xml configuration.dynamicProjectUrl=/tmp/ProjectConfiguration.xml -configuration.h2020template=C:\\Users\\gkolokythas\\Documents\\OpenAIRE-EUDAT-DMP-service-pilot\\dmp-backend\\web\\src\\main\\resources\\documents\\h2020.docx +configuration.h2020template=C:\\Users\\gkolokythas\\Documents\\openDmp\\dmp-backend\\web\\src\\main\\resources\\documents\\h2020.docx +configuration.exportUrl=C:\\Users\\gkolokythas\\Documents\\openDmp\\dmp-backend\\web\\src\\main\\exportFiles\\ #############TWITTER LOGIN CONFIGURATIONS######### twitter.login.redirect_uri=http://127.0.0.1:4200/login/twitter