Skip to content

Commit

Permalink
PDF new path
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Sep 4, 2024
1 parent 42b641f commit 17dd135
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.gov.pagopa.bizeventsservice.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* This configuration is required ignore the 'accept' header in the request.
* If the client uses a different value of the response then SpringBoot goes wrong because the response type doesn't match.
* We can ignore the accept header from the client.
* <p>
* see more information in this GitHub <a href="https://github.com/swagger-api/swagger-ui/issues/5649">issue</a>
*/
// TODO should I remove this configuration?
@Configuration
public class ContentNegotiationConf implements WebMvcConfigurer {

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.ignoreAcceptHeader(true)
.defaultContentType(MediaType.ALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import it.gov.pagopa.bizeventsservice.model.ProblemJson;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.io.Resource;

import javax.validation.constraints.NotBlank;

Expand All @@ -37,5 +40,20 @@ ResponseEntity<Void> disablePaidNotice(
@RequestHeader(X_FISCAL_CODE) @NotBlank String fiscalCode,
@Parameter(description = "The id of the paid event.", required = true) @NotBlank @PathVariable("event-id") String eventId);

@Operation(summary = "Retrieve the PDF receipt given event id.", security = {
@SecurityRequirement(name = "ApiKey")}, operationId = "generatePDF")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Obtained the PDF receipt.",
headers = {@Header(name = HttpHeaders.CONTENT_DISPOSITION, description = "Content disposition with name of the file", schema = @Schema(type = "string"))},
content = {@Content(mediaType = MediaType.APPLICATION_PDF_VALUE, schema = @Schema(implementation = Resource.class)),}),
@ApiResponse(responseCode = "401", description = "Wrong or missing function key.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(responseCode = "404", description = "Not found the receipt.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class))),
@ApiResponse(responseCode = "422", description = "Unprocessable receipt.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class))),
@ApiResponse(responseCode = "429", description = "Too many requests.", content = @Content(schema = @Schema())),
@ApiResponse(responseCode = "500", description = "Service unavailable.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class)))})
@GetMapping(value = "/{event-id}/pdf", produces = {MediaType.APPLICATION_PDF_VALUE, MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<Resource> generatePDF(
@RequestHeader(X_FISCAL_CODE) @NotBlank String fiscalCode,
@Parameter(description = "The id of the paid event.", required = true) @NotBlank @PathVariable("event-id") String eventId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ ResponseEntity<Void> disableTransaction(
@RequestHeader("x-fiscal-code") @NotBlank String fiscalCode,
@Parameter(description = "The id of the transaction.", required = true) @NotBlank @PathVariable("transaction-id") String transactionId);

@Operation(summary = "Retrieve the PDF receipt given event id.", security = {
@Operation(summary = "Retrieve the PDF receipt given event id.", deprecated = true,
description = "This operation is deprecated. Use Paid Notice APIs instead", security = {
@SecurityRequirement(name = "ApiKey")}, operationId = "getPDFReceipt")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Obtained the PDF receipt.", content = @Content(mediaType = MediaType.APPLICATION_PDF_VALUE, schema = @Schema(type = "string", format = "binary"))),
Expand All @@ -148,6 +149,7 @@ ResponseEntity<Void> disableTransaction(
@ApiResponse(responseCode = "429", description = "Too many requests.", content = @Content(schema = @Schema())),
@ApiResponse(responseCode = "500", description = "Service unavailable.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class)))})
@GetMapping(value = "/{event-id}/pdf")
@Deprecated(forRemoval = false)
ResponseEntity<byte[]> getPDFReceipt(
@RequestHeader("x-fiscal-code") @NotBlank String fiscalCode,
@Parameter(description = "The id of the event.", required = true) @NotBlank @PathVariable("event-id") String eventId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package it.gov.pagopa.bizeventsservice.controller.impl;

import it.gov.pagopa.bizeventsservice.controller.IPaidNoticeController;
import it.gov.pagopa.bizeventsservice.service.IBizEventsService;
import it.gov.pagopa.bizeventsservice.service.ITransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.io.ByteArrayInputStream;

/**
* Implementation of {@link IPaidNoticeController} that contains the Rest Controller
* for events services
Expand All @@ -15,10 +20,12 @@
public class PaidNoticeController implements IPaidNoticeController {

private final ITransactionService transactionService;
private final IBizEventsService bizEventsService;

@Autowired
public PaidNoticeController(ITransactionService transactionService) {
public PaidNoticeController(ITransactionService transactionService, IBizEventsService bizEventsService) {
this.transactionService = transactionService;
this.bizEventsService = bizEventsService;
}


Expand All @@ -28,5 +35,17 @@ public ResponseEntity<Void> disablePaidNotice(String fiscalCode, String transact
return new ResponseEntity<>(HttpStatus.OK);
}

@Override
public ResponseEntity<Resource> generatePDF(@NotBlank String fiscalCode, @NotBlank String eventId) {
// to check if is an OLD event present only on the PM --> the receipt is not available for events present exclusively on the PM
bizEventsService.getBizEvent(eventId);
byte[] receiptFile = transactionService.getPDFReceipt(fiscalCode, eventId);
return ResponseEntity
.ok()
.contentLength(receiptFile.length)
.contentType(MediaType.APPLICATION_PDF)
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline().filename("Receipt.pdf").build().toString())
.body(new ByteArrayResource(receiptFile));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="local">
<springProfile name="!local">
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
Expand All @@ -18,7 +18,7 @@
</root>
</springProfile>

<springProfile name="!local">
<springProfile name="local">
<springProperty name="ECS_SERVICE_VERSION" source="info.application.version"/>
<springProperty name="ECS_SERVICE_NAME" source="info.application.name"/>
<appender name="ECS_JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
Expand Down

0 comments on commit 17dd135

Please sign in to comment.