Merge pull request 'custom exceptions added' (#8) from exception into master

Reviewed-on: #8
pull/10/head
commit 23ff720b71

@ -4,6 +4,8 @@ package entgra.mailsender.Controller;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.Service.MailService; import entgra.mailsender.Service.MailService;
import entgra.mailsender.exception.MailException; import entgra.mailsender.exception.MailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
@RestController @RestController
@RequestMapping("/api/message-distribution/email") @RequestMapping("/api/message-distribution/email")
@ -21,6 +24,9 @@ public class MailController {
@Autowired @Autowired
public MailService mailService; public MailService mailService;
private static final Logger logger = LoggerFactory.getLogger(MailController.class);
@PostMapping @PostMapping
public ResponseEntity<String> sendEmail(@ModelAttribute MailModel emailModel) { public ResponseEntity<String> sendEmail(@ModelAttribute MailModel emailModel) {
String msg; String msg;
@ -30,11 +36,12 @@ public class MailController {
return ResponseEntity.ok(msg); return ResponseEntity.ok(msg);
} catch (MailException e) { } catch (MailException e) {
msg = "Invalid email address"; msg = "Invalid email address";
logger.error(msg, e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg);
} catch (MessagingException | IOException exception) { } catch (MessagingException | SQLException | IOException exception) {
msg = "Failed to send email"; msg = "Failed to send email";
logger.error(msg, exception);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception);
} }
} }
} }

@ -4,12 +4,12 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.exception.DatabaseAccessException;
import entgra.mailsender.exception.FileConversionException;
import entgra.mailsender.exception.MailProcessingException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -33,11 +33,10 @@ private Connection getConnection() throws SQLException {
} }
@Override @Override
public int addMail(MailModel mailModel){ public int addMail(MailModel mailModel) throws SQLException {
PreparedStatement stmt;
int generatedId = -1;
try { try {
PreparedStatement stmt;
int generatedId;
Connection conn = this.getConnection(); Connection conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO email (EMAIL_ADDRESS, MSG_TEMPLATE, PRIORITY, ATTACHMENT, PARAMETERS, TIME, FILENAME, EXPIRY_AT) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement("INSERT INTO email (EMAIL_ADDRESS, MSG_TEMPLATE, PRIORITY, ATTACHMENT, PARAMETERS, TIME, FILENAME, EXPIRY_AT) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS);
Timestamp current_time = new Timestamp(System.currentTimeMillis()); Timestamp current_time = new Timestamp(System.currentTimeMillis());
@ -55,17 +54,18 @@ public int addMail(MailModel mailModel){
ResultSet rs = stmt.getGeneratedKeys(); ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) { if (rs.next()) {
generatedId = rs.getInt(1); generatedId = rs.getInt(1);
return generatedId;
} }
logger.info("Stored successfully");
} catch (SQLException e) { } catch (SQLException e) {
logger.info(e.getMessage()); logger.info(e.getMessage());
throw new RuntimeException(e); throw new SQLException("error processing sql !!", e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); logger.info(e.getMessage());
} throw new MailProcessingException("Error processing mailModel", e);
return generatedId;
} }
return 0;
}
@Override @Override
@ -85,7 +85,8 @@ public List<MailModel> getUnsentMessages(){
unsentMails.add(mailModel); unsentMails.add(mailModel);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new DatabaseAccessException("Error accessing database", e);
} }
return unsentMails; return unsentMails;
@ -117,17 +118,16 @@ public MailModel getMailDetails(Integer mail_id){
Blob blob = rs.getBlob("attachment"); Blob blob = rs.getBlob("attachment");
File file = convertBlobToFile(blob, filename); File file = convertBlobToFile(blob, filename);
mailModel.setFile(file); mailModel.setFile(file);
} }
return mailModel; return mailModel;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new MailProcessingException("Error processing mail", e);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new DatabaseAccessException("Error accessing database", e);
} }
} }
@ -138,6 +138,7 @@ private List<MailModel.Parameter> parseJsonParameters(String parametersJson) {
return objectMapper.readValue(parametersJson, new TypeReference<>() { return objectMapper.readValue(parametersJson, new TypeReference<>() {
}); });
} catch (IOException e) { } catch (IOException e) {
logger.info(e.getMessage());
throw new IllegalArgumentException("Error parsing parameters JSON", e); throw new IllegalArgumentException("Error parsing parameters JSON", e);
} }
} }
@ -150,31 +151,25 @@ public static File convertBlobToFile(Blob blob, String fileName) throws IOExcept
while (inputStream.read(buffer) > 0) { while (inputStream.read(buffer) > 0) {
outputStream.write(buffer); outputStream.write(buffer);
} }
} catch (IOException e) {
throw new FileConversionException("Error converting Blob to File", e);
} }
return file; return file;
} }
public void addToSentMail(Integer mail_id) { public void addToSentMail(Integer mail_id) {
PreparedStatement stmt;
try { try {
PreparedStatement stmt;
Connection connection = this.getConnection(); Connection connection = this.getConnection();
stmt = connection.prepareStatement("INSERT INTO sentEmail (email_id,sent_time) VALUE (?,?)"); stmt = connection.prepareStatement("INSERT INTO sentEmail (email_id,sent_time) VALUE (?,?)");
stmt.setInt(1, mail_id); stmt.setInt(1, mail_id);
stmt.setDate(2, Date.valueOf(LocalDate.now())); stmt.setDate(2, Date.valueOf(LocalDate.now()));
stmt.execute(); stmt.execute();
logger.info("added to sent_email successfully");
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new DatabaseAccessException("Error accessing database", e);
} }
} }
} }

