diff --git a/api/pom.xml b/api/pom.xml index 5cb4a73b0..5290e5b83 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -118,6 +118,10 @@ org.openmrs.module webservices.rest-omod-common + + org.bahmni.module + bahmni-commons-api + org.openmrs.web openmrs-web diff --git a/api/src/main/java/org/openmrs/module/appointments/notification/MailSender.java b/api/src/main/java/org/openmrs/module/appointments/notification/MailSender.java deleted file mode 100644 index d119ef2f8..000000000 --- a/api/src/main/java/org/openmrs/module/appointments/notification/MailSender.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.openmrs.module.appointments.notification; - -public interface MailSender { - void send(String subject, String body, String[] to, String[] cc, String[] bcc); -} diff --git a/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultMailSender.java b/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultMailSender.java deleted file mode 100644 index c3c2aed93..000000000 --- a/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultMailSender.java +++ /dev/null @@ -1,146 +0,0 @@ -package org.openmrs.module.appointments.notification.impl; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.openmrs.api.AdministrationService; -import org.openmrs.module.appointments.notification.MailSender; -import org.openmrs.util.OpenmrsUtil; - -import javax.mail.Address; -import javax.mail.Authenticator; -import javax.mail.Message; -import javax.mail.Multipart; -import javax.mail.PasswordAuthentication; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Date; -import java.util.Properties; - -public class DefaultMailSender implements MailSender { - private static final String EMAIL_PROPERTIES_FILENAME = "mail-config.properties"; - private Log log = LogFactory.getLog(this.getClass()); - private volatile Session session = null; - - private AdministrationService administrationService; - - public DefaultMailSender(AdministrationService administrationService) { - this.administrationService = administrationService; - } - - @Override - public void send(String subject, String bodyText, String[] to, String[] cc, String[] bcc) { - try { - MimeMessage mail = new MimeMessage(getSession()); - mail.setFrom(new InternetAddress(this.administrationService.getGlobalProperty("mail.from", ""))); - Address[] toAddresses = new Address[1]; - toAddresses[0] = new InternetAddress(to[0]); - mail.setRecipients(Message.RecipientType.TO, getAddresses(to)); - if (cc != null && cc.length > 0) { - mail.setRecipients(Message.RecipientType.CC, getAddresses(cc)); - } - if (bcc != null && bcc.length > 0) { - mail.setRecipients(Message.RecipientType.BCC, getAddresses(bcc)); - } - mail.setSubject(subject); - mail.setSentDate(new Date()); - - MimeBodyPart mimeBodyPart = new MimeBodyPart(); - //TODO: might need to read from GP mail.default_content_type - //mail.setContent(bodyText, "text/html;charset=utf-8"); - mimeBodyPart.setContent(bodyText, "text/html"); - Multipart multipart = new MimeMultipart(); - multipart.addBodyPart(mimeBodyPart); - mail.setContent(multipart); - Transport.send(mail); - } - catch (Exception e) { - throw new RuntimeException("Error occurred while sending email", e); - } - } - - private Address[] getAddresses(String[] addrs) throws AddressException { - if (addrs != null && addrs.length > 0) { - Address[] addresses = new Address[addrs.length]; - for (int i = 0; i < addrs.length; i++) { - addresses[i] = new InternetAddress(addrs[i]); - } - return addresses; - } - return new Address[0]; - } - - private Session getSession() { - if (session == null) { - synchronized(this) { - if (session == null) { - Properties sessionProperties = mailSessionPropertiesFromPath(); - if (sessionProperties == null) { - log.info("Could not load mail properties from application data directory. Loading from OMRS settings."); - sessionProperties = mailSessionPropertiesFromOMRS(); - } - final String user = sessionProperties.getProperty("mail.user"); - final String password = sessionProperties.getProperty("mail.password"); - if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password)) { - session = Session.getInstance(sessionProperties, new Authenticator() { - public PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(user, password); - } - }); - } - else { - session = Session.getInstance(sessionProperties); - } - } - } - } - return session; - } - - /** - * To be used as fallback. Mail properties are visible in openmrs settings. - * @param as - * @return - */ - private Properties mailSessionPropertiesFromOMRS() { - Properties p = new Properties(); - p.put("mail.transport.protocol", administrationService.getGlobalProperty("mail.transport_protocol", "smtp")); - p.put("mail.smtp.host", administrationService.getGlobalProperty("mail.smtp_host", "")); - p.put("mail.smtp.port", administrationService.getGlobalProperty("mail.smtp_port", "25")); // mail.smtp_port - p.put("mail.smtp.auth", administrationService.getGlobalProperty("mail.smtp_auth", "false")); // mail.smtp_auth - p.put("mail.smtp.starttls.enable", administrationService.getGlobalProperty("mail.smtp.starttls.enable", "true")); - p.put("mail.debug", administrationService.getGlobalProperty("mail.debug", "false")); - p.put("mail.from", administrationService.getGlobalProperty("mail.from", "")); - p.put("mail.user", administrationService.getGlobalProperty("mail.user", "")); - p.put("mail.password", administrationService.getGlobalProperty("mail.password", "")); - //p.put("mail.smtp.ssl.trust", "smtp.gmail.com"); - return p; - } - - private Properties mailSessionPropertiesFromPath() { - Path propertyFilePath = Paths.get(OpenmrsUtil.getApplicationDataDirectory(), EMAIL_PROPERTIES_FILENAME); - if (Files.exists(propertyFilePath)) { - Properties properties = new Properties(); - try { - log.info("Reading properties from: " + propertyFilePath); - properties.load(Files.newInputStream(propertyFilePath)); - return properties; - } catch (IOException e) { - log.error("Could not load email properties from: " + propertyFilePath, e); - } - } else { - log.warn("No mail configuration defined at " + propertyFilePath); - } - return null; - } - -} diff --git a/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultTCAppointmentPatientEmailNotifier.java b/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultTCAppointmentPatientEmailNotifier.java index 369611359..ff882992c 100644 --- a/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultTCAppointmentPatientEmailNotifier.java +++ b/api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultTCAppointmentPatientEmailNotifier.java @@ -3,13 +3,13 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.bahmni.module.bahmnicommons.notification.MailSender; import org.openmrs.Patient; import org.openmrs.PersonAttribute; import org.openmrs.api.context.Context; import org.openmrs.module.appointments.model.AppointmentKind; import org.openmrs.module.appointments.model.AppointmentServiceDefinition; import org.openmrs.module.appointments.notification.AppointmentEventNotifier; -import org.openmrs.module.appointments.notification.MailSender; import org.openmrs.module.appointments.notification.NotificationException; import org.openmrs.module.appointments.notification.NotificationResult; import org.openmrs.module.appointments.model.Appointment; diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml index 303090eaa..2f733d3cb 100644 --- a/api/src/main/resources/moduleApplicationContext.xml +++ b/api/src/main/resources/moduleApplicationContext.xml @@ -25,11 +25,8 @@ - - - - + diff --git a/api/src/test/java/org/openmrs/module/appointments/notification/impl/DefaultMailSenderTest.java b/api/src/test/java/org/openmrs/module/appointments/notification/impl/DefaultMailSenderTest.java deleted file mode 100644 index cbb64c495..000000000 --- a/api/src/test/java/org/openmrs/module/appointments/notification/impl/DefaultMailSenderTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.openmrs.module.appointments.notification.impl; - - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.openmrs.api.AdministrationService; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.modules.junit4.PowerMockRunner; - -import static org.hamcrest.Matchers.instanceOf; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.when; - -@PowerMockIgnore("javax.management.*") -@RunWith(PowerMockRunner.class) -public class DefaultMailSenderTest { - - @Mock - AdministrationService administrationService; - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void shouldThrowErrorForInvalidEmailAddress() { - DefaultMailSender mailSender = new DefaultMailSender(administrationService); - - when(administrationService.getGlobalProperty(eq("mail.transport_protocol"), eq("smtp"))).thenReturn("smtp"); - //when(administrationService.getGlobalProperty(any(), any())).thenReturn("smtp"); - when(administrationService.getGlobalProperty("mail.smtp_host", "")).thenReturn("localhost"); - when(administrationService.getGlobalProperty("mail.smtp_port", "25")).thenReturn("25"); - when(administrationService.getGlobalProperty("mail.smtp_auth", "false")).thenReturn("true"); - when(administrationService.getGlobalProperty("mail.smtp.starttls.enable", "true")).thenReturn("true"); - when(administrationService.getGlobalProperty("mail.debug", "false")).thenReturn("false"); - when(administrationService.getGlobalProperty("mail.from", "")).thenReturn("noreply@bahmni.org"); - when(administrationService.getGlobalProperty("mail.user", "")).thenReturn("test"); - when(administrationService.getGlobalProperty("mail.password", "")).thenReturn("random"); - - expectedEx.expect(RuntimeException.class); - expectedEx.expectMessage("Error occurred while sending email"); - expectedEx.expectCause(instanceOf(javax.mail.internet.AddressException.class)); - mailSender.send("test", "nothing", new String[] {""}, null, null); - } - -} diff --git a/api/src/test/java/org/openmrs/module/appointments/service/impl/DefaultTeleconsultationAppointmentPatientEmailNotifierTest.java b/api/src/test/java/org/openmrs/module/appointments/service/impl/DefaultTeleconsultationAppointmentPatientEmailNotifierTest.java index 72f59d3de..4af35ba8b 100644 --- a/api/src/test/java/org/openmrs/module/appointments/service/impl/DefaultTeleconsultationAppointmentPatientEmailNotifierTest.java +++ b/api/src/test/java/org/openmrs/module/appointments/service/impl/DefaultTeleconsultationAppointmentPatientEmailNotifierTest.java @@ -8,6 +8,7 @@ import org.mockito.AdditionalMatchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.bahmni.module.bahmnicommons.notification.MailSender; import org.openmrs.Patient; import org.openmrs.PersonAttribute; import org.openmrs.PersonAttributeType; @@ -15,7 +16,6 @@ import org.openmrs.api.context.Context; import org.openmrs.module.appointments.model.Appointment; import org.openmrs.module.appointments.notification.AppointmentEventNotifier; -import org.openmrs.module.appointments.notification.MailSender; import org.openmrs.module.appointments.notification.NotificationException; import org.openmrs.module.appointments.notification.impl.DefaultTCAppointmentPatientEmailNotifier; import org.powermock.api.mockito.PowerMockito; diff --git a/omod/pom.xml b/omod/pom.xml index a2dd796d7..bd9afa9b2 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -36,6 +36,10 @@ openmrs-web provided + + org.bahmni.module + bahmni-commons-api + javax.servlet javax.servlet-api diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 3b4e2b777..a612928ac 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -13,6 +13,7 @@ org.openmrs.module.webservices.rest + org.bahmni.module.bahmnicommons diff --git a/pom.xml b/pom.xml index e72bbc80f..a9c9f9d8b 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,7 @@ 1.10.1 1.6.2 2.29.0 + 0.2-SNAPSHOT @@ -192,6 +193,12 @@ ${openmrs.platform.version} provided + + org.bahmni.module + bahmni-commons-api + ${bahmniCommons.version} + provided + org.openmrs.web