mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
added mailing class
This commit is contained in:
@@ -11,7 +11,6 @@ import javax.inject.Inject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.dao.ApplicationSettingsDAO;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
@@ -23,6 +22,7 @@ import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||
import com.commafeed.backend.services.UserService;
|
||||
|
||||
@Startup
|
||||
@@ -48,7 +48,7 @@ public class StartupBean {
|
||||
UserService userService;
|
||||
|
||||
@Inject
|
||||
ApplicationSettingsDAO applicationSettingsDAO;
|
||||
ApplicationSettingsService applicationSettingsService;
|
||||
|
||||
@Inject
|
||||
FeedRefreshWorker worker;
|
||||
@@ -62,7 +62,7 @@ public class StartupBean {
|
||||
initialData();
|
||||
}
|
||||
|
||||
ApplicationSettings settings = applicationSettingsDAO.get();
|
||||
ApplicationSettings settings = applicationSettingsService.get();
|
||||
for (int i = 0; i < settings.getBackgroundThreads(); i++) {
|
||||
worker.start();
|
||||
}
|
||||
@@ -72,7 +72,7 @@ public class StartupBean {
|
||||
private void initialData() {
|
||||
log.info("Populating database with default values");
|
||||
|
||||
applicationSettingsDAO.save(new ApplicationSettings());
|
||||
applicationSettingsService.save(new ApplicationSettings());
|
||||
|
||||
User user = userService.register(ADMIN_NAME, "admin",
|
||||
Arrays.asList(Role.ADMIN, Role.USER));
|
||||
|
||||
@@ -1,34 +1,11 @@
|
||||
package com.commafeed.backend.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||
import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Stateless
|
||||
public class ApplicationSettingsDAO {
|
||||
public class ApplicationSettingsDAO extends GenericDAO<ApplicationSettings> {
|
||||
|
||||
@PersistenceContext
|
||||
protected EntityManager em;
|
||||
|
||||
public void save(ApplicationSettings settings) {
|
||||
if (settings.getId() == null) {
|
||||
em.persist(settings);
|
||||
} else {
|
||||
em.merge(settings);
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationSettings get() {
|
||||
EasyCriteria<ApplicationSettings> criteria = EasyCriteriaFactory
|
||||
.createQueryCriteria(em, ApplicationSettings.class);
|
||||
List<ApplicationSettings> list = criteria.getResultList();
|
||||
return Iterables.getFirst(list, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ public class ApplicationSettings extends AbstractModel {
|
||||
private String googleClientSecret;
|
||||
private int backgroundThreads = 3;
|
||||
|
||||
private String smtpHost;
|
||||
private int smtpPort;
|
||||
private boolean smtpTls;
|
||||
private String smtpUserName;
|
||||
private String smtpPassword;
|
||||
|
||||
public String getPublicUrl() {
|
||||
return publicUrl;
|
||||
}
|
||||
@@ -54,4 +60,44 @@ public class ApplicationSettings extends AbstractModel {
|
||||
this.backgroundThreads = backgroundThreads;
|
||||
}
|
||||
|
||||
public String getSmtpHost() {
|
||||
return smtpHost;
|
||||
}
|
||||
|
||||
public void setSmtpHost(String smtpHost) {
|
||||
this.smtpHost = smtpHost;
|
||||
}
|
||||
|
||||
public int getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public void setSmtpPort(int smtpPort) {
|
||||
this.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
public boolean isSmtpTls() {
|
||||
return smtpTls;
|
||||
}
|
||||
|
||||
public void setSmtpTls(boolean smtpTls) {
|
||||
this.smtpTls = smtpTls;
|
||||
}
|
||||
|
||||
public String getSmtpUserName() {
|
||||
return smtpUserName;
|
||||
}
|
||||
|
||||
public void setSmtpUserName(String smtpUserName) {
|
||||
this.smtpUserName = smtpUserName;
|
||||
}
|
||||
|
||||
public String getSmtpPassword() {
|
||||
return smtpPassword;
|
||||
}
|
||||
|
||||
public void setSmtpPassword(String smtpPassword) {
|
||||
this.smtpPassword = smtpPassword;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.commafeed.backend.dao.ApplicationSettingsDAO;
|
||||
import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
@Stateless
|
||||
public class ApplicationSettingsService {
|
||||
|
||||
@Inject
|
||||
ApplicationSettingsDAO applicationSettingsDAO;
|
||||
|
||||
public void save(ApplicationSettings settings) {
|
||||
applicationSettingsDAO.saveOrUpdate(settings);
|
||||
}
|
||||
|
||||
public ApplicationSettings get() {
|
||||
return Iterables.getFirst(applicationSettingsDAO.findAll(), null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.commafeed.backend.model.User;
|
||||
|
||||
public class MailService {
|
||||
|
||||
@Inject
|
||||
ApplicationSettingsService applicationSettingsService;
|
||||
|
||||
public void sendMail(User user, String subject, String content) throws Exception {
|
||||
|
||||
ApplicationSettings settings = applicationSettingsService.get();
|
||||
|
||||
final String username = settings.getSmtpUserName();
|
||||
final String password = settings.getSmtpPassword();
|
||||
|
||||
String dest = user.getEmail();
|
||||
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", settings.isSmtpTls());
|
||||
props.put("mail.smtp.host", settings.getSmtpHost());
|
||||
props.put("mail.smtp.port", settings.getSmtpPort());
|
||||
|
||||
Session session = Session.getInstance(props,
|
||||
new javax.mail.Authenticator() {
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(username, "CommaFeed"));
|
||||
message.setRecipients(Message.RecipientType.TO,
|
||||
InternetAddress.parse(dest));
|
||||
message.setSubject("CommaFeed - " + subject);
|
||||
message.setText(content);
|
||||
|
||||
Transport.send(message);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user