@ -2,11 +2,12 @@ package entgra.mailsender.DAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import java.sql.SQLException;
import java.util.List; import java.util.List;
public interface MailDAO { public interface MailDAO {
int addMail(MailModel mailModel); int addMail(MailModel mailModel) throws SQLException;
MailModel getMailDetails(Integer mail_id); MailModel getMailDetails(Integer mail_id);

@ -6,6 +6,7 @@ import entgra.mailsender.Service.MailQueueService;
import entgra.mailsender.Service.MailService; import entgra.mailsender.Service.MailService;
import entgra.mailsender.exception.BadRequestException; import entgra.mailsender.exception.BadRequestException;
import entgra.mailsender.exception.MailException; import entgra.mailsender.exception.MailException;
import entgra.mailsender.exception.QueueException;
import entgra.mailsender.util.PriorityQueueHolder; import entgra.mailsender.util.PriorityQueueHolder;
import jakarta.annotation.PreDestroy; import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -13,6 +14,7 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -26,7 +28,6 @@ public class MailServiceImpl implements MailService {
@Autowired @Autowired
private MailQueueService mailQueueService; private MailQueueService mailQueueService;
@PreDestroy @PreDestroy
public void shutdown() { public void shutdown() {
shutdownRequested = true; shutdownRequested = true;
@ -39,9 +40,7 @@ public class MailServiceImpl implements MailService {
@Autowired @Autowired
private MailDAO mailDAO; private MailDAO mailDAO;
public void sendEmail(MailModel emailModel) throws MailException, SQLException {
public void sendEmail(MailModel emailModel) throws MailException {
if (shutdownRequested) { if (shutdownRequested) {
String msg = "Server is shutting down. SMS requests will not be accepted."; String msg = "Server is shutting down. SMS requests will not be accepted.";
logger.warning(msg); logger.warning(msg);
@ -65,7 +64,8 @@ public class MailServiceImpl implements MailService {
try { try {
mailQueueService.enqueMails(mailModels); mailQueueService.enqueMails(mailModels);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new QueueException("Error processing Queue", e);
} }
//send the high priority mail //send the high priority mail
@ -101,7 +101,6 @@ public class MailServiceImpl implements MailService {
} }
} }
public boolean isValidEmailAddress(String email) { public boolean isValidEmailAddress(String email) {
@ -116,7 +115,8 @@ public class MailServiceImpl implements MailService {
mailQueueService.enqueMails(mailModels); mailQueueService.enqueMails(mailModels);
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); logger.info(e.getMessage());
throw new QueueException("Error processing Queue", e);
} }
while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) { while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
@ -158,5 +158,4 @@ public class MailServiceImpl implements MailService {
} }
} }

@ -6,10 +6,11 @@ import entgra.mailsender.exception.MailException;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
public interface MailService { public interface MailService {
void sendEmail(MailModel emailModel) throws MessagingException, IOException, BadRequestException, MailException; void sendEmail(MailModel emailModel) throws MessagingException, IOException, BadRequestException, MailException, SQLException;
boolean isValidEmailAddress(String email); boolean isValidEmailAddress(String email);

@ -0,0 +1,11 @@
package entgra.mailsender.exception;
public class DatabaseAccessException extends RuntimeException{
public DatabaseAccessException(String message) {
super(message);
}
public DatabaseAccessException(String message, Throwable cause) {
super(message, cause);
}
}

@ -0,0 +1,11 @@
package entgra.mailsender.exception;
public class FileConversionException extends RuntimeException{
public FileConversionException(String message) {
super(message);
}
public FileConversionException(String message, Throwable cause) {
super(message, cause);
}
}

@ -0,0 +1,11 @@
package entgra.mailsender.exception;
public class MailProcessingException extends RuntimeException{
public MailProcessingException(String msg){
super(msg);
}
public MailProcessingException(String msg, Throwable cause){
super(msg,cause);
}
}

@ -0,0 +1,11 @@
package entgra.mailsender.exception;
public class QueueException extends RuntimeException{
public QueueException(String message){
super(message);
}
public QueueException(String message, Throwable cause) {
super(message, cause);
}
}
Loading…
Cancel
Save