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
|
//DEPRECATED
|
||||||
private static final String template = "Hello %s! This is a test mail from the Fachschaft WIAI.";
|
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
|
private final AtomicLong counter = new AtomicLong(); //from the tutorial... deprc
|
||||||
|
|
||||||
@GetMapping("/mail")
|
@GetMapping("/mail")
|
||||||
@ -32,31 +32,36 @@ public class MailController {
|
|||||||
@RequestParam(value = "body", defaultValue = "") String body,
|
@RequestParam(value = "body", defaultValue = "") String body,
|
||||||
@RequestParam(value = "mailConfig", defaultValue = "") String mailConfig)
|
@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);
|
Email mail = new Email(counter.incrementAndGet(), toMail, fromMail, body, subject);
|
||||||
// TODO add all the exam file stuff via another service, i.e. WatermarkingService
|
// TODO add all the exam file stuff via another service, i.e. WatermarkingService
|
||||||
// ...
|
// ...
|
||||||
// TODO Mail Validate
|
// TODO Improve Mail Validate
|
||||||
if(mail.validateFields()){
|
if(!mail.validateFields()){
|
||||||
|
System.err.println("Mail is not valid.");
|
||||||
|
return new Email(-1, "", "", "", "");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO load the mail config file dep. on the given name in mailConfig and load that config.
|
// TODO load the mail config file dep. on the given name in mailConfig and load that config.
|
||||||
MailService mailService = new MailService();
|
MailService mailService = new MailService();
|
||||||
//For now...
|
|
||||||
mailService.loadDevelopmentDefault();
|
|
||||||
//mailService.loadFromConfig();
|
|
||||||
|
|
||||||
|
// 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
|
// Send the mail via the mailService
|
||||||
boolean sendResult = mailService.sendMail(mail);
|
boolean sendResult = mailService.sendMail(mail);
|
||||||
|
|
||||||
//TODO Properly handle return value from sendMail
|
//TODO Properly handle return value from sendMail
|
||||||
if(sendResult) {
|
if(sendResult) {
|
||||||
System.out.println("Mail was sent successfully.");
|
System.out.println("Mail was sent successfully.");
|
||||||
}
|
}
|
||||||
return mail;
|
return mail;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.err.println("Unable to successfully load the mailer configuration.");
|
||||||
|
return new Email(-1, "", "", "", "");
|
||||||
|
}
|
||||||
|
|
||||||
} catch (javax.mail.internet.AddressException jMailException) {
|
} catch (javax.mail.internet.AddressException jMailException) {
|
||||||
System.err.println("Address Exception whilst sending the mail");
|
System.err.println("Address Exception whilst sending the mail");
|
||||||
jMailException.printStackTrace();
|
jMailException.printStackTrace();
|
||||||
@ -66,9 +71,6 @@ public class MailController {
|
|||||||
jMailException.printStackTrace();
|
jMailException.printStackTrace();
|
||||||
return new Email(-1, "", "", "", "");
|
return new Email(-1, "", "", "", "");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
System.err.println("Mail is not valid.");
|
|
||||||
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;
|
package de.fswiai.klausurenmodul.service;
|
||||||
|
|
||||||
import de.fswiai.klausurenmodul.model.Email;
|
import de.fswiai.klausurenmodul.model.Email;
|
||||||
|
import de.fswiai.klausurenmodul.model.MailerConfig;
|
||||||
|
|
||||||
import javax.mail.*;
|
import javax.mail.*;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
@ -9,41 +10,35 @@ import javax.mail.internet.MimeMessage;
|
|||||||
import javax.mail.internet.MimeMultipart;
|
import javax.mail.internet.MimeMultipart;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class MailService {
|
public class MailService {
|
||||||
private String username = "";
|
private MailerConfig mailerConfig;
|
||||||
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 MailService() {
|
public MailService() {
|
||||||
|
mailerConfig = new MailerConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MailService(String username, String password, int smtpPort, String smtpHost, String tlsVersion, boolean useSTARTTLS) {
|
public MailService(String username, String password, int smtpPort, String smtpHost, String tlsVersion, boolean useSTARTTLS) {
|
||||||
this.username = username;
|
this.mailerConfig = new MailerConfig("customConfig",username, password, smtpPort, smtpHost, tlsVersion, useSTARTTLS);
|
||||||
this.password = password;
|
|
||||||
this.smtpPort = smtpPort;
|
|
||||||
this.smtpHost = smtpHost;
|
|
||||||
this.tlsVersion = tlsVersion;
|
|
||||||
this.useSTARTTLS = useSTARTTLS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadFromConfig(String configJSON){
|
public boolean loadFromConfig(String configJSONFilePath){
|
||||||
//TODO First Idea for how we handle mail accounts/configurations.
|
//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.
|
// -> Build some parser for a JSON schema (or something else, idc) that will set username, password and so on.
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadDevelopmentDefault(){
|
|
||||||
this.username = "ex311199c";
|
|
||||||
this.password = "";
|
|
||||||
this.smtpPort = 587;
|
|
||||||
this.smtpHost = "exhub.uni-bamberg.de";
|
|
||||||
this.tlsVersion = "TLSv1.2";
|
|
||||||
this.useSTARTTLS = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO switch boolean to some result class
|
//TODO switch boolean to some result class
|
||||||
@ -116,66 +111,19 @@ public class MailService {
|
|||||||
private Session getNewSession(){
|
private Session getNewSession(){
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
prop.put("mail.smtp.auth", true);
|
prop.put("mail.smtp.auth", true);
|
||||||
prop.put("mail.smtp.starttls.enable", this.useSTARTTLS ? "true" : "false");
|
prop.put("mail.smtp.starttls.enable", mailerConfig.getUseSTARTTLS() ? "true" : "false");
|
||||||
prop.put("mail.smtp.starttls.required", this.useSTARTTLS ? "true" : "false");
|
prop.put("mail.smtp.starttls.required", mailerConfig.getUseSTARTTLS() ? "true" : "false");
|
||||||
prop.put("mail.smtp.host", this.smtpHost);
|
prop.put("mail.smtp.host", mailerConfig.getSmtpHost());
|
||||||
prop.put("mail.smtp.port", this.smtpPort);
|
prop.put("mail.smtp.port", mailerConfig.getSmtpPort());
|
||||||
prop.put("mail.smtp.ssl.protocols", this.tlsVersion);
|
prop.put("mail.smtp.ssl.protocols", mailerConfig.getTlsVersion());
|
||||||
prop.put("mail.smtp.ssl.trust", this.smtpHost);
|
prop.put("mail.smtp.ssl.trust", mailerConfig.getSmtpHost());
|
||||||
Session session = Session.getInstance(prop, new Authenticator() {
|
Session session = Session.getInstance(prop, new Authenticator() {
|
||||||
@Override
|
@Override
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
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;
|
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