diff --git a/src/main/java/com/commafeed/backend/StartupBean.java b/src/main/java/com/commafeed/backend/StartupBean.java index f8f4066f..a1d9f922 100644 --- a/src/main/java/com/commafeed/backend/StartupBean.java +++ b/src/main/java/com/commafeed/backend/StartupBean.java @@ -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)); diff --git a/src/main/java/com/commafeed/backend/dao/ApplicationSettingsDAO.java b/src/main/java/com/commafeed/backend/dao/ApplicationSettingsDAO.java index 5fa5a967..b0ed013c 100644 --- a/src/main/java/com/commafeed/backend/dao/ApplicationSettingsDAO.java +++ b/src/main/java/com/commafeed/backend/dao/ApplicationSettingsDAO.java @@ -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 { - @PersistenceContext - protected EntityManager em; - - public void save(ApplicationSettings settings) { - if (settings.getId() == null) { - em.persist(settings); - } else { - em.merge(settings); - } - } - - public ApplicationSettings get() { - EasyCriteria criteria = EasyCriteriaFactory - .createQueryCriteria(em, ApplicationSettings.class); - List list = criteria.getResultList(); - return Iterables.getFirst(list, null); - } } diff --git a/src/main/java/com/commafeed/backend/model/ApplicationSettings.java b/src/main/java/com/commafeed/backend/model/ApplicationSettings.java index 8c7af53b..c1bef176 100644 --- a/src/main/java/com/commafeed/backend/model/ApplicationSettings.java +++ b/src/main/java/com/commafeed/backend/model/ApplicationSettings.java @@ -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; + } + } diff --git a/src/main/java/com/commafeed/backend/services/ApplicationSettingsService.java b/src/main/java/com/commafeed/backend/services/ApplicationSettingsService.java new file mode 100644 index 00000000..cb1be5ba --- /dev/null +++ b/src/main/java/com/commafeed/backend/services/ApplicationSettingsService.java @@ -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); + } + +} diff --git a/src/main/java/com/commafeed/backend/services/MailService.java b/src/main/java/com/commafeed/backend/services/MailService.java new file mode 100644 index 00000000..7fc8c151 --- /dev/null +++ b/src/main/java/com/commafeed/backend/services/MailService.java @@ -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); + + } +} diff --git a/src/main/java/com/commafeed/frontend/pages/GoogleImportCallbackPage.java b/src/main/java/com/commafeed/frontend/pages/GoogleImportCallbackPage.java index d37cc9ce..d6db2c4d 100644 --- a/src/main/java/com/commafeed/frontend/pages/GoogleImportCallbackPage.java +++ b/src/main/java/com/commafeed/frontend/pages/GoogleImportCallbackPage.java @@ -10,10 +10,10 @@ import org.apache.wicket.request.UrlRenderer; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.mapper.parameter.PageParameters; -import com.commafeed.backend.dao.ApplicationSettingsDAO; import com.commafeed.backend.dao.UserDAO; import com.commafeed.backend.feeds.OPMLImporter; import com.commafeed.backend.model.ApplicationSettings; +import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.frontend.utils.WicketUtils; import com.commafeed.frontend.utils.exception.DisplayException; import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; @@ -33,7 +33,7 @@ public class GoogleImportCallbackPage extends WebPage { private static final String EXPORT_URL = "https://www.google.com/reader/subscriptions/export"; @Inject - ApplicationSettingsDAO applicationSettingsDAO; + ApplicationSettingsService applicationSettingsService; @Inject OPMLImporter importer; @@ -65,7 +65,7 @@ public class GoogleImportCallbackPage extends WebPage { } else if (code == null) { throw new DisplayException("Missing authorization code"); } else { - ApplicationSettings settings = applicationSettingsDAO.get(); + ApplicationSettings settings = applicationSettingsService.get(); String redirectUri = getCallbackUrl(); String clientId = settings.getGoogleClientId(); String clientSecret = settings.getGoogleClientSecret(); @@ -90,8 +90,7 @@ public class GoogleImportCallbackPage extends WebPage { httpRequest, accessToken); String opml = httpRequest.execute().parseAsString(); String state = responseUrl.getState(); - importer.importOpml(userDAO.findById(Long.valueOf(state)), - opml); + importer.importOpml(userDAO.findById(Long.valueOf(state)), opml); } catch (Exception e) { throw new DisplayException(e); } diff --git a/src/main/java/com/commafeed/frontend/pages/GoogleImportRedirectPage.java b/src/main/java/com/commafeed/frontend/pages/GoogleImportRedirectPage.java index 74ec8c7c..aa17082f 100644 --- a/src/main/java/com/commafeed/frontend/pages/GoogleImportRedirectPage.java +++ b/src/main/java/com/commafeed/frontend/pages/GoogleImportRedirectPage.java @@ -9,8 +9,8 @@ import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.request.flow.RedirectToUrlException; import org.jboss.logging.Logger; -import com.commafeed.backend.dao.ApplicationSettingsDAO; import com.commafeed.backend.model.ApplicationSettings; +import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.frontend.CommaFeedSession; @SuppressWarnings("serial") @@ -23,11 +23,11 @@ public class GoogleImportRedirectPage extends WebPage { private static final String AUTH_URL = "https://accounts.google.com/o/oauth2/auth"; @Inject - ApplicationSettingsDAO applicationSettingsDAO; + ApplicationSettingsService applicationSettingsService; public GoogleImportRedirectPage() { - ApplicationSettings settings = applicationSettingsDAO.get(); + ApplicationSettings settings = applicationSettingsService.get(); String clientId = settings.getGoogleClientId(); diff --git a/src/main/java/com/commafeed/frontend/pages/WelcomePage.java b/src/main/java/com/commafeed/frontend/pages/WelcomePage.java index ff933dde..90ae6b05 100644 --- a/src/main/java/com/commafeed/frontend/pages/WelcomePage.java +++ b/src/main/java/com/commafeed/frontend/pages/WelcomePage.java @@ -5,7 +5,7 @@ import javax.inject.Inject; import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.html.link.BookmarkablePageLink; -import com.commafeed.backend.dao.ApplicationSettingsDAO; +import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.frontend.pages.components.LoginPanel; import com.commafeed.frontend.pages.components.RegisterPanel; import com.commafeed.frontend.utils.WicketUtils; @@ -14,7 +14,7 @@ import com.commafeed.frontend.utils.WicketUtils; public class WelcomePage extends BasePage { @Inject - ApplicationSettingsDAO applicationSettingsDAO; + ApplicationSettingsService applicationSettingsService; public WelcomePage() { add(new BookmarkablePageLink("logo-link", getApplication() @@ -24,7 +24,7 @@ public class WelcomePage extends BasePage { @Override protected void onConfigure() { super.onConfigure(); - setVisibilityAllowed(applicationSettingsDAO.get() + setVisibilityAllowed(applicationSettingsService.get() .isAllowRegistrations()); } }); diff --git a/src/main/java/com/commafeed/frontend/pages/components/RegisterPanel.java b/src/main/java/com/commafeed/frontend/pages/components/RegisterPanel.java index 17a3fc67..503def7a 100644 --- a/src/main/java/com/commafeed/frontend/pages/components/RegisterPanel.java +++ b/src/main/java/com/commafeed/frontend/pages/components/RegisterPanel.java @@ -20,10 +20,10 @@ import org.apache.wicket.validation.IValidator; import org.apache.wicket.validation.ValidationError; import org.apache.wicket.validation.validator.StringValidator; -import com.commafeed.backend.dao.ApplicationSettingsDAO; import com.commafeed.backend.dao.UserDAO; 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; import com.commafeed.frontend.CommaFeedSession; import com.commafeed.frontend.model.RegistrationRequest; @@ -40,7 +40,7 @@ public class RegisterPanel extends Panel { UserService userService; @Inject - ApplicationSettingsDAO applicationSettingsDAO; + ApplicationSettingsService applicationSettingsService; public RegisterPanel(String markupId) { super(markupId); @@ -51,7 +51,7 @@ public class RegisterPanel extends Panel { "form", model) { @Override protected void onSubmit() { - if (applicationSettingsDAO.get().isAllowRegistrations()) { + if (applicationSettingsService.get().isAllowRegistrations()) { RegistrationRequest req = getModelObject(); userService.register(req.getName(), req.getPassword(), Arrays.asList(Role.USER)); diff --git a/src/main/java/com/commafeed/frontend/rest/resources/AdminSettingsREST.java b/src/main/java/com/commafeed/frontend/rest/resources/AdminSettingsREST.java index e90e8513..ec6312ee 100644 --- a/src/main/java/com/commafeed/frontend/rest/resources/AdminSettingsREST.java +++ b/src/main/java/com/commafeed/frontend/rest/resources/AdminSettingsREST.java @@ -6,9 +6,9 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Response; -import com.commafeed.backend.dao.ApplicationSettingsDAO; import com.commafeed.backend.model.ApplicationSettings; import com.commafeed.backend.model.UserRole.Role; +import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.frontend.SecurityCheck; @SecurityCheck(Role.ADMIN) @@ -16,18 +16,18 @@ import com.commafeed.frontend.SecurityCheck; public class AdminSettingsREST { @Inject - ApplicationSettingsDAO applicationSettingsDAO; + ApplicationSettingsService applicationSettingsService; @Path("get") @GET public ApplicationSettings get() { - return applicationSettingsDAO.get(); + return applicationSettingsService.get(); } @Path("save") @POST public Response save(ApplicationSettings settings) { - applicationSettingsDAO.save(settings); + applicationSettingsService.save(settings); return Response.ok().build(); } } diff --git a/src/main/java/com/commafeed/frontend/rest/resources/EntriesREST.java b/src/main/java/com/commafeed/frontend/rest/resources/EntriesREST.java index e30b927b..91cd674a 100644 --- a/src/main/java/com/commafeed/frontend/rest/resources/EntriesREST.java +++ b/src/main/java/com/commafeed/frontend/rest/resources/EntriesREST.java @@ -74,15 +74,15 @@ public class EntriesREST extends AbstractREST { if (ALL.equals(id)) { entries.setName("All"); List unreadEntries = feedEntryStatusDAO - .findAll(getUser(), unreadOnly, offset, limit, - order, true); + .findAll(getUser(), unreadOnly, offset, limit, order, + true); for (FeedEntryStatus status : unreadEntries) { entries.getEntries().add(buildEntry(status)); } } else { - FeedCategory feedCategory = feedCategoryDAO.findById( - getUser(), Long.valueOf(id)); + FeedCategory feedCategory = feedCategoryDAO.findById(getUser(), + Long.valueOf(id)); if (feedCategory != null) { List childrenCategories = feedCategoryDAO .findAllChildrenCategories(getUser(), feedCategory); @@ -142,8 +142,8 @@ public class EntriesREST extends AbstractREST { feedEntryStatusDAO.update(status); } else if (type == Type.feed) { if (read) { - FeedSubscription subscription = feedSubscriptionDAO - .findById(getUser(), Long.valueOf(id)); + FeedSubscription subscription = feedSubscriptionDAO.findById( + getUser(), Long.valueOf(id)); feedEntryStatusDAO.markFeedEntries(getUser(), subscription.getFeed(), olderThan); } else { @@ -156,7 +156,8 @@ public class EntriesREST extends AbstractREST { feedEntryStatusDAO.markAllEntries(getUser(), olderThan); } else { List categories = feedCategoryDAO - .findAllChildrenCategories(getUser(), + .findAllChildrenCategories( + getUser(), feedCategoryDAO.findById(getUser(), Long.valueOf(id))); feedEntryStatusDAO.markCategoryEntries(getUser(),