=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/Email.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/Email.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/Email.java 2014-12-21 17:57:33 +0000 @@ -0,0 +1,140 @@ +package org.hisp.dhis.email; + +/* + * Copyright (c) 2004-2014, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.util.Set; + +import org.hisp.dhis.common.BaseIdentifiableObject; +import org.hisp.dhis.common.DxfNamespaces; +import org.hisp.dhis.common.view.DetailedView; +import org.hisp.dhis.common.view.ExportView; +import org.hisp.dhis.user.User; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonView; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +/** + * @author Lars Helge Overland + */ +@JacksonXmlRootElement( localName = "email", namespace = DxfNamespaces.DXF_2_0 ) +public class Email +{ + private String subject; + + private String text; + + private User sender; + + private Set recipients; + + // ------------------------------------------------------------------------- + // Contructors + // ------------------------------------------------------------------------- + + public Email() + { + } + + public Email( String subject, String text ) + { + this.subject = subject; + this.text = text; + } + + public Email( String subject, String text, User sender, Set recipients ) + { + this.subject = subject; + this.text = text; + this.sender = sender; + this.recipients = recipients; + } + + // ------------------------------------------------------------------------- + // Getters and setters + // ------------------------------------------------------------------------- + + @JsonProperty + @JsonView( { DetailedView.class, ExportView.class } ) + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public String getSubject() + { + return subject; + } + + public void setSubject( String subject ) + { + this.subject = subject; + } + + @JsonProperty + @JsonView( { DetailedView.class, ExportView.class } ) + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public String getText() + { + return text; + } + + public void setText( String text ) + { + this.text = text; + } + + @JsonProperty + @JsonSerialize( as = BaseIdentifiableObject.class ) + @JsonView( { DetailedView.class, ExportView.class } ) + @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 ) + public User getSender() + { + return sender; + } + + public void setSender( User sender ) + { + this.sender = sender; + } + + @JsonProperty + @JsonSerialize( contentAs = BaseIdentifiableObject.class ) + @JsonView( { DetailedView.class, ExportView.class } ) + @JacksonXmlElementWrapper( localName = "recipients", namespace = DxfNamespaces.DXF_2_0 ) + @JacksonXmlProperty( localName = "recipient", namespace = DxfNamespaces.DXF_2_0 ) + public Set getRecipients() + { + return recipients; + } + + public void setRecipients( Set recipients ) + { + this.recipients = recipients; + } +} === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/EmailService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/EmailService.java 2014-06-16 14:31:54 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/email/EmailService.java 2014-12-21 17:57:33 +0000 @@ -28,10 +28,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import org.hisp.dhis.user.User; - -import java.util.Set; - /** * @author Halvdan Hoem Grelland */ @@ -46,28 +42,24 @@ /** * Sends an email to the recipient user from the sender. * - * @param subject the subject text of the email. - * @param text the text (body) of the email. - * @param sender the sender of the email. - * @param recipient the recipient of the email. - * @param forceSend if true the email is sent regardless of the recipients' email notification settings. - */ - void sendEmail( String subject, String text, User sender, User recipient, boolean forceSend ); - - /** - * Sends an email to multiple recipients from the sender. - * - * @param subject the subject text of the email. - * @param text the text (body) of the email. - * @param sender the sender of the email. - * @param recipients the recipients of the email. - * @param forceSend if true the email is sent regardless of the email notification settings of the recipients. - */ - void sendEmail( String subject, String text, User sender, Set recipients, boolean forceSend); + * @param email the email to send. + */ + void sendEmail( Email email ); /** * Sends an automatically generated email message to the current user. * Useful for testing the SMTP configuration of the system. */ - void sendTestEmail( ); + void sendTestEmail(); + + /** + * Sends an email using the system notification email as recipient. Requires + * that a valid system notification email address has been specified. Only + * the subject and text properties of the given email are read. + * + * @param subject the subject text of the email. + * @param text the text (body) of the email. + * @return true if an email was sent, false if not. + */ + boolean sendSystemEmail( Email email ); } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java 2014-11-25 12:35:16 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java 2014-12-21 17:57:33 +0000 @@ -184,6 +184,8 @@ boolean selfRegistrationNoRecaptcha(); boolean emailEnabled(); + + boolean systemNotificationEmailValid(); String googleAnalyticsUA(); === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/email/DefaultEmailService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/email/DefaultEmailService.java 2014-08-27 09:49:23 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/email/DefaultEmailService.java 2014-12-21 17:57:33 +0000 @@ -28,16 +28,17 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.HashSet; -import java.util.Set; - import org.apache.commons.lang.StringUtils; import org.hisp.dhis.message.MessageSender; import org.hisp.dhis.setting.SystemSettingManager; +import org.hisp.dhis.system.util.ValidationUtils; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.user.User; +import org.hisp.dhis.user.UserCredentials; import org.springframework.transaction.annotation.Transactional; +import com.google.common.collect.Sets; + /** * @author Halvdan Hoem Grelland */ @@ -85,26 +86,44 @@ } @Override - public void sendEmail( String subject, String text, User sender, User recipient, boolean forceSend ) - { - Set recipients = new HashSet<>(); - recipients.add( recipient ); - - emailMessageSender.sendMessage( subject, text, sender, new HashSet<>( recipients ), forceSend ); - } - - @Override - public void sendEmail( String subject, String text, User sender, Set recipients, boolean forceSend ) - { - emailMessageSender.sendMessage( subject, text, sender, new HashSet<>( recipients ), forceSend ); - } - - @Override - public void sendTestEmail( ) + public void sendEmail( Email email ) + { + emailMessageSender.sendMessage( email.getSubject(), email.getText(), email.getSender(), email.getRecipients(), true ); + } + + @Override + public void sendTestEmail() { String instanceName = StringUtils.defaultIfBlank( (String) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_APPLICATION_TITLE ), TEST_DEFAULT_SENDER ); - sendEmail( TEST_EMAIL_SUBJECT, TEST_EMAIL_TEXT + instanceName, null, currentUserService.getCurrentUser(), true ); + Email email = new Email( TEST_EMAIL_SUBJECT, TEST_EMAIL_TEXT + instanceName, null, Sets.newHashSet( currentUserService.getCurrentUser() ) ); + + sendEmail( email ); + } + + @Override + public boolean sendSystemEmail( Email email ) + { + String recipient = (String) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_SYSTEM_NOTIFICATIONS_EMAIL ); + String appTitle = (String) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_APPLICATION_TITLE ); + + if ( recipient == null || !ValidationUtils.emailIsValid( recipient ) ) + { + return false; + } + + User user = new User(); + UserCredentials credentials = new UserCredentials(); + credentials.setUsername( recipient ); + user.setEmail( recipient ); + + User sender = new User(); + sender.setFirstName( StringUtils.trimToEmpty( appTitle ) ); + sender.setSurname( recipient ); + + emailMessageSender.sendMessage( email.getSubject(), email.getText(), sender, Sets.newHashSet( user ), true ); + + return true; } } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java 2014-11-25 20:22:50 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java 2014-12-21 17:57:33 +0000 @@ -33,24 +33,20 @@ import java.util.List; import java.util.Set; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hisp.dhis.configuration.ConfigurationService; import org.hisp.dhis.dataset.CompleteDataSetRegistration; import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.setting.SystemSettingManager; -import org.hisp.dhis.system.util.ValidationUtils; +import org.hisp.dhis.email.Email; +import org.hisp.dhis.email.EmailService; import org.hisp.dhis.system.velocity.VelocityManager; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.user.User; -import org.hisp.dhis.user.UserCredentials; import org.hisp.dhis.user.UserGroup; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; -import com.google.common.collect.Sets; - /** * @author Lars Helge Overland */ @@ -89,20 +85,13 @@ this.configurationService = configurationService; } - private SystemSettingManager systemSettingManager; - - public void setSystemSettingManager( SystemSettingManager systemSettingManager ) - { - this.systemSettingManager = systemSettingManager; - } - - private MessageSender emailMessageSender; - - public void setEmailMessageSender( MessageSender emailMessageSender ) - { - this.emailMessageSender = emailMessageSender; - } + private EmailService emailService; + public void setEmailService( EmailService emailService ) + { + this.emailService = emailService; + } + private List messageSenders; @Autowired @@ -190,31 +179,8 @@ @Override public int sendSystemNotification( String subject, String text ) { - String email = (String) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_SYSTEM_NOTIFICATIONS_EMAIL ); - String appTitle = (String) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_APPLICATION_TITLE ); - - // --------------------------------------------------------------------- - // Send email to system notification email address - // --------------------------------------------------------------------- - - if ( email != null && ValidationUtils.emailIsValid( email ) ) - { - User user = new User(); - UserCredentials credentials = new UserCredentials(); - credentials.setUsername( email ); - user.setEmail( email ); - - User sender = new User(); - sender.setFirstName( StringUtils.trimToEmpty( appTitle ) ); - sender.setSurname( email ); - - emailMessageSender.sendMessage( subject, text, sender, Sets.newHashSet( user ), true ); - } - - // --------------------------------------------------------------------- - // Send feedback - // --------------------------------------------------------------------- - + emailService.sendSystemEmail( new Email( subject, text ) ); + return sendFeedback( subject, text, null ); } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java 2014-10-16 06:17:19 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java 2014-12-21 17:57:33 +0000 @@ -29,6 +29,7 @@ */ import org.apache.commons.lang.StringUtils; +import org.hisp.dhis.system.util.ValidationUtils; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; @@ -198,6 +199,14 @@ } @Override + public boolean systemNotificationEmailValid() + { + String address = (String) getSystemSetting( KEY_SYSTEM_NOTIFICATIONS_EMAIL ); + + return address != null && ValidationUtils.emailIsValid( address ); + } + + @Override public String googleAnalyticsUA() { return StringUtils.trimToNull( (String) getSystemSetting( KEY_GOOGLE_ANALYTICS_UA ) ); === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2014-12-19 15:42:38 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2014-12-21 17:57:33 +0000 @@ -629,8 +629,7 @@ - - + === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/EmailController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/EmailController.java 2014-11-11 12:51:06 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/EmailController.java 2014-12-21 17:57:33 +0000 @@ -28,17 +28,19 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import javax.servlet.http.HttpServletResponse; + +import org.hisp.dhis.email.Email; import org.hisp.dhis.email.EmailService; +import org.hisp.dhis.setting.SystemSettingManager; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.webapi.utils.ContextUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - /** * @author Halvdan Hoem Grelland */ @@ -57,27 +59,54 @@ @Autowired private CurrentUserService currentUserService; + + @Autowired + private SystemSettingManager systemSettingManager; @RequestMapping( value = "/test", method = RequestMethod.POST ) - public void sendTestEmail( HttpServletRequest request, HttpServletResponse response ) + public void sendTestEmail( HttpServletResponse response ) { String userEmail = currentUserService.getCurrentUser().getEmail(); boolean smtpConfigured = emailService.emailEnabled(); boolean userEmailConfigured = userEmail != null && !userEmail.isEmpty(); - if ( smtpConfigured && userEmailConfigured ) - { - emailService.sendTestEmail(); - - ContextUtils.okResponse( response, "A test email was sent to " + userEmail ); - } - else if ( !smtpConfigured ) + if ( !smtpConfigured ) { ContextUtils.conflictResponse( response, "Could not send test email, SMTP server not configured" ); + return; } - else + + if ( !userEmailConfigured ) { ContextUtils.conflictResponse( response, "Could not send test email, no email configured for current user" ); - } + return; + } + + emailService.sendTestEmail(); + + ContextUtils.okResponse( response, "Test email was sent to " + userEmail ); + } + + @RequestMapping( value = "/notification", method = RequestMethod.POST ) + public void sendSystemNotificationEmail( @RequestBody Email email, HttpServletResponse response ) + { + boolean smtpConfigured = emailService.emailEnabled(); + boolean systemNotificationEmailValid = systemSettingManager.systemNotificationEmailValid(); + + if ( !smtpConfigured ) + { + ContextUtils.conflictResponse( response, "Could not send email, SMTP server not configured" ); + return; + } + + if ( !systemNotificationEmailValid ) + { + ContextUtils.conflictResponse( response, "Could not send email, system notification email address not set or not valid" ); + return; + } + + emailService.sendSystemEmail( email ); + + ContextUtils.okResponse( response, "System notification email sent" ); } }