From 046bb2181139b79e4ec5030be7087620c443d47c Mon Sep 17 00:00:00 2001 From: Rohit Date: Mon, 20 May 2024 21:45:44 +0530 Subject: [PATCH] feature/registration-otp-mail-sending --- pom.xml | 9 + .../email/configuration/ApplicationBeans.java | 69 ++++ .../email/configuration/MailConfig.java | 44 +++ .../email/constants/BeanNameConstants.java | 5 + .../email/constants/EnvironmentConstants.java | 5 + .../email/constants/InternalErrorCodes.java | 1 + .../email/constants/ServiceConstants.java | 2 + .../com/bloggios/email/dao/PgAbstractDao.java | 62 ++++ .../pgabstractdao/MailHistoryEntityDao.java | 23 ++ .../pgrepository/MailHistoryRepository.java | 16 + .../email/entity/MailHistoryEntity.java | 46 +++ .../com/bloggios/email/enums/DaoStatus.java | 16 + .../com/bloggios/email/enums/MailFor.java | 15 + .../bloggios/email/enums/MailProvider.java | 16 + .../payloads/InternalException.java | 23 ++ .../bloggios/email/javamail/GmailSending.java | 46 +++ .../RegistrationOtpMailSending.java | 57 +++ .../listeners/RegistrationOtpListener.java | 31 +- .../payload/event/RegisterOtpMailEvent.java | 27 ++ .../payload/response/ApplicationResponse.java | 26 ++ .../payload/response/ExceptionResponse.java | 28 ++ .../com/bloggios/email/processor/Process.java | 39 +++ .../RegisterOtpEventProcessor.java | 80 +++++ .../email/properties/MailProperties.java | 25 ++ .../email/service/MailSendingService.java | 18 + .../bloggios/email/transformer/Transform.java | 16 + .../RegisterOtpToMailHistoryTransformer.java | 48 +++ .../email/ymlparser/ImportErrorMessages.java | 45 +++ src/main/resources/application.yml | 22 +- src/main/resources/error-messages.properties | 2 + .../resources/templates/RegistrationOtp.html | 328 ++++++++++++++++++ 31 files changed, 1188 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/bloggios/email/configuration/ApplicationBeans.java create mode 100644 src/main/java/com/bloggios/email/configuration/MailConfig.java create mode 100644 src/main/java/com/bloggios/email/dao/PgAbstractDao.java create mode 100644 src/main/java/com/bloggios/email/dao/implementation/pgabstractdao/MailHistoryEntityDao.java create mode 100644 src/main/java/com/bloggios/email/dao/repository/pgrepository/MailHistoryRepository.java create mode 100644 src/main/java/com/bloggios/email/entity/MailHistoryEntity.java create mode 100644 src/main/java/com/bloggios/email/enums/DaoStatus.java create mode 100644 src/main/java/com/bloggios/email/enums/MailFor.java create mode 100644 src/main/java/com/bloggios/email/enums/MailProvider.java create mode 100644 src/main/java/com/bloggios/email/exceptions/payloads/InternalException.java create mode 100644 src/main/java/com/bloggios/email/javamail/GmailSending.java create mode 100644 src/main/java/com/bloggios/email/javamail/implementation/RegistrationOtpMailSending.java create mode 100644 src/main/java/com/bloggios/email/payload/event/RegisterOtpMailEvent.java create mode 100644 src/main/java/com/bloggios/email/payload/response/ApplicationResponse.java create mode 100644 src/main/java/com/bloggios/email/payload/response/ExceptionResponse.java create mode 100644 src/main/java/com/bloggios/email/processor/Process.java create mode 100644 src/main/java/com/bloggios/email/processor/implementation/RegisterOtpEventProcessor.java create mode 100644 src/main/java/com/bloggios/email/properties/MailProperties.java create mode 100644 src/main/java/com/bloggios/email/service/MailSendingService.java create mode 100644 src/main/java/com/bloggios/email/transformer/Transform.java create mode 100644 src/main/java/com/bloggios/email/transformer/implementation/transform/RegisterOtpToMailHistoryTransformer.java create mode 100644 src/main/java/com/bloggios/email/ymlparser/ImportErrorMessages.java create mode 100644 src/main/resources/error-messages.properties create mode 100644 src/main/resources/templates/RegistrationOtp.html diff --git a/pom.xml b/pom.xml index 1f3e2c9..a2b7524 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,15 @@ logstash-logback-encoder 6.6 + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + runtime + diff --git a/src/main/java/com/bloggios/email/configuration/ApplicationBeans.java b/src/main/java/com/bloggios/email/configuration/ApplicationBeans.java new file mode 100644 index 0000000..485473c --- /dev/null +++ b/src/main/java/com/bloggios/email/configuration/ApplicationBeans.java @@ -0,0 +1,69 @@ +/* + * Copyright © 2023-2024 Rohit Parihar and Bloggios + * All rights reserved. + * This software is the property of Rohit Parihar and is protected by copyright law. + * The software, including its source code, documentation, and associated files, may not be used, copied, modified, distributed, or sublicensed without the express written consent of Rohit Parihar. + * For licensing and usage inquiries, please contact Rohit Parihar at rohitparih@gmail.com, or you can also contact support@bloggios.com. + * This software is provided as-is, and no warranties or guarantees are made regarding its fitness for any particular purpose or compatibility with any specific technology. + * For license information and terms of use, please refer to the accompanying LICENSE file or visit http://www.apache.org/licenses/LICENSE-2.0. + * Unauthorized use of this software may result in legal action and liability for damages. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bloggios.email.configuration; + +import com.bloggios.email.constants.BeanNameConstants; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +/** + * Owner - Rohit Parihar + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.configuration + * Created_on - 04 May-2024 + * Created_at - 20 : 07 + */ + +@Configuration +public class ApplicationBeans { + + @Bean(BeanNameConstants.ASYNC_TASK_INTERNAL_POOL) + public TaskExecutor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(5); + executor.setMaxPoolSize(10); + executor.setQueueCapacity(20); + executor.setThreadNamePrefix("mail-internal-pool-"); + executor.setAllowCoreThreadTimeOut(true); + executor.setKeepAliveSeconds(60); + executor.initialize(); + return executor; + } + + @Bean(BeanNameConstants.ASYNC_TASK_EXTERNAL_POOL) + public TaskExecutor externalExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(7); + executor.setMaxPoolSize(16); + executor.setQueueCapacity(40); + executor.setThreadNamePrefix("user-external-pool-"); + executor.setAllowCoreThreadTimeOut(true); + executor.setKeepAliveSeconds(60); + executor.initialize(); + return executor; + } +} diff --git a/src/main/java/com/bloggios/email/configuration/MailConfig.java b/src/main/java/com/bloggios/email/configuration/MailConfig.java new file mode 100644 index 0000000..d9a5dd0 --- /dev/null +++ b/src/main/java/com/bloggios/email/configuration/MailConfig.java @@ -0,0 +1,44 @@ +package com.bloggios.email.configuration; + +import com.bloggios.email.constants.BeanNameConstants; +import com.bloggios.email.constants.EnvironmentConstants; +import com.bloggios.email.properties.MailProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.util.Objects; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.configuration + * Created_on - May 05 - 2024 + * Created_at - 16:17 + */ + +@Configuration +public class MailConfig { + + private final Environment environment; + + public MailConfig( + Environment environment + ) { + this.environment = environment; + } + + @Bean(BeanNameConstants.GOOGLE_MAIL_SENDER_BEAN) + public JavaMailSender getGoogleMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost(environment.getProperty(EnvironmentConstants.GOOGLE_MAIL_HOST)); + mailSender.setPort(Integer.parseInt(Objects.requireNonNull(environment.getProperty(EnvironmentConstants.GOOGLE_MAIL_PORT)))); + mailSender.setUsername(environment.getProperty(EnvironmentConstants.GOOGLE_MAIL_USERNAME)); + mailSender.setPassword(environment.getProperty(EnvironmentConstants.GOOGLE_MAIL_PASSWORD)); + MailProperties.mailProperties(mailSender); + return mailSender; + } +} diff --git a/src/main/java/com/bloggios/email/constants/BeanNameConstants.java b/src/main/java/com/bloggios/email/constants/BeanNameConstants.java index 4099393..edb7f72 100644 --- a/src/main/java/com/bloggios/email/constants/BeanNameConstants.java +++ b/src/main/java/com/bloggios/email/constants/BeanNameConstants.java @@ -16,4 +16,9 @@ public class BeanNameConstants { public static final String INCOMING_MESSAGE_FACTORY_COMPONENT = "incomingMessageFactoryComponent"; public static final String MANAGEMENT_CONSUMER_FACTORY = "managementConsumerFactory"; + public static final String ASYNC_TASK_INTERNAL_POOL = "asyncTaskInternalPool"; + public static final String ASYNC_TASK_EXTERNAL_POOL = "asyncTaskExternalPool"; + public static final String GOOGLE_MAIL_SENDER_BEAN = "googleMailSenderBean"; + public static final String ERROR_CODES_BEAN = "errorCodesBean"; + public static final String REGISTER_OTP_EVENT_PROCESSOR = "registerOtpEventProcessor"; } diff --git a/src/main/java/com/bloggios/email/constants/EnvironmentConstants.java b/src/main/java/com/bloggios/email/constants/EnvironmentConstants.java index 418ae59..5d2c7eb 100644 --- a/src/main/java/com/bloggios/email/constants/EnvironmentConstants.java +++ b/src/main/java/com/bloggios/email/constants/EnvironmentConstants.java @@ -16,4 +16,9 @@ public class EnvironmentConstants { public static final String KAFKA_GROUP_ID = "mail-service.kafka.group-id"; public static final String REGISTRATION_OTP_TOPIC = "mail-service.kafka.consumer.topics.registration-otp"; + public static final String GOOGLE_MAIL_HOST = "mail-configuration.google-mail.host"; + public static final String GOOGLE_MAIL_PORT = "mail-configuration.google-mail.port"; + public static final String GOOGLE_MAIL_PASSWORD = "mail-configuration.google-mail.password"; + public static final String GOOGLE_MAIL_USERNAME = "mail-configuration.google-mail.username"; + public static final String APPLICATION_VERSION = "application.version"; } diff --git a/src/main/java/com/bloggios/email/constants/InternalErrorCodes.java b/src/main/java/com/bloggios/email/constants/InternalErrorCodes.java index c5b2869..3fec74c 100644 --- a/src/main/java/com/bloggios/email/constants/InternalErrorCodes.java +++ b/src/main/java/com/bloggios/email/constants/InternalErrorCodes.java @@ -15,4 +15,5 @@ public class InternalErrorCodes { public static final String JSON_DESERIALIZATION = "IE__MAIL-1001"; + public static final String EXCEPTION_CODES = "IE__MAIL-1002"; } diff --git a/src/main/java/com/bloggios/email/constants/ServiceConstants.java b/src/main/java/com/bloggios/email/constants/ServiceConstants.java index e46b17a..e007bc8 100644 --- a/src/main/java/com/bloggios/email/constants/ServiceConstants.java +++ b/src/main/java/com/bloggios/email/constants/ServiceConstants.java @@ -20,4 +20,6 @@ public class ServiceConstants { public static final String TOPICS = "Topics"; public static final String SERVICE_NAME = "ServiceName"; public static final String SERVICE_CONTAINER_FACTORY_NAME = "ServiceContainerFactoryName"; + public static final String RANDOM_UUID = "randomUUID"; + public static final String BREADCRUMB_ID = "breadcrumbId"; } diff --git a/src/main/java/com/bloggios/email/dao/PgAbstractDao.java b/src/main/java/com/bloggios/email/dao/PgAbstractDao.java new file mode 100644 index 0000000..59967eb --- /dev/null +++ b/src/main/java/com/bloggios/email/dao/PgAbstractDao.java @@ -0,0 +1,62 @@ +/* + * Copyright © 2023-2024 Rohit Parihar and Bloggios + * All rights reserved. + * This software is the property of Rohit Parihar and is protected by copyright law. + * The software, including its source code, documentation, and associated files, may not be used, copied, modified, distributed, or sublicensed without the express written consent of Rohit Parihar. + * For licensing and usage inquiries, please contact Rohit Parihar at rohitparih@gmail.com, or you can also contact support@bloggios.com. + * This software is provided as-is, and no warranties or guarantees are made regarding its fitness for any particular purpose or compatibility with any specific technology. + * For license information and terms of use, please refer to the accompanying LICENSE file or visit http://www.apache.org/licenses/LICENSE-2.0. + * Unauthorized use of this software may result in legal action and liability for damages. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bloggios.email.dao; + +import com.bloggios.email.enums.DaoStatus; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - auth-server + * Package - com.bloggios.authserver.dao + * Created_on - May 02 - 2024 + * Created_at - 12:33 + */ + +public abstract class PgAbstractDao> { + + protected final B repository; + + protected PgAbstractDao( + B repository + ) { + this.repository = repository; + } + + public final A initOperation(DaoStatus status, A a) { + return switch (status) { + case CREATE -> initCreate(a); + case UPDATE -> initUpdate(a); + }; + } + + protected A initUpdate(A a) { + return repository.save(a); + } + + protected A initCreate(A a) { + return repository.save(a); + } +} diff --git a/src/main/java/com/bloggios/email/dao/implementation/pgabstractdao/MailHistoryEntityDao.java b/src/main/java/com/bloggios/email/dao/implementation/pgabstractdao/MailHistoryEntityDao.java new file mode 100644 index 0000000..abfa570 --- /dev/null +++ b/src/main/java/com/bloggios/email/dao/implementation/pgabstractdao/MailHistoryEntityDao.java @@ -0,0 +1,23 @@ +package com.bloggios.email.dao.implementation.pgabstractdao; + +import com.bloggios.email.dao.PgAbstractDao; +import com.bloggios.email.dao.repository.pgrepository.MailHistoryRepository; +import com.bloggios.email.entity.MailHistoryEntity; +import org.springframework.stereotype.Component; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.dao.implementation.pgabstractdao + * Created_on - May 06 - 2024 + * Created_at - 20:03 + */ + +@Component +public class MailHistoryEntityDao extends PgAbstractDao { + + public MailHistoryEntityDao(MailHistoryRepository repository) { + super(repository); + } +} diff --git a/src/main/java/com/bloggios/email/dao/repository/pgrepository/MailHistoryRepository.java b/src/main/java/com/bloggios/email/dao/repository/pgrepository/MailHistoryRepository.java new file mode 100644 index 0000000..d0225ad --- /dev/null +++ b/src/main/java/com/bloggios/email/dao/repository/pgrepository/MailHistoryRepository.java @@ -0,0 +1,16 @@ +package com.bloggios.email.dao.repository.pgrepository; + +import com.bloggios.email.entity.MailHistoryEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.dao.repository.pgrepository + * Created_on - May 06 - 2024 + * Created_at - 20:02 + */ + +public interface MailHistoryRepository extends JpaRepository { +} diff --git a/src/main/java/com/bloggios/email/entity/MailHistoryEntity.java b/src/main/java/com/bloggios/email/entity/MailHistoryEntity.java new file mode 100644 index 0000000..968b481 --- /dev/null +++ b/src/main/java/com/bloggios/email/entity/MailHistoryEntity.java @@ -0,0 +1,46 @@ +package com.bloggios.email.entity; + +import com.bloggios.email.constants.ServiceConstants; +import com.bloggios.email.enums.MailFor; +import com.bloggios.email.enums.MailProvider; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; +import org.hibernate.annotations.GenericGenerator; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.Date; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.entity + * Created_on - May 06 - 2024 + * Created_at - 19:49 + */ + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +@Table(name = "mail_history") +public class MailHistoryEntity { + + @Id + @GeneratedValue(generator = ServiceConstants.RANDOM_UUID) + @GenericGenerator(name = ServiceConstants.RANDOM_UUID, strategy = "org.hibernate.id.UUIDGenerator") + private String mailHistoryId; + + private String mailTo; + private String mailContent; + private MailProvider mailProvider; + private MailFor mailFor; + private Date dateSent; + private String apiVersion; +} diff --git a/src/main/java/com/bloggios/email/enums/DaoStatus.java b/src/main/java/com/bloggios/email/enums/DaoStatus.java new file mode 100644 index 0000000..ad8723e --- /dev/null +++ b/src/main/java/com/bloggios/email/enums/DaoStatus.java @@ -0,0 +1,16 @@ +package com.bloggios.email.enums; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - auth-server + * Package - com.bloggios.authserver.enums + * Created_on - May 02 - 2024 + * Created_at - 15:25 + */ + +public enum DaoStatus { + + CREATE, + UPDATE +} diff --git a/src/main/java/com/bloggios/email/enums/MailFor.java b/src/main/java/com/bloggios/email/enums/MailFor.java new file mode 100644 index 0000000..cd5275d --- /dev/null +++ b/src/main/java/com/bloggios/email/enums/MailFor.java @@ -0,0 +1,15 @@ +package com.bloggios.email.enums; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.enums + * Created_on - May 06 - 2024 + * Created_at - 19:57 + */ + +public enum MailFor { + + REGISTER_OTP +} diff --git a/src/main/java/com/bloggios/email/enums/MailProvider.java b/src/main/java/com/bloggios/email/enums/MailProvider.java new file mode 100644 index 0000000..6bbd7a0 --- /dev/null +++ b/src/main/java/com/bloggios/email/enums/MailProvider.java @@ -0,0 +1,16 @@ +package com.bloggios.email.enums; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.enums + * Created_on - May 06 - 2024 + * Created_at - 19:56 + */ + +public enum MailProvider { + + GMAIL, + GODADDY +} diff --git a/src/main/java/com/bloggios/email/exceptions/payloads/InternalException.java b/src/main/java/com/bloggios/email/exceptions/payloads/InternalException.java new file mode 100644 index 0000000..df24cc7 --- /dev/null +++ b/src/main/java/com/bloggios/email/exceptions/payloads/InternalException.java @@ -0,0 +1,23 @@ +package com.bloggios.email.exceptions.payloads; + +import com.bloggios.email.exceptions.ExceptionProvider; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.exceptions.payloads + * Created_on - May 20 - 2024 + * Created_at - 20:29 + */ + +@Getter +@EqualsAndHashCode(callSuper = true) +public class InternalException extends ExceptionProvider { + + public InternalException(String code) { + super(code); + } +} diff --git a/src/main/java/com/bloggios/email/javamail/GmailSending.java b/src/main/java/com/bloggios/email/javamail/GmailSending.java new file mode 100644 index 0000000..f163eca --- /dev/null +++ b/src/main/java/com/bloggios/email/javamail/GmailSending.java @@ -0,0 +1,46 @@ +package com.bloggios.email.javamail; + +import com.bloggios.email.constants.BeanNameConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.util.Date; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.javamail + * Created_on - May 05 - 2024 + * Created_at - 16:30 + */ + +@Component +public abstract class GmailSending { + + private static final Logger logger = LoggerFactory.getLogger(GmailSending.class); + + private final JavaMailSender javaMailSender; + + protected GmailSending( + @Qualifier(BeanNameConstants.GOOGLE_MAIL_SENDER_BEAN) JavaMailSender javaMailSender + ) { + this.javaMailSender = javaMailSender; + } + + @Async(BeanNameConstants.ASYNC_TASK_INTERNAL_POOL) + public void sendMail(A a) throws MessagingException { + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + mailingData(mimeMessage, a); + javaMailSender.send(mimeMessage); + logger.info("Mail sent using Gmail @{}", new Date()); + } + + public abstract void mailingData(MimeMessage message, A a) throws MessagingException; +} diff --git a/src/main/java/com/bloggios/email/javamail/implementation/RegistrationOtpMailSending.java b/src/main/java/com/bloggios/email/javamail/implementation/RegistrationOtpMailSending.java new file mode 100644 index 0000000..5ef62d3 --- /dev/null +++ b/src/main/java/com/bloggios/email/javamail/implementation/RegistrationOtpMailSending.java @@ -0,0 +1,57 @@ +package com.bloggios.email.javamail.implementation; + +import com.bloggios.email.constants.EnvironmentConstants; +import com.bloggios.email.javamail.GmailSending; +import com.bloggios.email.payload.event.RegisterOtpMailEvent; +import org.springframework.core.env.Environment; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.util.Objects; + +/** + * Owner - Rohit Parihar + * Author - rohit + * Project - bloggios-notification-service + * Package - com.bloggios.notification.service.javamail.implementation + * Created_on - 19 December-2023 + * Created_at - 22 : 20 + */ + +@Component +public class RegistrationOtpMailSending extends GmailSending { + + private final TemplateEngine templateEngine; + private final Environment environment; + + protected RegistrationOtpMailSending( + JavaMailSender javaMailSender, + TemplateEngine templateEngine, + Environment environment + ) { + super(javaMailSender); + this.templateEngine = templateEngine; + this.environment = environment; + } + + private String getContext(RegisterOtpMailEvent registerOtpMailEvent) { + Context context = new Context(); + context.setVariable("otpPayload", registerOtpMailEvent); + return templateEngine.process("RegistrationOtp", context); + } + + @Override + public void mailingData(MimeMessage message, RegisterOtpMailEvent request) throws MessagingException { + String context = getContext(request); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(Objects.requireNonNull(environment.getProperty(EnvironmentConstants.GOOGLE_MAIL_USERNAME))); + helper.setSubject("Bloggios | Registration OTP"); + helper.setText(context, true); + helper.setTo(request.getEmail()); + } +} diff --git a/src/main/java/com/bloggios/email/kafka/cosumer/listeners/RegistrationOtpListener.java b/src/main/java/com/bloggios/email/kafka/cosumer/listeners/RegistrationOtpListener.java index 4c80ca0..89b5961 100644 --- a/src/main/java/com/bloggios/email/kafka/cosumer/listeners/RegistrationOtpListener.java +++ b/src/main/java/com/bloggios/email/kafka/cosumer/listeners/RegistrationOtpListener.java @@ -1,11 +1,21 @@ package com.bloggios.email.kafka.cosumer.listeners; +import com.bloggios.email.constants.InternalErrorCodes; +import com.bloggios.email.constants.ServiceConstants; +import com.bloggios.email.exceptions.ExceptionProvider; +import com.bloggios.email.exceptions.payloads.InitializationException; import com.bloggios.email.payload.IncomingMessage; +import com.bloggios.email.payload.event.RegisterOtpMailEvent; +import com.bloggios.email.processor.Process; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.slf4j.MDC; import org.springframework.kafka.listener.ConsumerAwareMessageListener; import org.springframework.stereotype.Component; +import java.io.IOException; + /** * Owner - Rohit Parihar and Bloggios * Author - rohit @@ -18,8 +28,27 @@ @Component public class RegistrationOtpListener implements ConsumerAwareMessageListener { + private static final ObjectMapper mapper = new ObjectMapper(); + private final Process registerOtpMailEventProcess; + + public RegistrationOtpListener( + Process registerOtpMailEventProcess + ) { + this.registerOtpMailEventProcess = registerOtpMailEventProcess; + } + @Override public void onMessage(ConsumerRecord consumerRecord, Consumer consumer) { - System.err.println(consumerRecord.value()); + try { + RegisterOtpMailEvent registerOtpMailEvent = mapper.readValue(mapper.writeValueAsBytes(consumerRecord.value().getMessageData().getData()), RegisterOtpMailEvent.class); + String breadcrumbId = mapper.readValue(mapper.writeValueAsBytes(consumerRecord.value().getMessageData().getBreadcrumbId()), String.class); + MDC.put(ServiceConstants.BREADCRUMB_ID, breadcrumbId); + registerOtpMailEventProcess.process(registerOtpMailEvent); + } catch (IOException ignored) { + throw new InitializationException(InternalErrorCodes.JSON_DESERIALIZATION); + } finally { + MDC.remove(ServiceConstants.BREADCRUMB_ID); + consumer.commitAsync(); + } } } diff --git a/src/main/java/com/bloggios/email/payload/event/RegisterOtpMailEvent.java b/src/main/java/com/bloggios/email/payload/event/RegisterOtpMailEvent.java new file mode 100644 index 0000000..6a03e94 --- /dev/null +++ b/src/main/java/com/bloggios/email/payload/event/RegisterOtpMailEvent.java @@ -0,0 +1,27 @@ +package com.bloggios.email.payload.event; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.payload.request + * Created_on - May 05 - 2024 + * Created_at - 16:42 + */ + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class RegisterOtpMailEvent { + + private String otp; + private String email; +} diff --git a/src/main/java/com/bloggios/email/payload/response/ApplicationResponse.java b/src/main/java/com/bloggios/email/payload/response/ApplicationResponse.java new file mode 100644 index 0000000..c8aecec --- /dev/null +++ b/src/main/java/com/bloggios/email/payload/response/ApplicationResponse.java @@ -0,0 +1,26 @@ +package com.bloggios.email.payload.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.payload.response + * Created_on - May 05 - 2024 + * Created_at - 16:40 + */ + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class ApplicationResponse { + + private String message; + private String userId; + private String id; +} diff --git a/src/main/java/com/bloggios/email/payload/response/ExceptionResponse.java b/src/main/java/com/bloggios/email/payload/response/ExceptionResponse.java new file mode 100644 index 0000000..6334ae2 --- /dev/null +++ b/src/main/java/com/bloggios/email/payload/response/ExceptionResponse.java @@ -0,0 +1,28 @@ +package com.bloggios.email.payload.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.payload.response + * Created_on - May 01 - 2024 + * Created_at - 16:37 + */ + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class ExceptionResponse { + + private String message; + private String code; + private String field; + private String type; + private String status; +} diff --git a/src/main/java/com/bloggios/email/processor/Process.java b/src/main/java/com/bloggios/email/processor/Process.java new file mode 100644 index 0000000..c2b19ed --- /dev/null +++ b/src/main/java/com/bloggios/email/processor/Process.java @@ -0,0 +1,39 @@ +/* + * Copyright © 2023-2024 Rohit Parihar and Bloggios + * All rights reserved. + * This software is the property of Rohit Parihar and is protected by copyright law. + * The software, including its source code, documentation, and associated files, may not be used, copied, modified, distributed, or sublicensed without the express written consent of Rohit Parihar. + * For licensing and usage inquiries, please contact Rohit Parihar at rohitparih@gmail.com, or you can also contact support@bloggios.com. + * This software is provided as-is, and no warranties or guarantees are made regarding its fitness for any particular purpose or compatibility with any specific technology. + * For license information and terms of use, please refer to the accompanying LICENSE file or visit http://www.apache.org/licenses/LICENSE-2.0. + * Unauthorized use of this software may result in legal action and liability for damages. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bloggios.email.processor; + +/** + * Owner - Rohit Parihar + * Author - rohit + * Project - bloggios-notification-service + * Package - com.bloggios.notification.service.processor + * Created_on - 19 December-2023 + * Created_at - 19 : 44 + */ + +@FunctionalInterface +public interface Process { + + void process(A a); +} diff --git a/src/main/java/com/bloggios/email/processor/implementation/RegisterOtpEventProcessor.java b/src/main/java/com/bloggios/email/processor/implementation/RegisterOtpEventProcessor.java new file mode 100644 index 0000000..2eafac4 --- /dev/null +++ b/src/main/java/com/bloggios/email/processor/implementation/RegisterOtpEventProcessor.java @@ -0,0 +1,80 @@ +/* + * Copyright © 2023-2024 Rohit Parihar and Bloggios + * All rights reserved. + * This software is the property of Rohit Parihar and is protected by copyright law. + * The software, including its source code, documentation, and associated files, may not be used, copied, modified, distributed, or sublicensed without the express written consent of Rohit Parihar. + * For licensing and usage inquiries, please contact Rohit Parihar at rohitparih@gmail.com, or you can also contact support@bloggios.com. + * This software is provided as-is, and no warranties or guarantees are made regarding its fitness for any particular purpose or compatibility with any specific technology. + * For license information and terms of use, please refer to the accompanying LICENSE file or visit http://www.apache.org/licenses/LICENSE-2.0. + * Unauthorized use of this software may result in legal action and liability for damages. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bloggios.email.processor.implementation; + +import com.bloggios.email.constants.BeanNameConstants; +import com.bloggios.email.dao.implementation.pgabstractdao.MailHistoryEntityDao; +import com.bloggios.email.entity.MailHistoryEntity; +import com.bloggios.email.enums.DaoStatus; +import com.bloggios.email.javamail.implementation.RegistrationOtpMailSending; +import com.bloggios.email.payload.event.RegisterOtpMailEvent; +import com.bloggios.email.processor.Process; +import com.bloggios.email.transformer.implementation.transform.RegisterOtpToMailHistoryTransformer; +import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; + +/** + * Owner - Rohit Parihar + * Author - rohit + * Project - bloggios-notification-service + * Package - com.bloggios.notification.service.processor.implementation + * Created_on - 19 December-2023 + * Created_at - 19 : 46 + */ + +@Component(BeanNameConstants.REGISTER_OTP_EVENT_PROCESSOR) +public class RegisterOtpEventProcessor implements Process { + + private static final Logger logger = LoggerFactory.getLogger(RegisterOtpEventProcessor.class); + + private final RegistrationOtpMailSending registrationOtpMailSending; + private final RegisterOtpToMailHistoryTransformer registerOtpToMailHistoryTransformer; + private final MailHistoryEntityDao mailHistoryEntityDao; + + public RegisterOtpEventProcessor( + RegistrationOtpMailSending registrationOtpMailSending, + RegisterOtpToMailHistoryTransformer registerOtpToMailHistoryTransformer, + MailHistoryEntityDao mailHistoryEntityDao + ) { + this.registrationOtpMailSending = registrationOtpMailSending; + this.registerOtpToMailHistoryTransformer = registerOtpToMailHistoryTransformer; + this.mailHistoryEntityDao = mailHistoryEntityDao; + } + + @Override + @SneakyThrows(value = MessagingException.class) + @Async(BeanNameConstants.ASYNC_TASK_INTERNAL_POOL) + public void process(RegisterOtpMailEvent registerOtpMailEvent) { + long startTime = System.currentTimeMillis(); + registrationOtpMailSending.sendMail(registerOtpMailEvent); + MailHistoryEntity transform = registerOtpToMailHistoryTransformer.transform(registerOtpMailEvent); + mailHistoryEntityDao.initOperation(DaoStatus.CREATE, transform); + logger.info("Execution complete for Register OTP mail sending in {}ms", System.currentTimeMillis() - startTime); + } +} diff --git a/src/main/java/com/bloggios/email/properties/MailProperties.java b/src/main/java/com/bloggios/email/properties/MailProperties.java new file mode 100644 index 0000000..3673bfb --- /dev/null +++ b/src/main/java/com/bloggios/email/properties/MailProperties.java @@ -0,0 +1,25 @@ +package com.bloggios.email.properties; + +import lombok.experimental.UtilityClass; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.util.Properties; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.properties + * Created_on - May 20 - 2024 + * Created_at - 20:24 + */ + +@UtilityClass +public class MailProperties { + + public static void mailProperties(JavaMailSenderImpl javaMailSender) { + Properties props = javaMailSender.getJavaMailProperties(); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + } +} diff --git a/src/main/java/com/bloggios/email/service/MailSendingService.java b/src/main/java/com/bloggios/email/service/MailSendingService.java new file mode 100644 index 0000000..20a7aa2 --- /dev/null +++ b/src/main/java/com/bloggios/email/service/MailSendingService.java @@ -0,0 +1,18 @@ +package com.bloggios.email.service; + +import com.bloggios.email.payload.event.RegisterOtpMailEvent; +import com.bloggios.email.payload.response.ApplicationResponse; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.service + * Created_on - May 05 - 2024 + * Created_at - 16:44 + */ + +public interface MailSendingService { + + ApplicationResponse sendRegisterOtpMail(RegisterOtpMailEvent request); +} diff --git a/src/main/java/com/bloggios/email/transformer/Transform.java b/src/main/java/com/bloggios/email/transformer/Transform.java new file mode 100644 index 0000000..3c68922 --- /dev/null +++ b/src/main/java/com/bloggios/email/transformer/Transform.java @@ -0,0 +1,16 @@ +package com.bloggios.email.transformer; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.transformer + * Created_on - May 06 - 2024 + * Created_at - 20:05 + */ + +@FunctionalInterface +public interface Transform { + + A transform(B b); +} diff --git a/src/main/java/com/bloggios/email/transformer/implementation/transform/RegisterOtpToMailHistoryTransformer.java b/src/main/java/com/bloggios/email/transformer/implementation/transform/RegisterOtpToMailHistoryTransformer.java new file mode 100644 index 0000000..0ad781a --- /dev/null +++ b/src/main/java/com/bloggios/email/transformer/implementation/transform/RegisterOtpToMailHistoryTransformer.java @@ -0,0 +1,48 @@ +package com.bloggios.email.transformer.implementation.transform; + +import com.bloggios.email.constants.EnvironmentConstants; +import com.bloggios.email.entity.MailHistoryEntity; +import com.bloggios.email.enums.MailFor; +import com.bloggios.email.enums.MailProvider; +import com.bloggios.email.payload.event.RegisterOtpMailEvent; +import com.bloggios.email.transformer.Transform; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import java.util.Date; + +import static com.bloggios.email.constants.EnvironmentConstants.APPLICATION_VERSION; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.transformer.implementation.transform + * Created_on - May 06 - 2024 + * Created_at - 20:08 + */ + +@Component +public class RegisterOtpToMailHistoryTransformer implements Transform { + + private final Environment environment; + + public RegisterOtpToMailHistoryTransformer( + Environment environment + ) { + this.environment = environment; + } + + @Override + public MailHistoryEntity transform(RegisterOtpMailEvent request) { + return MailHistoryEntity + .builder() + .mailTo(request.getEmail()) + .mailContent(request.getOtp()) + .mailProvider(MailProvider.GMAIL) + .mailFor(MailFor.REGISTER_OTP) + .dateSent(new Date()) + .apiVersion(environment.getProperty(EnvironmentConstants.APPLICATION_VERSION)) + .build(); + } +} diff --git a/src/main/java/com/bloggios/email/ymlparser/ImportErrorMessages.java b/src/main/java/com/bloggios/email/ymlparser/ImportErrorMessages.java new file mode 100644 index 0000000..ff3ec0b --- /dev/null +++ b/src/main/java/com/bloggios/email/ymlparser/ImportErrorMessages.java @@ -0,0 +1,45 @@ +package com.bloggios.email.ymlparser; + +import com.bloggios.email.constants.BeanNameConstants; +import com.bloggios.email.constants.InternalErrorCodes; +import com.bloggios.email.exceptions.payloads.InternalException; +import com.thoughtworks.xstream.InitializationException; +import lombok.SneakyThrows; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.ResourceUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * Owner - Rohit Parihar and Bloggios + * Author - rohit + * Project - bloggios-email-service + * Package - com.bloggios.email.ymlparser + * Created_on - May 01 - 2024 + * Created_at - 16:16 + */ + +@Configuration +public class ImportErrorMessages { + + @SneakyThrows(value = {IOException.class, InitializationException.class}) + @Bean(BeanNameConstants.ERROR_CODES_BEAN) + public Properties errorMessages() { + File file = ResourceUtils.getFile("classpath:error-messages.properties"); + InputStream in = new FileInputStream(file); + Properties properties = new Properties(); + try { + properties.load(in); + } catch (IOException exception) { + throw new InternalException(InternalErrorCodes.EXCEPTION_CODES); + } finally { + in.close(); + } + return properties; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 67fb2e4..3084fed 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,6 +13,20 @@ spring: properties: spring.deserializer.value.delegate.class: org.springframework.kafka.support.serializer.JsonDeserializer spring.json.trusted.packages: com.bloggios.email + datasource: + url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT:5432}/${POSTGRES_DATABASE}?currentSchema=mail-service + hikari: + username: ${POSTGRES_USERNAME} + password: ${POSTGRES_PASSWORD} + maximum-pool-size: ${POSTGRES_POOL_SIZE:100} + minimum-idle: ${POSTGRES_IDLE:7} + leak-detection-threshold: 120000 + max-lifetime: 1800000 + idle-timeout: 600000 + connection-timeout: 600000 + jpa: + hibernate: + ddl-auto: update mail-service: kafka: group-id: ${ACTIVE_PROFILE:local}.mail-service @@ -37,4 +51,10 @@ eureka: allow-redirects: true service-url: defaultZone: ${EUREKA_URL} - enabled: false \ No newline at end of file + enabled: false +mail-configuration: + google-mail: # Please add your gmail details below + host: ${MAIL_HOST} + port: ${MAIL_PORT} + password: ${MAIL_PASSWORD} + username: ${MAIL_USERNAME} \ No newline at end of file diff --git a/src/main/resources/error-messages.properties b/src/main/resources/error-messages.properties new file mode 100644 index 0000000..a6387ed --- /dev/null +++ b/src/main/resources/error-messages.properties @@ -0,0 +1,2 @@ +IE__MAIL-1001=Exception Occurred while sending mail +IE__MAIL-1002=Unable to fetch exception codes diff --git a/src/main/resources/templates/RegistrationOtp.html b/src/main/resources/templates/RegistrationOtp.html new file mode 100644 index 0000000..671c10d --- /dev/null +++ b/src/main/resources/templates/RegistrationOtp.html @@ -0,0 +1,328 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file