Added JSON Config for MailerConfig loading
This commit is contained in:
parent
6fb7824d8a
commit
2b49181052
9
MailClientConfig/defaultMailConfig.json
Normal file
9
MailClientConfig/defaultMailConfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"configname":"default",
|
||||
"username":"",
|
||||
"password":"",
|
||||
"smtpPort":0,
|
||||
"smtpHost":"",
|
||||
"tlsVersion":"TLSv1.2",
|
||||
"useSTARTTLS":true
|
||||
}
|
||||
@ -14,7 +14,7 @@ public class MailController {
|
||||
//DEPRECATED
|
||||
private static final String template = "Hello %s! This is a test mail from the Fachschaft WIAI.";
|
||||
|
||||
//TODO controllers should be stateless. This certainly is not. Solve differently.
|
||||
//TODO controllers should be stateless... idk is this okay?.
|
||||
private final AtomicLong counter = new AtomicLong(); //from the tutorial... deprc
|
||||
|
||||
@GetMapping("/mail")
|
||||
@ -32,43 +32,45 @@ public class MailController {
|
||||
@RequestParam(value = "body", defaultValue = "") String body,
|
||||
@RequestParam(value = "mailConfig", defaultValue = "") String mailConfig)
|
||||
{
|
||||
if(toMail.isEmpty() || fromMail.isEmpty()){
|
||||
System.out.println("Abort Mail sending");
|
||||
return new Email(-1, "", "", "", "");
|
||||
}
|
||||
|
||||
Email mail = new Email(counter.incrementAndGet(), toMail, fromMail, body, subject);
|
||||
// TODO add all the exam file stuff via another service, i.e. WatermarkingService
|
||||
// ...
|
||||
// TODO Mail Validate
|
||||
if(mail.validateFields()){
|
||||
try {
|
||||
// TODO load the mail config file dep. on the given name in mailConfig and load that config.
|
||||
MailService mailService = new MailService();
|
||||
//For now...
|
||||
mailService.loadDevelopmentDefault();
|
||||
//mailService.loadFromConfig();
|
||||
|
||||
// Send the mail via the mailService
|
||||
boolean sendResult = mailService.sendMail(mail);
|
||||
|
||||
//TODO Properly handle return value from sendMail
|
||||
if(sendResult){
|
||||
System.out.println("Mail was sent successfully.");
|
||||
}
|
||||
return mail;
|
||||
} catch (javax.mail.internet.AddressException jMailException) {
|
||||
System.err.println("Address Exception whilst sending the mail");
|
||||
jMailException.printStackTrace();
|
||||
return new Email(-1, "", "", "", "");
|
||||
} catch (javax.mail.MessagingException jMailException) {
|
||||
System.err.println("Messaging Exception whilst sending the mail");
|
||||
jMailException.printStackTrace();
|
||||
return new Email(-1, "", "", "", "");
|
||||
}
|
||||
} else {
|
||||
// TODO Improve Mail Validate
|
||||
if(!mail.validateFields()){
|
||||
System.err.println("Mail is not valid.");
|
||||
return new Email(-1, "", "", "", "");
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO load the mail config file dep. on the given name in mailConfig and load that config.
|
||||
MailService mailService = new MailService();
|
||||
|
||||
// NOTE: FOR OTHER DEVS - CREATE/CONFIGURE THIS FILE ON YOUR LOCAL MACHINE, DONT ADD TO GIT!
|
||||
boolean loadConfigResult = mailService.loadFromConfig("../kModulDevconfig.json");
|
||||
|
||||
if(loadConfigResult) {
|
||||
// Send the mail via the mailService
|
||||
boolean sendResult = mailService.sendMail(mail);
|
||||
//TODO Properly handle return value from sendMail
|
||||
if(sendResult) {
|
||||
System.out.println("Mail was sent successfully.");
|
||||
}
|
||||
return mail;
|
||||
}
|
||||
else {
|
||||
System.err.println("Unable to successfully load the mailer configuration.");
|
||||
return new Email(-1, "", "", "", "");
|
||||
}
|
||||
|
||||
} catch (javax.mail.internet.AddressException jMailException) {
|
||||
System.err.println("Address Exception whilst sending the mail");
|
||||
jMailException.printStackTrace();
|
||||
return new Email(-1, "", "", "", "");
|
||||
} catch (javax.mail.MessagingException jMailException) {
|
||||
System.err.println("Messaging Exception whilst sending the mail");
|
||||
jMailException.printStackTrace();
|
||||
return new Email(-1, "", "", "", "");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,80 @@
|
||||
package de.fswiai.klausurenmodul.model;
|
||||
|
||||
public class MailerConfig {
|
||||
private String configname = "";
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
private int smtpPort = 0;
|
||||
private String smtpHost = "";
|
||||
private String tlsVersion = "TLSv1.2"; //maybe already use 1.3?
|
||||
private boolean useSTARTTLS = true;
|
||||
|
||||
public MailerConfig() {
|
||||
}
|
||||
|
||||
public MailerConfig(String configname, String username, String password, int smtpPort, String smtpHost, String tlsVersion, boolean useSTARTTLS) {
|
||||
this.configname = configname;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.smtpPort = smtpPort;
|
||||
this.smtpHost = smtpHost;
|
||||
this.tlsVersion = tlsVersion;
|
||||
this.useSTARTTLS = useSTARTTLS;
|
||||
}
|
||||
|
||||
public String getConfigname() {
|
||||
return configname;
|
||||
}
|
||||
|
||||
public void setConfigname(String configname) {
|
||||
this.configname = configname;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public void setSmtpPort(int smtpPort) {
|
||||
this.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
public String getSmtpHost() {
|
||||
return smtpHost;
|
||||
}
|
||||
|
||||
public void setSmtpHost(String smtpHost) {
|
||||
this.smtpHost = smtpHost;
|
||||
}
|
||||
|
||||
public String getTlsVersion() {
|
||||
return tlsVersion;
|
||||
}
|
||||
|
||||
public void setTlsVersion(String tlsVersion) {
|
||||
this.tlsVersion = tlsVersion;
|
||||
}
|
||||
|
||||
public boolean getUseSTARTTLS() {
|
||||
return useSTARTTLS;
|
||||
}
|
||||
|
||||
public void setUseSTARTTLS(boolean useSTARTTLS) {
|
||||
this.useSTARTTLS = useSTARTTLS;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package de.fswiai.klausurenmodul.service;
|
||||
|
||||
import de.fswiai.klausurenmodul.model.Email;
|
||||
import de.fswiai.klausurenmodul.model.MailerConfig;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
@ -9,41 +10,35 @@ import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class MailService {
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
private int smtpPort = 0;
|
||||
private String smtpHost = "";
|
||||
private String tlsVersion = "TLSv1.2"; //maybe already use 1.3?
|
||||
private boolean useSTARTTLS = true;
|
||||
private MailerConfig mailerConfig;
|
||||
|
||||
public MailService() {
|
||||
mailerConfig = new MailerConfig();
|
||||
}
|
||||
|
||||
public MailService(String username, String password, int smtpPort, String smtpHost, String tlsVersion, boolean useSTARTTLS) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.smtpPort = smtpPort;
|
||||
this.smtpHost = smtpHost;
|
||||
this.tlsVersion = tlsVersion;
|
||||
this.useSTARTTLS = useSTARTTLS;
|
||||
this.mailerConfig = new MailerConfig("customConfig",username, password, smtpPort, smtpHost, tlsVersion, useSTARTTLS);
|
||||
}
|
||||
|
||||
public void loadFromConfig(String configJSON){
|
||||
public boolean loadFromConfig(String configJSONFilePath){
|
||||
//TODO First Idea for how we handle mail accounts/configurations.
|
||||
// -> Build some parser for a JSON schema (or something else, idc) that will set username, password and so on.
|
||||
}
|
||||
|
||||
public void loadDevelopmentDefault(){
|
||||
this.username = "ex311199c";
|
||||
this.password = "";
|
||||
this.smtpPort = 587;
|
||||
this.smtpHost = "exhub.uni-bamberg.de";
|
||||
this.tlsVersion = "TLSv1.2";
|
||||
this.useSTARTTLS = true;
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
System.out.println("Loading Config from: " + configJSONFilePath);
|
||||
mailerConfig = mapper.readValue(new File(configJSONFilePath),MailerConfig.class);
|
||||
return true;
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
System.err.println("Error whilst loading a config for the Mailer.");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO switch boolean to some result class
|
||||
@ -116,66 +111,19 @@ public class MailService {
|
||||
private Session getNewSession(){
|
||||
Properties prop = new Properties();
|
||||
prop.put("mail.smtp.auth", true);
|
||||
prop.put("mail.smtp.starttls.enable", this.useSTARTTLS ? "true" : "false");
|
||||
prop.put("mail.smtp.starttls.required", this.useSTARTTLS ? "true" : "false");
|
||||
prop.put("mail.smtp.host", this.smtpHost);
|
||||
prop.put("mail.smtp.port", this.smtpPort);
|
||||
prop.put("mail.smtp.ssl.protocols", this.tlsVersion);
|
||||
prop.put("mail.smtp.ssl.trust", this.smtpHost);
|
||||
prop.put("mail.smtp.starttls.enable", mailerConfig.getUseSTARTTLS() ? "true" : "false");
|
||||
prop.put("mail.smtp.starttls.required", mailerConfig.getUseSTARTTLS() ? "true" : "false");
|
||||
prop.put("mail.smtp.host", mailerConfig.getSmtpHost());
|
||||
prop.put("mail.smtp.port", mailerConfig.getSmtpPort());
|
||||
prop.put("mail.smtp.ssl.protocols", mailerConfig.getTlsVersion());
|
||||
prop.put("mail.smtp.ssl.trust", mailerConfig.getSmtpHost());
|
||||
Session session = Session.getInstance(prop, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(MailService.this.username, MailService.this.password);
|
||||
return new PasswordAuthentication(MailService.this.mailerConfig.getUsername(), MailService.this.mailerConfig.getPassword());
|
||||
}
|
||||
});
|
||||
return session;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public void setSmtpPort(int smtpPort) {
|
||||
this.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
public String getSmtpHost() {
|
||||
return smtpHost;
|
||||
}
|
||||
|
||||
public void setSmtpHost(String smtpHost) {
|
||||
this.smtpHost = smtpHost;
|
||||
}
|
||||
|
||||
public String getTlsVersion() {
|
||||
return tlsVersion;
|
||||
}
|
||||
|
||||
public void setTlsVersion(String tlsVersion) {
|
||||
this.tlsVersion = tlsVersion;
|
||||
}
|
||||
|
||||
public boolean isUseSTARTTLS() {
|
||||
return useSTARTTLS;
|
||||
}
|
||||
|
||||
public void setUseSTARTTLS(boolean useSTARTTLS) {
|
||||
this.useSTARTTLS = useSTARTTLS;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user