diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java index b6f7d12..d208502 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java @@ -22,7 +22,10 @@ import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -38,6 +41,7 @@ public class GradBusinessService { private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_PDF = "application/pdf"; private static final String ACCEPT = "*/*"; + private static final String TMP = "/tmp"; /** * The Web client. */ @@ -188,7 +192,7 @@ public ResponseEntity getSchoolReportPDFByMincode(String mincode, String if(result != null) { res = result.getInputStream().readAllBytes(); } - return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameSchoolReports(mincode,year,month,type), MediaType.APPLICATION_PDF); + return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameSchoolReports(mincode,year,month,type,MediaType.APPLICATION_PDF), MediaType.APPLICATION_PDF); } catch (Exception e) { return getInternalServerErrorResponse(e); } @@ -206,7 +210,7 @@ public ResponseEntity getAmalgamatedSchoolReportPDFByMincode(String minc Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"), Locale.CANADA); int year = cal.get(Calendar.YEAR); String month = "00"; - String fileName = EducGradBusinessUtil.getFileNameSchoolReports(mincode, year, month, type); + String fileName = EducGradBusinessUtil.getFileNameSchoolReports(mincode, year, month, type, MediaType.APPLICATION_PDF); try { logger.debug("******** Merging Documents Started ******"); byte[] res = EducGradBusinessUtil.mergeDocumentsPDFs(locations); @@ -215,6 +219,7 @@ public ResponseEntity getAmalgamatedSchoolReportPDFByMincode(String minc headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList(BEARER + accessToken)); headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_PDF)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_PDF)); + saveBinaryResponseToFile(res, fileName); return handleBinaryResponse(res, fileName, MediaType.APPLICATION_PDF); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -346,4 +351,12 @@ private ResponseEntity handleBinaryResponse(byte[] resultBinary, String } return response; } + + private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException { + if(resultBinary.length > 0) { + try (OutputStream out = new FileOutputStream(TMP + "/" + reportFile)) { + out.write(resultBinary); + } + } + } } diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGradBusinessUtil.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGradBusinessUtil.java index 60fbb7a..d44b0b8 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGradBusinessUtil.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGradBusinessUtil.java @@ -9,6 +9,7 @@ import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; import java.io.*; import java.util.ArrayList; @@ -98,8 +99,8 @@ public static String getTempDirPath() { return Optional.ofNullable(System.getProperty("java.io.tmpdir")).orElse("/tmp").concat(File.pathSeparator); } - public static String getFileNameSchoolReports(String mincode, int year, String month, String type) { - return mincode + "_" + year + month + "_" + type + ".pdf"; + public static String getFileNameSchoolReports(String mincode, int year, String month, String type, MediaType mediaType) { + return mincode + "_" + year + month + "_" + type + "." + mediaType.getSubtype(); } public static String getFileNameStudentCredentials(String mincode, String pen, String type) {