diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java index 5b4c331fdc..831f365079 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java @@ -29,6 +29,17 @@ public class EmailMessageProperties { private String firstName; private String enrolmentUrl; private String title; + private String password; + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + private String userName; public String getMessageBody() { return messageBody; @@ -94,6 +105,14 @@ public class EmailMessageProperties { this.title = title; } + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + @Override public String toString() { return "EmailMessageProperties{" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 71891e3f7f..d5554fbf53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -39,4 +39,12 @@ public final class DeviceManagementConstants { public final static String OAUTH_ADMIN_SERVICE = "/services/OAuthAdminService"; } + public static final class EmailNotifications { + private EmailNotifications() { + throw new AssertionError(); + } + + public static final String ENROL_NOTIFICATION_TYPE = "enrol"; + public static final String USER_REGISTRATION_NOTIFICATION_TYPE = "userRegistration"; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 4e077dcc03..a63770e738 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -28,7 +28,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.email.EnrolmentNotifications; +import org.wso2.carbon.device.mgt.core.config.email.NotificationMessages; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -233,12 +233,26 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ @Override public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { - EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() - .getNotificationMessagesConfig().getEnrolmentNotifications(); - - String messageHeader = enrolmentNotifications.getHeader(); - String messageBody = enrolmentNotifications.getBody(); - String messageFooter = enrolmentNotifications.getFooter(); + List notificationMessages = DeviceConfigurationManager.getInstance() + .getNotificationMessagesConfig().getNotificationMessagesList(); + + String messageHeader = ""; + String messageBody = ""; + String messageFooter = ""; + String url = ""; + String subject = ""; + + for(NotificationMessages notificationMessage : notificationMessages){ + if (notificationMessage.getType().equals(DeviceManagementConstants.EmailNotifications + .ENROL_NOTIFICATION_TYPE)){ + messageHeader = notificationMessage.getHeader(); + messageBody = notificationMessage.getBody(); + messageFooter = notificationMessage.getFooter(); + url = notificationMessage.getUrl(); + subject = notificationMessage.getSubject(); + break; + } + } StringBuilder messageBuilder = new StringBuilder(); @@ -249,13 +263,77 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.TITLE + "\\}", URLEncoder.encode(title, EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); - messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.USERNAME + "\\}", + messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", URLEncoder.encode(emailMessageProperties.getFirstName(), EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); - messageBody = messageBody + System.getProperty("line.separator") + enrolmentNotifications.getUrl() - .replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.DOwN_LOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" + + EmailConstants.EnrolmentEmailConstants.DOwN_LOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + messageBuilder.append(messageHeader).append(System.getProperty("line.separator")).append( + System.getProperty("line.separator")); + + messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( + System.getProperty("line.separator")).append(messageFooter); + + } catch (IOException e) { + log.error("IO error in processing enrol email message "+emailMessageProperties); + throw new DeviceManagementException("Error replacing tags in email template '" + + emailMessageProperties.getSubject() + "'", e); + } + emailMessageProperties.setMessageBody(messageBuilder.toString()); + emailMessageProperties.setSubject(subject); + EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); + } + + @Override + public void sendRegistrationEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { + List notificationMessages = DeviceConfigurationManager.getInstance() + .getNotificationMessagesConfig().getNotificationMessagesList(); + + String messageHeader = ""; + String messageBody = ""; + String messageFooter = ""; + String url = ""; + String subject = ""; + + for(NotificationMessages notificationMessage : notificationMessages){ + if (notificationMessage.getType().equals(DeviceManagementConstants.EmailNotifications + .USER_REGISTRATION_NOTIFICATION_TYPE)){ + messageHeader = notificationMessage.getHeader(); + messageBody = notificationMessage.getBody(); + messageFooter = notificationMessage.getFooter(); + url = notificationMessage.getUrl(); + subject = notificationMessage.getSubject(); + break; + } + } + + StringBuilder messageBuilder = new StringBuilder(); + + try { + String title = ""; + if (emailMessageProperties.getTitle() != null){ + title = emailMessageProperties.getTitle(); + } + messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.TITLE + "\\}", + URLEncoder.encode(title, EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + + messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.USERNAME + "\\}", + URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); + + messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", + URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); + + messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" + + EmailConstants.EnrolmentEmailConstants.DOwN_LOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")).append( System.getProperty("line.separator")); @@ -268,7 +346,7 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); - emailMessageProperties.setSubject(enrolmentNotifications.getSubject()); + emailMessageProperties.setSubject(subject); EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessages.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessages.java new file mode 100644 index 0000000000..9fe3966902 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessages.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.config.email; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "Notifications") +public class NotificationMessages { + + private String header; + private String body; + private String footer; + private String subject; + private String url; + + @XmlAttribute(name = "type") + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + private String type; + + @XmlElement(name = "Header", required = true) + public String getHeader() { + return header; + } + + public void setHeader(String header) { + this.header = header; + } + + @XmlElement(name = "Body", required = true) + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + @XmlElement(name = "Footer", required = true) + public String getFooter() { + return footer; + } + + public void setFooter(String footer) { + this.footer = footer; + } + + @XmlElement(name = "Subject", required = true) + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + @XmlElement(name = "Url", required = true) + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessagesConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessagesConfig.java index 9a681b3b76..4e2b6ab9a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessagesConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/NotificationMessagesConfig.java @@ -20,19 +20,19 @@ package org.wso2.carbon.device.mgt.core.config.email; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; @XmlRootElement(name = "Notifications") public class NotificationMessagesConfig { - private EnrolmentNotifications enrolmentNotifications; + private List notificationMessagesList; - @XmlElement(name = "EnrolmentNotifications", required = true) - public EnrolmentNotifications getEnrolmentNotifications() { - return enrolmentNotifications; + public List getNotificationMessagesList() { + return notificationMessagesList; } - public void setEnrolmentNotifications(EnrolmentNotifications enrolmentNotifications) { - this.enrolmentNotifications = enrolmentNotifications; + public void setNotificationMessagesList(List notificationMessagesList) { + this.notificationMessagesList = notificationMessagesList; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java index 2e9babdbdb..9e37b57ed6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java @@ -24,6 +24,8 @@ public final class EmailConstants { public static final String USERNAME = "user-name"; public static final String DOwN_LOAD_URL = "downloadUrl"; public static final String ENCODED_SCHEME = "UTF-8"; + public static final String PASSWORD = "password"; + public static final String FIRST_NAME = "first-name"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index d9b22c404d..1f302ba9dc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -40,6 +40,8 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException; + void sendRegistrationEmail(EmailMessageProperties config) throws DeviceManagementException; + FeatureManager getFeatureManager(String type) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index a55fda0a79..1a2fefc443 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -26,7 +26,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import java.util.List; -public class DeviceManagementServiceImpl implements DeviceManagementService{ +public class DeviceManagementServiceImpl implements DeviceManagementService { @Override public String getProviderType() { @@ -173,10 +173,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService{ } @Override - public void sendEnrolmentInvitation( - EmailMessageProperties emailMessageProperties) throws DeviceManagementException { + public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties) + throws DeviceManagementException { DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .sendEnrolmentInvitation(emailMessageProperties); } -} + @Override + public void sendRegistrationEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .sendRegistrationEmail(emailMessageProperties); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java index d53ddca970..2fb995ecc8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java @@ -23,4 +23,5 @@ import org.wso2.carbon.device.mgt.common.EmailMessageProperties; public interface EmailService { public void sendEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException; + } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml index 563b4a58c0..1ed8609304 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml @@ -18,15 +18,30 @@ --> - -
Dear {title} {user-name},
+ +
Dear {title} {first-name},
You have been registered to the WSO2 MDM. Below is the link to enroll. {downloadUrl}
-Best Regards, -WSO2 Carbon Team -http://www.wso2.com + Best Regards, + WSO2 MDM Team + http://www.wso2.com
Enrol your device with WSO2 MDM -
+
+ +
Dear {title} {first-name},
+ You have been registered to WSO2 MDM with following credentials. + Username: {user-name} + Password: {password} + Below is the link to enroll. + + {downloadUrl} +
+ Best Regards, + WSO2 MDM Team + http://www.wso2.com +
+ Enrol your device with WSO2 MDM +
\ No newline at end of file