From 18aadf6d0d39463ff1f1a1cb5795400e3c803951 Mon Sep 17 00:00:00 2001 From: Neha Farheen Date: Fri, 19 Apr 2024 14:40:53 +0530 Subject: [PATCH] kafka bean creation condition Signed-off-by: Neha Farheen --- .../service/config/KafkaProducerConfig.java | 3 ++- .../AuthenticationErrorEventingPublisher.java | 3 ++- .../constant/IdAuthConfigKeyConstants.java | 2 ++ .../otp/service/controller/OTPController.java | 19 ++++++++++++++++- .../service/controller/AuthController.java | 18 +++++++++++++++- .../kyc/controller/KycAuthController.java | 21 +++++++++++++++++-- 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/KafkaProducerConfig.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/KafkaProducerConfig.java index a2d09b2399c..4daaf7381fe 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/KafkaProducerConfig.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/KafkaProducerConfig.java @@ -16,6 +16,7 @@ import io.mosip.authentication.core.logger.IdaLogger; import io.mosip.kernel.core.logger.spi.Logger; +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_ENABLED; /** * The Class KafkaProducerConfig. @@ -24,7 +25,7 @@ */ @Configuration -@ConditionalOnProperty(value = "mosip.ida.authentication.error.eventing.enabled", havingValue = "true", matchIfMissing = false) +@ConditionalOnProperty(value = AUTHENTICATION_ERROR_EVENTING_ENABLED, havingValue = "true", matchIfMissing = false) public class KafkaProducerConfig { private static final Logger logger = IdaLogger.getLogger(KafkaProducerConfig.class); diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/kafka/impl/AuthenticationErrorEventingPublisher.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/kafka/impl/AuthenticationErrorEventingPublisher.java index daf9d9cb351..e1a08b4f28a 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/kafka/impl/AuthenticationErrorEventingPublisher.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/kafka/impl/AuthenticationErrorEventingPublisher.java @@ -1,5 +1,6 @@ package io.mosip.authentication.common.service.kafka.impl; +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_ENABLED; import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_TOPIC; import java.util.HashMap; @@ -32,7 +33,7 @@ * @author Neha */ @Component -@ConditionalOnProperty(value = "mosip.ida.authentication.error.eventing.enabled", havingValue = "true", matchIfMissing = false) +@ConditionalOnProperty(value = AUTHENTICATION_ERROR_EVENTING_ENABLED, havingValue = "true", matchIfMissing = false) public class AuthenticationErrorEventingPublisher { private static final String REQUEST_SIGNATURE = "requestSignature"; diff --git a/authentication/authentication-core/src/main/java/io/mosip/authentication/core/constant/IdAuthConfigKeyConstants.java b/authentication/authentication-core/src/main/java/io/mosip/authentication/core/constant/IdAuthConfigKeyConstants.java index c2a86604265..79f48e37800 100644 --- a/authentication/authentication-core/src/main/java/io/mosip/authentication/core/constant/IdAuthConfigKeyConstants.java +++ b/authentication/authentication-core/src/main/java/io/mosip/authentication/core/constant/IdAuthConfigKeyConstants.java @@ -174,4 +174,6 @@ private IdAuthConfigKeyConstants() { public static final String KYC_EXCHANGE_DEFAULT_LANGUAGE = "mosip.ida.kyc.exchange.default.lang"; public static final String IDP_AMR_ACR_IDA_MAPPING_SOURCE = "idp.amr-acr.ida.mapping.property.source"; + + public static final String AUTHENTICATION_ERROR_EVENTING_ENABLED = "mosip.ida.authentication.error.eventing.enabled"; } diff --git a/authentication/authentication-otp-service/src/main/java/io/mosip/authentication/otp/service/controller/OTPController.java b/authentication/authentication-otp-service/src/main/java/io/mosip/authentication/otp/service/controller/OTPController.java index 5dbf15e9e50..b6b81ff84a4 100644 --- a/authentication/authentication-otp-service/src/main/java/io/mosip/authentication/otp/service/controller/OTPController.java +++ b/authentication/authentication-otp-service/src/main/java/io/mosip/authentication/otp/service/controller/OTPController.java @@ -3,6 +3,7 @@ import java.util.Objects; import java.util.Optional; +import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; @@ -14,7 +15,10 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityScheme; import io.swagger.v3.oas.annotations.tags.Tag; + +import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.validation.Errors; import org.springframework.web.bind.WebDataBinder; @@ -54,6 +58,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import springfox.documentation.annotations.ApiIgnore; +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_ENABLED; /** * The {@code OTPAuthController} use to send request to generate otp. @@ -94,11 +99,23 @@ public class OTPController { @Autowired(required = false) private AuthenticationErrorEventingPublisher authenticationErrorEventingPublisher; + + @Value("${"+ AUTHENTICATION_ERROR_EVENTING_ENABLED +":false}") + private boolean isEventingEnabled; @InitBinder private void initBinder(WebDataBinder binder) { binder.setValidator(otpRequestValidator); } + + @PostConstruct + public void init() { + if (isEventingEnabled) { + if (Objects.isNull(authenticationErrorEventingPublisher)) { + throw new BeanCreationException(AuthenticationErrorEventingPublisher.class.getName(), "Failed to create a bean"); + } + } + } /** * send OtpRequestDTO request to generate OTP and received OtpResponseDTO as @@ -163,7 +180,7 @@ public OtpResponseDTO generateOTP(@Valid @RequestBody OtpRequestDTO otpRequestDt } catch (IdAuthenticationBusinessException e) { logger.error(IdAuthCommonConstants.SESSION_ID, e.getClass().toString(), e.getErrorCode(), e.getErrorText()); - if (authenticationErrorEventingPublisher != null) { + if (isEventingEnabled) { if (IdAuthenticationErrorConstants.ID_NOT_AVAILABLE.getErrorCode().equals(e.getErrorCode())) { authenticationErrorEventingPublisher.notify(otpRequestDto, request.getHeader("signature"), partner, e, otpRequestDto.getMetadata()); diff --git a/authentication/authentication-service/src/main/java/io/mosip/authentication/service/controller/AuthController.java b/authentication/authentication-service/src/main/java/io/mosip/authentication/service/controller/AuthController.java index faf7c605716..e5251017fe2 100644 --- a/authentication/authentication-service/src/main/java/io/mosip/authentication/service/controller/AuthController.java +++ b/authentication/authentication-service/src/main/java/io/mosip/authentication/service/controller/AuthController.java @@ -3,9 +3,12 @@ import java.util.Objects; import java.util.Optional; +import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.validation.Errors; import org.springframework.validation.annotation.Validated; @@ -54,6 +57,7 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme; import io.swagger.v3.oas.annotations.tags.Tag; import springfox.documentation.annotations.ApiIgnore; +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_ENABLED; /** * The {@code AuthController} used to handle all the authentication requests. @@ -92,6 +96,9 @@ public class AuthController { @Autowired(required = false) private AuthenticationErrorEventingPublisher authenticationErrorEventingPublisher; + + @Value("${"+ AUTHENTICATION_ERROR_EVENTING_ENABLED +":false}") + private boolean isEventingEnabled; /** @@ -102,6 +109,15 @@ public class AuthController { private void initAuthRequestBinder(WebDataBinder binder) { binder.setValidator(authRequestValidator); } + + @PostConstruct + public void init() { + if (isEventingEnabled) { + if (Objects.isNull(authenticationErrorEventingPublisher)) { + throw new BeanCreationException(AuthenticationErrorEventingPublisher.class.getName(), "Failed to create a bean"); + } + } + } /** * authenticateRequest - method to authenticate request. @@ -162,7 +178,7 @@ public AuthResponseDTO authenticateIndividual(@Validated @RequestBody AuthReques } catch (IdAuthenticationBusinessException e) { mosipLogger.error(IdAuthCommonConstants.SESSION_ID, this.getClass().getSimpleName(), "authenticateApplication", e.getErrorCode() + " : " + e.getErrorText()); - if (authenticationErrorEventingPublisher != null) { + if (isEventingEnabled) { if (IdAuthenticationErrorConstants.ID_NOT_AVAILABLE.getErrorCode().equals(e.getErrorCode())) { authenticationErrorEventingPublisher.notify(authrequestdto, request.getHeader("signature"), partner, e, authrequestdto.getMetadata()); diff --git a/authentication/authentication-service/src/main/java/io/mosip/authentication/service/kyc/controller/KycAuthController.java b/authentication/authentication-service/src/main/java/io/mosip/authentication/service/kyc/controller/KycAuthController.java index 51a1c96fe07..148c916a30b 100644 --- a/authentication/authentication-service/src/main/java/io/mosip/authentication/service/kyc/controller/KycAuthController.java +++ b/authentication/authentication-service/src/main/java/io/mosip/authentication/service/kyc/controller/KycAuthController.java @@ -4,10 +4,14 @@ import java.util.Objects; import java.util.Optional; +import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import io.mosip.authentication.core.indauth.dto.*; + +import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.validation.Errors; import org.springframework.validation.annotation.Validated; @@ -56,6 +60,7 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme; import io.swagger.v3.oas.annotations.tags.Tag; import springfox.documentation.annotations.ApiIgnore; +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.AUTHENTICATION_ERROR_EVENTING_ENABLED; /** * The {@code AuthController} used to handle all the authentication requests. @@ -102,6 +107,9 @@ public class KycAuthController { @Autowired(required = false) private AuthenticationErrorEventingPublisher authenticationErrorEventingPublisher; + + @Value("${"+ AUTHENTICATION_ERROR_EVENTING_ENABLED +":false}") + private boolean isEventingEnabled; /** * @@ -129,6 +137,15 @@ private void initEKycBinder(WebDataBinder binder) { private void initKycExchangeBinder(WebDataBinder binder) { binder.setValidator(kycExchangeValidator); } + + @PostConstruct + public void init() { + if (isEventingEnabled) { + if (Objects.isNull(authenticationErrorEventingPublisher)) { + throw new BeanCreationException(AuthenticationErrorEventingPublisher.class.getName(), "Failed to create a bean"); + } + } + } /** @@ -197,7 +214,7 @@ public EKycAuthResponseDTO processKyc(@Validated @RequestBody EkycAuthRequestDTO } catch (IdAuthenticationBusinessException e) { mosipLogger.error(IdAuthCommonConstants.SESSION_ID, this.getClass().getSimpleName(), "processEKyc", e.getErrorTexts().isEmpty() ? "" : e.getErrorText()); - if (authenticationErrorEventingPublisher != null) { + if (isEventingEnabled) { if (IdAuthenticationErrorConstants.ID_NOT_AVAILABLE.getErrorCode().equals(e.getErrorCode())) { authenticationErrorEventingPublisher.notify(ekycAuthRequestDTO, request.getHeader("signature"), partner, e, ekycAuthRequestDTO.getMetadata()); @@ -280,7 +297,7 @@ public KycAuthResponseDTO processKycAuth(@Validated @RequestBody KycAuthRequestD mosipLogger.error(IdAuthCommonConstants.SESSION_ID, this.getClass().getSimpleName(), "processKycAuth", e.getErrorTexts().isEmpty() ? "" : e.getErrorText()); - if (authenticationErrorEventingPublisher != null) { + if (isEventingEnabled) { if (IdAuthenticationErrorConstants.ID_NOT_AVAILABLE.getErrorCode().equals(e.getErrorCode())) { authenticationErrorEventingPublisher.notify(authRequestDTO, request.getHeader("signature"), partner, e, authRequestDTO.getMetadata());