Skip to content

Commit

Permalink
NOD-784 upgrade wsdl generation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsca committed Mar 30, 2024
1 parent c1176ca commit 2b3455f
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public ResourceBundleMessageSource messageSource() {
var resourceBundleMessageSource=new ResourceBundleMessageSource();
resourceBundleMessageSource.setBasename("i18n/messages"); // directory with messages_XX.properties
resourceBundleMessageSource.setDefaultLocale(Locale.ENGLISH);
resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
resourceBundleMessageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
resourceBundleMessageSource.setAlwaysUseMessageFormat(true);
return resourceBundleMessageSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class RedirectController {
})
@GetMapping
public void redirect(@Parameter(description = "", example = "identificativoIntermediarioPA_sessionId")
@NotBlank(message = "{redirect.session-id.not-blank}") @RequestParam("sessionId") String sessionId,
@NotBlank(message = "{redirect.session-id.not-blank}")
@RequestParam("sessionId") String sessionId,
HttpServletResponse response) throws IOException {
ConversionResultDTO conversionResultDTO = converterService.convert(sessionId);
response.sendRedirect(conversionResultDTO.getUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.util.DefaultUriBuilderFactory;

import java.net.URI;
import java.time.Instant;
Expand All @@ -27,92 +34,76 @@
@RequiredArgsConstructor
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@Value("${error.code.uri}")
private String errorCodeUri;
private static final String ERROR_CODE_TITLE = "error-code.%s.title";
private static final String ERROR_CODE_DETAIL = "error-code.%s.detail";

private final MessageSource messageSource;

@ExceptionHandler(AppException.class)
public ErrorResponse handleAppException(AppException appEx) {
return ErrorResponse.builder(appEx, forAppErrorCodeMessageEnum(appEx.getError(), appEx.getReason()))
.typeMessageCode("https://pagopa.it/errors/"+getAppCode(appEx.getError()))
.titleMessageCode("error-code."+appEx.getError().name()+".title")
.detailMessageCode("error-code."+appEx.getError().name()+".detail")
return forAppException(appEx);
}

@ExceptionHandler(Exception.class)
public ErrorResponse handleGenericException(Exception ex) {
String operationId = MDC.get(LoggingAspect.OPERATION_ID);
log.error(String.format("GenericException: operation-id=[%s]", operationId!=null?operationId:"n/a"), ex);
return forAppException(new AppException(ex, AppErrorCodeMessageEnum.ERROR, ex.getMessage()));
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) {
if (body == null && ex instanceof ErrorResponse errorResponse) {
ProblemDetail problemDetail = errorResponse.updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
setExtraProperties(problemDetail);
body = problemDetail;
}
return super.handleExceptionInternal(ex, body, headers, statusCode, request);
}

private ErrorResponse forAppException(AppException appEx){
return ErrorResponse.builder(appEx, forAppErrorCodeMessageEnum(appEx.getError(), appEx.getMessage()))
.titleMessageCode(String.format(ERROR_CODE_TITLE, appEx.getError().name()))
.detailMessageCode(String.format(ERROR_CODE_DETAIL, appEx.getError().name()))
.detailMessageArguments(appEx.getArgs())
.build(messageSource, LocaleContextHolder.getLocale());
}

private static ProblemDetail forAppErrorCodeMessageEnum(AppErrorCodeMessageEnum error, String reason) {
private ProblemDetail forAppErrorCodeMessageEnum(AppErrorCodeMessageEnum error, @Nullable String detail) {
Assert.notNull(error, "AppErrorCodeMessageEnum is required");

ProblemDetail problemDetail = ProblemDetail.forStatus(error.getStatus());
problemDetail.setType(URI.create("https://pagopa.it/errors/"+getAppCode(error)));
problemDetail.setType(getTypeFromErrorCode(getAppCode(error)));
problemDetail.setTitle(error.getTitle());
problemDetail.setDetail(reason);
problemDetail.setDetail(detail);

problemDetail.setProperty("timestamp", Instant.now());
problemDetail.setProperty("error-code", getAppCode(error));
setExtraProperties(problemDetail);

return problemDetail;
}

private void setExtraProperties(ProblemDetail problemDetail){
problemDetail.setProperty("timestamp", Instant.now());
String operationId = MDC.get(LoggingAspect.OPERATION_ID);
if(operationId!=null){
problemDetail.setProperty("operation-id", operationId);
}
return problemDetail;
}
private URI getTypeFromErrorCode(String errorCode) {
return new DefaultUriBuilderFactory()
.uriString(errorCodeUri)
.pathSegment(errorCode)
.build();
}

private static String getAppCode(AppErrorCodeMessageEnum error){
private String getAppCode(AppErrorCodeMessageEnum error){
return String.format("%s-%s", Constants.SERVICE_CODE_APP, error.getCode());
}


// @ExceptionHandler(AppException.class)
// ProblemDetail handleAppException(AppException e) {
// Locale locale = LocaleContextHolder.getLocale();
// String ff = messageSource.getMessage("error-code.PERSISTENCE_.title", Arrays.asList("ddd").toArray(), locale);
//// return e.getBody();
// return e.getBody();
// }
//
// private static ProblemDetail forAppException(AppException e) {
// Assert.notNull(e, "AppException is required");
// AppErrorCodeMessageEnum error = e.getError();
//
// ProblemDetail problemDetail = e.getBody();
// problemDetail.setType(URI.create("https://pagopa.it/errors/"+getAppCode(error)));
// problemDetail.setTitle("error-code."+error.name()+".title");
// problemDetail.setDetail("error-code."+error.name()+".detail");
// problemDetail.setProperty("timestamp", Instant.now());
// problemDetail.setProperty("error-code", getAppCode(error));
// String operationId = MDC.get(OPERATION_ID);
// if(operationId!=null){
// problemDetail.setProperty("operation-id", operationId);
// }
// return problemDetail;
// }
//
// private static String getAppCode(AppErrorCodeMessageEnum error){
// return String.format("%s-%s", Constants.SERVICE_CODE_APP, error.getCode());
// }

// private ErrorResponse forAppClientException(AppClientException e) {
// Assert.notNull(e, "AppClientException is required");
// AppErrorCodeMessageEnum codeMessage = e.getError();
//
// ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(codeMessage.getStatus(), codeMessage.getReason());
// problemDetail.setProperty("timestamp", Instant.now());
// problemDetail.setProperty("client-error-code", String.format("%s-%s", Constants.SERVICE_CODE_APP, codeMessage.getCode()));
//
// Optional<String> requestIdOpt = Optional.ofNullable(MDC.get(HEADER_REQUEST_ID));
// requestIdOpt.ifPresent(requestId -> problemDetail.setProperty(HEADER_REQUEST_ID.toLowerCase(), requestId));
//
// String errorId = UUID.randomUUID().toString();
// problemDetail.setProperty("error-id", errorId);
//
// return ErrorResponse.builder(e, problemDetail)
// .detailMessageCode(codeMessage.getMessageDetailCode())
// .detailMessageArguments(e.getArgs())
// .build(messageSource, LocaleContextHolder.getLocale());
// }


// private final AppErrorUtil appErrorUtil;
// private final MessageSource messageSource;
//
// @ApiResponses(value = {
// @ApiResponse(responseCode = "500", description = "Internal Server Error", content = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@

@Getter
public enum AppErrorCodeMessageEnum {
UNKNOWN("0000", "Unknown error", HttpStatus.INTERNAL_SERVER_ERROR),
ERROR("0500", "System error", HttpStatus.INTERNAL_SERVER_ERROR),
BAD_REQUEST("0400", "Bad Request", HttpStatus.BAD_REQUEST),
PARSING_("1000", "a", HttpStatus.BAD_REQUEST),
PARSING_JAXB_EMPTY_NODE_ELEMENT("1001", "JAXB node element is empty", HttpStatus.BAD_REQUEST),
PARSING_JAXB_PARSE_ERROR("1002", "JAXB error during parse", HttpStatus.BAD_REQUEST),
PERSISTENCE_("2000", "Persistence error", HttpStatus.BAD_REQUEST),
CLIENT_("3000", "cvv", HttpStatus.BAD_REQUEST),
ERROR ( 500, "System error", "{0}", HttpStatus.INTERNAL_SERVER_ERROR),
PARSE_ERROR (1000, "Parse error", "Error while parsing: {0}", HttpStatus.BAD_REQUEST),
PRIMITIVE_NOT_VALID (1001, "Primitive not valid", "Primitive [{0}] not valid", HttpStatus.NOT_ACCEPTABLE),
RPT_NOT_FOUND (1002, "RPT not found", "RPT with sessionId [{0}] not found", HttpStatus.NOT_FOUND),
CLIENT_IUV_GENERATOR (1003, "IUVGeneratorClient error", "IUVGeneratorClient status [{0}] - {1}", HttpStatus.EXPECTATION_FAILED),
CLIENT_GPD (1004, "GPDClient error", "GPDClient status [{0}] - {1}", HttpStatus.EXPECTATION_FAILED),
CLIENT_DECOUPLER_CACHING(1005, "DecouplerCachingClient error", "DecouplerCachingClient status [{0}] - {1}",HttpStatus.EXPECTATION_FAILED),
;

private final String code;
private final Integer code;
private final String title;
private final String detail;
private final HttpStatusCode status;

AppErrorCodeMessageEnum(String code, String title, HttpStatus status) {
AppErrorCodeMessageEnum(Integer code, String title, String detail, HttpStatus status) {
this.code = code;
this.title = title;
this.detail = detail;
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package it.gov.pagopa.wispconverter.exception;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;

@Getter
public class AppException extends RuntimeException {

private final AppErrorCodeMessageEnum error;
private final transient Object[] args;
private final String reason;
// private final String detail;

public AppException(AppErrorCodeMessageEnum error, String reason, Object... args) {
super(reason);
public AppException(AppErrorCodeMessageEnum error, Object... args) {
super(composeMessage(error, getArgsOrNull(args)));
this.error = error;
this.reason = reason;
this.args = getArgsOrNull(args);
// this.detail = composeMessage(error, getArgsOrNull(args));
}

public AppException(Throwable cause, AppErrorCodeMessageEnum error, String reason, Object... args) {
super(reason, cause);
public AppException(Throwable cause, AppErrorCodeMessageEnum error, Object... args) {
super(composeMessage(error, getArgsOrNull(args)), cause);
this.error = error;
this.reason = reason;
this.args = getArgsOrNull(args);
// this.detail = composeMessage(error, getArgsOrNull(args));
}

private static Object[] getArgsOrNull(Object... args) {
return args.length > 0 ? args.clone() : null;
}

private static String composeMessage(AppErrorCodeMessageEnum error, Object[] args){
if(error.getDetail() != null){
return MessageFormat.format(error.getDetail(), args);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import feign.FeignException;
import io.lettuce.core.RedisException;
import it.gov.pagopa.wispconverter.client.decoupler.DecouplerCachingClient;
import it.gov.pagopa.wispconverter.exception.AppClientException;
import it.gov.pagopa.wispconverter.exception.AppErrorCodeMessageEnum;
import it.gov.pagopa.wispconverter.exception.AppException;
import it.gov.pagopa.wispconverter.repository.CacheRepository;
Expand Down Expand Up @@ -34,7 +33,6 @@ public class CacheService {

public void storeRequestMappingInCache(List<RPTContentDTO> rptContentDTOs, String sessionId) {
try {

rptContentDTOs.forEach(e -> {
String idIntermediarioPA = e.getIdIntermediarioPA();
String noticeNumber = e.getNoticeNumber();
Expand All @@ -46,12 +44,8 @@ public void storeRequestMappingInCache(List<RPTContentDTO> rptContentDTOs, Strin
String requestIDForRTHandling = String.format(CACHING_KEY_TEMPLATE, idIntermediarioPA, noticeNumber);
this.cacheRepository.insert(requestIDForRTHandling, sessionId, this.requestIDMappingTTL);
});


} catch (FeignException e) {
throw new AppClientException(e, e.status(), AppErrorCodeMessageEnum.CLIENT_, e.getMessage());
} catch (RedisException e) {
throw new AppException(e, AppErrorCodeMessageEnum.PERSISTENCE_, e.getMessage());
throw new AppException(e, AppErrorCodeMessageEnum.CLIENT_DECOUPLER_CACHING, e.status(), e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.gov.pagopa.wispconverter.service;

import com.azure.cosmos.CosmosException;
import com.azure.spring.data.cosmos.exception.CosmosAccessException;
import gov.telematici.pagamenti.ws.NodoInviaCarrelloRPT;
import gov.telematici.pagamenti.ws.NodoInviaRPT;
import gov.telematici.pagamenti.ws.ppthead.IntestazioneCarrelloPPT;
Expand Down Expand Up @@ -90,12 +88,9 @@ private PaymentPosition mapRPTToDebtPosition(RPTRequestDTO rptRequestDTO, RPTCon
}

private RPTRequestEntity getRPTRequestEntity(String sessionId) {
try {
Optional<RPTRequestEntity> optRPTReqEntity = this.rptRequestRepository.findById(sessionId);
return optRPTReqEntity.orElseThrow(() -> new AppException(AppErrorCodeMessageEnum.PERSISTENCE_, String.format("User with sessionId=[%s] not found", sessionId), sessionId));
} catch (CosmosException | CosmosAccessException e) {
throw new AppException(e, AppErrorCodeMessageEnum.PERSISTENCE_, e.getMessage());
}
Optional<RPTRequestEntity> optRPTReqEntity = this.rptRequestRepository.findById(sessionId);
return optRPTReqEntity.orElseThrow(() -> new AppException(AppErrorCodeMessageEnum.RPT_NOT_FOUND, sessionId));

// TODO RE
}

Expand Down Expand Up @@ -135,7 +130,7 @@ private List<RPTContentDTO> getRPTContentDTO(String primitive, String payload) t
.build();
}).toList();
}
default -> throw new AppException(AppErrorCodeMessageEnum.PARSING_, String.format("Primitive [%S] not valid", primitive));
default -> throw new AppException(AppErrorCodeMessageEnum.PRIMITIVE_NOT_VALID, primitive);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import it.gov.pagopa.wispconverter.client.gpd.GPDClient;
import it.gov.pagopa.wispconverter.client.gpd.model.MultiplePaymentPosition;
import it.gov.pagopa.wispconverter.client.gpd.model.PaymentPosition;
import it.gov.pagopa.wispconverter.exception.AppClientException;
import it.gov.pagopa.wispconverter.exception.AppErrorCodeMessageEnum;
import it.gov.pagopa.wispconverter.exception.AppException;
import it.gov.pagopa.wispconverter.service.mapper.DebtPositionMapper;
import it.gov.pagopa.wispconverter.service.model.RPTContentDTO;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -43,7 +43,7 @@ public void executeBulkCreation(List<RPTContentDTO> rptContentDTOs) {
});

} catch (FeignException e) {
throw new AppClientException(e, e.status(), AppErrorCodeMessageEnum.CLIENT_, e.getMessage());
throw new AppException(e, AppErrorCodeMessageEnum.CLIENT_GPD, e.status(), e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import it.gov.pagopa.wispconverter.client.iuvgenerator.IUVGeneratorClient;
import it.gov.pagopa.wispconverter.client.iuvgenerator.model.IUVGeneratorRequest;
import it.gov.pagopa.wispconverter.client.iuvgenerator.model.IUVGeneratorResponse;
import it.gov.pagopa.wispconverter.exception.AppClientException;
import it.gov.pagopa.wispconverter.exception.AppErrorCodeMessageEnum;
import it.gov.pagopa.wispconverter.exception.AppException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -35,13 +34,9 @@ public String getNAVCodeFromIUVGenerator(String creditorInstitutionCode) {
String navCode;
try {
IUVGeneratorResponse response = this.iuvGeneratorClient.generate(creditorInstitutionCode, request);
if (response == null) {
throw new AppException(AppErrorCodeMessageEnum.CLIENT_, "IUVGeneratorResponse is null");
}
navCode = response.getIuv();
return response.getIuv();
} catch (FeignException e) {
throw new AppClientException(e, e.status(), AppErrorCodeMessageEnum.CLIENT_, e.getMessage());
throw new AppException(e, AppErrorCodeMessageEnum.CLIENT_IUV_GENERATOR, e.status(), e.getMessage());
}
return navCode;
}
}
Loading

0 comments on commit 2b3455f

Please sign in to comment.