Skip to content

Commit

Permalink
P4ADEV-320-refactoring-accordingly-handbook
Browse files Browse the repository at this point in the history
  • Loading branch information
LarissaASLeite committed May 22, 2024
1 parent cbe8aa9 commit 213fd32
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 22 deletions.
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ openApiGenerate {
generatorName.set("spring")
inputSpec.set("$rootDir/openapi/p4pa-auth.openapi.yaml")
outputDir.set("$projectDir/build/generated")
apiPackage.set("openapi.pagopa.payhub.controller")
modelPackage.set("openapi.pagopa.payhub.model")
apiPackage.set("it.gov.pagopa.payhub.controller.generated")
modelPackage.set("it.gov.pagopa.payhub.model.generated")
configOptions.set(mapOf(
"dateLibrary" to "java8",
"requestMappingMode" to "api_interface",
"useSpringBoot3" to "true",
"interfaceOnly" to "true"
"interfaceOnly" to "true",
"useTags" to "true"
))
}
2 changes: 1 addition & 1 deletion openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ paths:
/auth:
get:
tags:
- authToken
- auth
operationId: authToken
parameters:
- name: token
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.gov.pagopa.payhub.auth.controller;

import it.gov.pagopa.payhub.auth.service.AuthService;
import openapi.pagopa.payhub.controller.AuthApi;
import it.gov.pagopa.payhub.controller.generated.AuthApi;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.TokenExpiredException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
Expand All @@ -20,21 +20,23 @@ public class AuthExceptionHandler {
@ExceptionHandler({InvalidTokenException.class, TokenExpiredException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public AuthErrorDTO handleInvalidTokenException(ServiceException ex, HttpServletRequest request){
logStackTrace(ex, request);
logException(ex, request);
String message = ex.getMessage();

log.info("A {} occurred handling request {}: HttpStatus 401 - {}",
ex.getClass(),
getRequestDetails(request), message);

return new AuthErrorDTO(ex.getCode(), message);
}

public static void logStackTrace(ServiceException error, HttpServletRequest request) {
public static void logException(ServiceException error, HttpServletRequest request) {
if(error.isPrintStackTrace()){
log.info("A {} occurred handling request {} at {}",
error.getClass().getSimpleName() ,
getRequestDetails(request),
error.getStackTrace().length > 0 ? error.getStackTrace()[0] : "UNKNOWN");
}else {
log.info("A {} occurred handling request {}: HttpStatus 401 - {}",
error.getClass(),
getRequestDetails(request), error.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.gov.pagopa.payhub.auth.exception;

import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import lombok.Getter;
import openapi.pagopa.payhub.model.AuthErrorDTO;

@Getter
public class ServiceException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.gov.pagopa.payhub.auth.exception;

import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
Expand All @@ -14,7 +14,7 @@

import java.util.Optional;

import static openapi.pagopa.payhub.model.AuthErrorDTO.CodeEnum.INVALID_REQUEST;
import static it.gov.pagopa.payhub.model.generated.AuthErrorDTO.CodeEnum.INVALID_REQUEST;

@RestControllerAdvice
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.gov.pagopa.payhub.auth.exception.custom;

import it.gov.pagopa.payhub.auth.exception.ServiceException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import lombok.Getter;
import openapi.pagopa.payhub.model.AuthErrorDTO;

@Getter
public class InvalidTokenException extends ServiceException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.gov.pagopa.payhub.auth.exception.custom;

import it.gov.pagopa.payhub.auth.exception.ServiceException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import lombok.Getter;
import openapi.pagopa.payhub.model.AuthErrorDTO;

@Getter
public class TokenExpiredException extends ServiceException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.auth0.jwt.interfaces.DecodedJWT;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.TokenExpiredException;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import org.springframework.stereotype.Component;

import java.security.interfaces.RSAPublicKey;
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

<property name="CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [${appName:-}] %-5level [%15.15t] [%-40.40logger{39}] - %msg%n"/>
<property name="FILE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN}"/>
<property name="AUDIT_LOG_PATTERN"
value="%msg"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.payhub.auth.exception.ValidationExceptionHandler;
import it.gov.pagopa.payhub.auth.service.AuthService;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.TokenExpiredException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import lombok.extern.slf4j.Slf4j;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.utils.JWTValidator;
import it.gov.pagopa.payhub.auth.utils.JWTValidatorUtils;
import openapi.pagopa.payhub.model.AuthErrorDTO;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit 213fd32

Please sign in to comment.