|
1 | 1 | package com.park.utmstack.web.rest.util; |
2 | 2 |
|
3 | | -import com.park.utmstack.domain.application_events.enums.ApplicationEventType; |
4 | 3 | import com.park.utmstack.security.jwt.JWTFilter; |
5 | | -import com.park.utmstack.service.application_events.ApplicationEventService; |
| 4 | +import com.park.utmstack.service.dto.web_pdf.PdfServiceResponse; |
6 | 5 | import com.park.utmstack.service.util.PdfService; |
7 | | -import com.park.utmstack.util.ResponseUtil; |
8 | 6 | import com.park.utmstack.web.rest.errors.BadRequestAlertException; |
9 | | -import javassist.NotFoundException; |
| 7 | +import lombok.RequiredArgsConstructor; |
10 | 8 | import org.slf4j.Logger; |
11 | 9 | import org.slf4j.LoggerFactory; |
12 | 10 | import org.springframework.http.HttpHeaders; |
13 | | -import org.springframework.http.HttpStatus; |
14 | 11 | import org.springframework.http.MediaType; |
15 | 12 | import org.springframework.http.ResponseEntity; |
16 | 13 | import org.springframework.web.bind.annotation.*; |
|
19 | 16 | * REST controller for managing {@link PdfGeneratorResource}. |
20 | 17 | */ |
21 | 18 | @RestController |
| 19 | +@RequiredArgsConstructor |
22 | 20 | @RequestMapping("/api") |
23 | 21 | public class PdfGeneratorResource { |
24 | 22 |
|
25 | 23 | private static final String CLASSNAME = "PdfGeneratorResource"; |
26 | 24 | private final Logger log = LoggerFactory.getLogger(PdfGeneratorResource.class); |
27 | 25 |
|
28 | | - private final ApplicationEventService applicationEventService; |
29 | | - |
30 | | - |
31 | 26 | private final PdfService pdfService; |
32 | 27 |
|
33 | | - public PdfGeneratorResource( |
34 | | - ApplicationEventService applicationEventService, |
35 | | - PdfService pdfService) { |
36 | | - this.applicationEventService = applicationEventService; |
37 | | - this.pdfService = pdfService; |
38 | | - } |
| 28 | + @GetMapping("/generate-pdf-report") |
| 29 | + public ResponseEntity<?> getPdfReportInBytes(@RequestParam String url, |
| 30 | + @RequestParam PdfService.PdfAccessTypes accessType, |
| 31 | + @RequestParam String filename, |
| 32 | + @RequestHeader(JWTFilter.AUTHORIZATION_HEADER) String accessKey) { |
39 | 33 |
|
40 | | - /** |
41 | | - * {@code GET /generate-pdf-report} : get pdf report in bytes, from a dashboard or compliance. |
42 | | - * |
43 | | - * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the pdf report in bytes in body. |
44 | | - */ |
45 | | - @RequestMapping( |
46 | | - value = "/generate-pdf-report", |
47 | | - produces = MediaType.APPLICATION_PDF_VALUE, |
48 | | - method = RequestMethod.GET |
49 | | - ) |
50 | | - public ResponseEntity getPdfReportInBytes(@RequestParam String url, |
51 | | - @RequestParam PdfService.PdfAccessTypes accessType, |
52 | | - @RequestParam String filename, |
53 | | - @RequestHeader(JWTFilter.AUTHORIZATION_HEADER) String accessKey) { |
54 | 34 | final String ctx = CLASSNAME + ".getPdfReportInBytes"; |
55 | | - try { |
56 | | - if (accessType == PdfService.PdfAccessTypes.PDF_TYPE_INTERNAL) { |
57 | | - throw new BadRequestAlertException("Access type ("+ accessType.get() +") not allowed","pdfreport","accesskeynotvalid"); |
58 | | - } |
59 | | - log.debug("REST request to get pdf report"); |
60 | | - |
61 | | - HttpHeaders headers = new HttpHeaders(); |
62 | | - headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); |
63 | | - headers.add(HttpHeaders.PRAGMA, "no-cache"); |
64 | | - headers.add(HttpHeaders.EXPIRES, "0"); |
65 | | - headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + filename); |
66 | | - headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE); |
67 | 35 |
|
68 | | - byte [] resultPdf = pdfService.getPdf( url, accessKey.substring(7), accessType.get()); |
69 | | - if (resultPdf != null && resultPdf.length > 0 ) { |
70 | | - return ResponseEntity.ok().headers(headers).body(resultPdf); |
71 | | - } else { |
72 | | - throw new NotFoundException("We couldn't generate the pdf, reason: No data returned from PDF service"); |
73 | | - } |
74 | | - } catch (Exception e) { |
75 | | - String msg = ctx + ": " + e.getLocalizedMessage(); |
76 | | - log.error(msg); |
77 | | - applicationEventService.createEvent(msg, ApplicationEventType.ERROR); |
78 | | - return ResponseUtil.buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, msg); |
| 36 | + if (accessType == PdfService.PdfAccessTypes.PDF_TYPE_INTERNAL) { |
| 37 | + throw new BadRequestAlertException(String.format("PDF Service not implemented for %s", accessType), CLASSNAME, null); |
79 | 38 | } |
| 39 | + |
| 40 | + log.debug("REST request to get pdf report"); |
| 41 | + |
| 42 | + PdfServiceResponse pdfResponse = |
| 43 | + pdfService.downloadPdf(url, accessKey.substring(7), accessType.get()); |
| 44 | + |
| 45 | + HttpHeaders headers = new HttpHeaders(); |
| 46 | + headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); |
| 47 | + headers.add(HttpHeaders.PRAGMA, "no-cache"); |
| 48 | + headers.add(HttpHeaders.EXPIRES, "0"); |
| 49 | + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + filename); |
| 50 | + headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE); |
| 51 | + |
| 52 | + return ResponseEntity.ok() |
| 53 | + .headers(headers) |
| 54 | + .body(pdfResponse.getPdfBytes()); |
80 | 55 | } |
81 | 56 | } |
0 commit comments