From 3726a57d9609f94c119744a3b0ee32c8dc78dc9b Mon Sep 17 00:00:00 2001 From: prabathabey Date: Sun, 13 Mar 2016 00:51:54 +0530 Subject: [PATCH] Making email-sender a separate component and code cleanup --- .../mgt/common/EmailMessageProperties.java | 137 ------------ .../org.wso2.carbon.device.mgt.core/pom.xml | 17 +- .../mgt/core/DeviceManagementConstants.java | 17 ++ .../DeviceManagementConfigRepository.java | 11 - .../config/email/EmailConfigurations.java | 85 -------- .../device/mgt/core/email/EmailConstants.java | 32 --- .../device/mgt/core/email/EmailUtil.java | 65 ------ .../mgt/core/email/sender/EmailSender.java | 104 ---------- ...eManagementAxis2ConfigContextObserver.java | 45 ---- .../internal/DeviceManagementDataHolder.java | 11 + .../DeviceManagementServiceComponent.java | 109 ++-------- .../core/internal/EmailServiceComponent.java | 86 -------- .../core/internal/EmailServiceDataHolder.java | 61 ------ .../DeviceManagementProviderService.java | 51 +++-- .../DeviceManagementProviderServiceImpl.java | 93 ++++----- .../EmailMetaInfo.java} | 16 +- .../mgt/core/service/EmailServiceImpl.java | 34 --- .../mgt/core/util/DeviceManagerUtil.java | 42 +++- .../org.wso2.carbon.email.sender.core/pom.xml | 159 ++++++++++++++ ...ContentProcessingInterruptedException.java | 2 +- .../sender/core/ContentProviderInfo.java | 55 +++++ .../sender/core}/EmailContentProvider.java | 2 +- .../core}/EmailContentProviderFactory.java | 2 +- .../email/sender/core/EmailContext.java | 138 +++++++++++++ .../carbon/email/sender/core}/EmailData.java | 2 +- .../email/sender/core/EmailSenderConfig.java | 106 ++++++++++ ...ailSenderConfigurationFailedException.java | 45 ++++ .../email/sender/core/EmailSenderUtil.java | 45 ++++ .../core}/EmailSendingFailedException.java | 2 +- .../InvalidConfigurationStateException.java | 74 +++++++ .../core}/RegistryBasedResourceLoader.java | 2 +- .../carbon/email/sender/core}/TypedValue.java | 2 +- .../VelocityBasedEmailContentProvider.java | 4 +- ...EmailSenderAxis2ConfigContextObserver.java | 111 ++++++++++ .../core/internal/EmailSenderDataHolder.java | 69 +++++++ .../internal/EmailSenderServiceComponent.java | 195 ++++++++++++++++++ .../core/service/EmailSenderService.java} | 12 +- .../core/service/EmailSenderServiceImpl.java} | 45 ++-- .../src/test/resources/log4j.properties | 32 +++ .../src/test/resources/testng.xml | 29 +++ .../velocity.log | 0 components/email-sender/pom.xml | 60 ++++++ .../pom.xml | 1 + .../src/main/resources/conf/cdm-config.xml | 8 - .../resources/conf/notification-messages.xml | 40 ---- .../email/templates/user-enrollment.vm | 19 +- .../email/templates/user-registration.vm | 19 +- .../src/main/resources/p2.inf | 1 - .../pom.xml | 107 ++++++++++ .../src/main/resources/build.properties | 1 + .../resources/conf/email-sender-config.xml | 30 +++ .../main/resources/email/templates/default.vm | 34 +++ .../src/main/resources/p2.inf | 3 + features/email-sender/pom.xml | 41 ++++ pom.xml | 18 ++ 55 files changed, 1593 insertions(+), 938 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/EmailConfigurations.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailUtil.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailSender.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceDataHolder.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/{email/EmailContext.java => service/EmailMetaInfo.java} (85%) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailServiceImpl.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/pom.xml rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/ContentProcessingInterruptedException.java (96%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProviderInfo.java rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/EmailContentProvider.java (95%) rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/EmailContentProviderFactory.java (94%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContext.java rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/EmailData.java (96%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfig.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfigurationFailedException.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderUtil.java rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/EmailSendingFailedException.java (96%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/InvalidConfigurationStateException.java rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/RegistryBasedResourceLoader.java (98%) rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/TypedValue.java (96%) rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core}/VelocityBasedEmailContentProvider.java (96%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderDataHolder.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java} (66%) rename components/{device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java => email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java} (71%) create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/log4j.properties create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/testng.xml create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/velocity.log create mode 100644 components/email-sender/pom.xml delete mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml create mode 100644 features/email-sender/org.wso2.carbon.email.sender.feature/pom.xml create mode 100644 features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/build.properties create mode 100644 features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/conf/email-sender-config.xml create mode 100644 features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/default.vm create mode 100644 features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/p2.inf create mode 100644 features/email-sender/pom.xml 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 deleted file mode 100644 index 7bac7098cc8..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2014, 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.common; - -import java.util.Arrays; - -public class EmailMessageProperties { - - private String messageBody; - private String[] mailTo; - private String[] ccList; - private String[] bccList; - private String subject; - private String firstName; - private String enrolmentUrl; - private String title; - private String password; - private String userName; - private String domainName; - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getDomainName() { - return domainName; - } - - public void setDomainName(String domainName) { - this.domainName = domainName; - } - - public String getMessageBody() { - return messageBody; - } - - public void setMessageBody(String messageBody) { - this.messageBody = messageBody; - } - - public String[] getMailTo() { - return mailTo; - } - - public void setMailTo(String[] mailTo) { - this.mailTo = mailTo; - } - - public String[] getCcList() { - return ccList; - } - - public void setCcList(String[] ccList) { - this.ccList = ccList; - } - - public String[] getBccList() { - return bccList; - } - - public void setBccList(String[] bccList) { - this.bccList = bccList; - } - - public String getSubject() { - return subject; - } - - public void setSubject(String subject) { - this.subject = subject; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getEnrolmentUrl() { - return enrolmentUrl; - } - - public void setEnrolmentUrl(String enrolmentUrl) { - this.enrolmentUrl = enrolmentUrl; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - @Override - public String toString() { - return "EmailMessageProperties{" + - "messageBody='" + messageBody + '\'' + - ", mailTo=" + Arrays.toString(mailTo) + - ", ccList=" + Arrays.toString(ccList) + - ", bccList=" + Arrays.toString(bccList) + - ", subject='" + subject + '\'' + - ", firstName='" + firstName + '\'' + - ", enrolmentUrl='" + enrolmentUrl + '\'' + - ", title='" + title + '\'' + - '}'; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index d4d6214a17c..da2705de4eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -81,17 +81,10 @@ org.wso2.carbon.ndatasource.core, org.wso2.carbon.apimgt.impl, org.wso2.carbon.ndatasource.core, - org.apache.axis2.transport.mail, org.apache.catalina, org.apache.catalina.core, org.apache.commons.collections, - org.apache.velocity, - org.apache.velocity.app, - org.apache.velocity.context, - org.apache.velocity.exception, - org.apache.velocity.runtime.resource, - org.apache.velocity.runtime.resource.loader, - org.apache.commons.io + org.wso2.carbon.email.sender.* !org.wso2.carbon.device.mgt.core.internal, @@ -225,12 +218,12 @@ tomcat-servlet-api - org.apache.velocity - velocity + commons-collections.wso2 + commons-collections - commons-io.wso2 - commons-io + org.wso2.carbon.devicemgt + org.wso2.carbon.email.sender.core 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 1b0cf9ee103..ad4a8e64ee2 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 @@ -58,4 +58,21 @@ public final class DeviceManagementConstants { public static final String MONITOR_OPERATION_CODE = "MONITOR"; public static final String POLICY_OPERATION_CODE = PolicyOperation.POLICY_OPERATION_CODE; } + + public static final class EmailAttributes { + private EmailAttributes() { + throw new AssertionError(); + } + + public static final String ENCODED_SCHEME = "UTF-8"; + public static final String FIRST_NAME = "first-name"; + public static final String USERNAME = "username"; + public static final String PASSWORD = "password"; + public static final String DOMAIN = "domain-name"; + + public static final String SERVER_BASE_URL_HTTPS = "base-url-https"; + public static final String SERVER_BASE_URL_HTTP = "base-url-http"; + public static final String DOWNLOAD_URL = "download-url"; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfigRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfigRepository.java index d3bfb77b9ab..5ef49d030db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfigRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfigRepository.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.core.config; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; -import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; @@ -32,7 +31,6 @@ import javax.xml.bind.annotation.XmlRootElement; public class DeviceManagementConfigRepository { private DataSourceConfig dataSourceConfig; - private EmailConfigurations emailConfigurations; private IdentityConfigurations identityConfigurations; private PolicyConfiguration policyConfiguration; @@ -45,15 +43,6 @@ public class DeviceManagementConfigRepository { this.dataSourceConfig = dataSourceConfig; } - @XmlElement(name = "EmailClientConfiguration", required = true) - public EmailConfigurations getEmailConfigurations() { - return emailConfigurations; - } - - public void setEmailConfigurations(EmailConfigurations emailConfigurations) { - this.emailConfigurations = emailConfigurations; - } - @XmlElement(name = "IdentityConfiguration", required = true) public IdentityConfigurations getIdentityConfigurations() { return identityConfigurations; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/EmailConfigurations.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/EmailConfigurations.java deleted file mode 100644 index b0e3a9009ae..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/email/EmailConfigurations.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "EmailClientConfiguration") -public class EmailConfigurations { - - private int minNumOfThread; - private int maxNumOfThread; - private int keepAliveTime; - private int threadQueueCapacity; - private String lBHostPortPrefix; - private String enrollmentContextPath; - - @XmlElement(name = "minimumThread", required = true) - public int getMinNumOfThread() { - return minNumOfThread; - } - - public void setMinNumOfThread(int minNumOfThread) { - this.minNumOfThread = minNumOfThread; - } - @XmlElement(name = "maximumThread", required = true) - public int getMaxNumOfThread() { - return maxNumOfThread; - } - - public void setMaxNumOfThread(int maxNumOfThread) { - this.maxNumOfThread = maxNumOfThread; - } - - @XmlElement(name = "keepAliveTime", required = true) - public int getKeepAliveTime() { - return keepAliveTime; - } - - public void setKeepAliveTime(int keepAliveTime) { - this.keepAliveTime = keepAliveTime; - } - @XmlElement(name = "ThreadQueueCapacity", required = true) - public int getThreadQueueCapacity() { - return threadQueueCapacity; - } - - public void setThreadQueueCapacity(int threadQueueCapacity) { - this.threadQueueCapacity = threadQueueCapacity; - } - - @XmlElement(name = "LBHostPortPrefix", required = true) - public String getlBHostPortPrefix() { - return lBHostPortPrefix; - } - - public void setlBHostPortPrefix(String lBHostPortPrefix) { - this.lBHostPortPrefix = lBHostPortPrefix; - } - - @XmlElement(name = "enrollmentContextPath", required = true) - public String getEnrollmentContextPath() { - return enrollmentContextPath; - } - - public void setEnrollmentContextPath(String enrollmentContextPath) { - this.enrollmentContextPath = enrollmentContextPath; - } -} 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 deleted file mode 100644 index 1b541d1c90d..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailConstants.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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.email; - -public final class EmailConstants { - - public static final String ENCODED_SCHEME = "UTF-8"; - public static final String FIRST_NAME = "first-name"; - public static final String USERNAME = "username"; - public static final String PASSWORD = "password"; - public static final String DOMAIN = "domain-name"; - - public static final String SERVER_BASE_URL_HTTPS = "base-url-https"; - public static final String SERVER_BASE_URL_HTTP = "base-url-http"; - public static final String DOWNLOAD_URL = "download-url"; - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailUtil.java deleted file mode 100644 index f7d8bfb3e6f..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailUtil.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2016, 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.email; - -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.utils.CarbonUtils; -import org.wso2.carbon.utils.ConfigurationContextService; -import org.wso2.carbon.utils.NetworkUtils; - -public class EmailUtil { - - public static String getServerBaseHttpsUrl() { - String hostName = "localhost"; - try { - hostName = NetworkUtils.getMgtHostName(); - } catch (Exception ignored) { - } - String mgtConsoleTransport = CarbonUtils.getManagementTransport(); - ConfigurationContextService configContextService = - DeviceManagementDataHolder.getInstance().getConfigurationContextService(); - int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport); - int httpsProxyPort = - CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), - mgtConsoleTransport); - if (httpsProxyPort > 0) { - port = httpsProxyPort; - } - return "https://" + hostName + ":" + port; - } - - public static String getServerBaseHttpUrl() { - String hostName = "localhost"; - try { - hostName = NetworkUtils.getMgtHostName(); - } catch (Exception ignored) { - } - ConfigurationContextService configContextService = - DeviceManagementDataHolder.getInstance().getConfigurationContextService(); - int port = CarbonUtils.getTransportPort(configContextService, "http"); - int httpProxyPort = - CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), - "http"); - if (httpProxyPort > 0) { - port = httpProxyPort; - } - return "http://" + hostName + ":" + port; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailSender.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailSender.java deleted file mode 100644 index 2ed38656167..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailSender.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2016, 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.email.sender; - -import org.apache.axiom.om.OMAbstractFactory; -import org.apache.axiom.om.OMElement; -import org.apache.axis2.AxisFault; -import org.apache.axis2.Constants; -import org.apache.axis2.addressing.EndpointReference; -import org.apache.axis2.client.Options; -import org.apache.axis2.client.ServiceClient; -import org.apache.axis2.context.MessageContext; -import org.apache.axis2.transport.base.BaseConstants; -import org.apache.axis2.transport.mail.MailConstants; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations; -import org.wso2.carbon.device.mgt.core.email.EmailData; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -public class EmailSender { - - private static ThreadPoolExecutor threadPoolExecutor; - - static { - EmailConfigurations emailConfig = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getDeviceManagementConfigRepository().getEmailConfigurations(); - - threadPoolExecutor = new ThreadPoolExecutor(emailConfig.getMinNumOfThread(), - emailConfig.getMaxNumOfThread(), emailConfig.getKeepAliveTime(), TimeUnit.SECONDS, - new LinkedBlockingQueue(emailConfig.getThreadQueueCapacity())); - } - - private static final String EMAIL_URI_SCHEME = "mailto:"; - private static Log log = LogFactory.getLog(EmailSender.class); - - public void sendEmail(EmailData emailData, String... recipients) throws DeviceManagementException { - for (String recipient : recipients) { - threadPoolExecutor - .submit(new EmailSenderTask(recipient, emailData)); - } - } - - public static class EmailSenderTask implements Runnable { - - private String to; - private EmailData data; - - EmailSenderTask(String to, EmailData data) { - this.to = to; - this.data = data; - } - - public void run() { - Map headerMap = new HashMap<>(); - headerMap.put(MailConstants.MAIL_HEADER_SUBJECT, data.getSubject()); - OMElement payload = OMAbstractFactory.getOMFactory().createOMElement( - BaseConstants.DEFAULT_TEXT_WRAPPER, null); - payload.setText(data.getBody()); - try { - ServiceClient serviceClient = new ServiceClient(); - Options options = new Options(); - options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE); - options.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap); - options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT, - MailConstants.TRANSPORT_FORMAT_TEXT); - options.setTo(new EndpointReference(EMAIL_URI_SCHEME + to)); - serviceClient.setOptions(options); - serviceClient.fireAndForget(payload); - if (log.isDebugEnabled()) { - log.debug("Email has been successfully sent to '" + to + "'"); - } - } catch (AxisFault e) { - log.error("Error occurred while delivering the message, subject: '" + data.getSubject() + - "', to: '" + to + "'", e); - } - } - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigContextObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigContextObserver.java index 169ae01bf87..3634164d338 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigContextObserver.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigContextObserver.java @@ -61,49 +61,4 @@ public class DeviceManagementAxis2ConfigContextObserver implements Axis2Configur } - private void setupEmailTemplates() throws DeviceManagementException { - File templateDir = - new File(CarbonUtils.getCarbonRepository() + "resources" + File.separator + "email-templates"); - if (!templateDir.exists()) { - if (log.isDebugEnabled()) { - log.debug("The directory that is expected to use as the container for all email templates is not " + - "available. Therefore, no template is uploaded to the registry"); - } - } - if (templateDir.canRead()) { - File[] templates = templateDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - name = name.toLowerCase(); - return name.endsWith(".vm"); - } - }); - try { - Registry registry = - CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); - if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { - Collection collection = registry.newCollection(); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); - for (File template : templates) { - Resource resource = registry.newResource(); - resource.setContent(template); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } else { - for (File template : templates) { - if (!registry.resourceExists( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName())) { - Resource resource = registry.newResource(); - resource.setContent(template); - registry.put( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } - } - } catch (RegistryException e) { - throw new DeviceManagementException("Error occurred while setting up email templates", e); - } - } - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 0921ef50810..1558d3c5321 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -46,6 +47,7 @@ public class DeviceManagementDataHolder { private ConfigurationContextService configurationContextService; private HashMap requireDeviceAuthorization = new HashMap<>(); private DeviceAccessAuthorizationService deviceAccessAuthorizationService; + private EmailSenderService emailSenderService; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); @@ -164,4 +166,13 @@ public class DeviceManagementDataHolder { DeviceAccessAuthorizationService deviceAccessAuthorizationService) { this.deviceAccessAuthorizationService = deviceAccessAuthorizationService; } + + public EmailSenderService getEmailSenderService() { + return emailSenderService; + } + + public void setEmailSenderService(EmailSenderService emailSenderService) { + this.emailSenderService = emailSenderService; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 57989e3d8a7..e2ab650831f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -17,13 +17,10 @@ */ package org.wso2.carbon.device.mgt.core.internal; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; -import org.w3c.dom.Document; -import org.w3c.dom.Element; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; @@ -54,21 +51,12 @@ import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceIm import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ndatasource.core.DataSourceService; -import org.wso2.carbon.registry.api.Collection; -import org.wso2.carbon.registry.api.Registry; -import org.wso2.carbon.registry.api.RegistryException; -import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; -import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.ConfigurationContextService; -import org.xml.sax.SAXException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import java.io.*; import java.util.ArrayList; import java.util.List; @@ -110,6 +98,12 @@ import java.util.List; * policy="dynamic" * bind="setConfigurationContextService" * unbind="unsetConfigurationContextService" + * @scr.reference name="email.sender.service" + * interface="org.wso2.carbon.email.sender.core.service.EmailSenderService" + * cardinality="0..1" + * policy="dynamic" + * bind="setEmailSenderService" + * unbind="unsetEmailSenderService" */ public class DeviceManagementServiceComponent { @@ -152,9 +146,6 @@ public class DeviceManagementServiceComponent { this.setupDeviceManagementSchema(dsConfig); } - /* Setting up email templates */ - this.setupEmailTemplates(); - /* Registering declarative service instances exposed by DeviceManagementServiceComponent */ this.registerServices(componentContext); @@ -231,78 +222,6 @@ public class DeviceManagementServiceComponent { } } - private void setupEmailTemplates() throws DeviceManagementException { - File templateDir = - new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + - "resources" + File.separator + "email-templates"); - if (!templateDir.exists()) { - if (log.isDebugEnabled()) { - log.debug("The directory that is expected to use as the container for all email templates is not " + - "available. Therefore, no template is uploaded to the registry"); - } - } - if (templateDir.canRead()) { - File[] templates = templateDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - name = name.toLowerCase(); - return name.endsWith(".vm"); - } - }); - try { - Registry registry = - DeviceManagementDataHolder.getInstance().getRegistryService().getConfigSystemRegistry(); - if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { - Collection collection = registry.newCollection(); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); - for (File template : templates) { - Resource resource = registry.newResource(); - String contents = FileUtils.readFileToString(template); - resource.setContent(contents.getBytes()); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } else { - /* Existence of a given resource is not checked consciously, before performing registry.put() below. - * The rationale is that, the only less expensive way that one can check if a resource exists is - * that through registry.resourceExists(), which only checks if 'some' resource exists at the given - * registry path. However, this does not capture scenarios where there can be updated contents to - * the same resource of which the path hasn't changed after it has been initialized for the first - * time. Therefore, whenever the server starts-up, all email templates are updated just to avoid - * the aforementioned problem */ - for (File template : templates) { - Resource resource = registry.newResource(); - String contents = FileUtils.readFileToString(template); - resource.setContent(contents.getBytes()); - registry.put( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } - } catch (RegistryException e) { - throw new DeviceManagementException("Error occurred while setting up email templates", e); - } catch (FileNotFoundException e) { - throw new DeviceManagementException("Error occurred while writing template file contents as an " + - "input stream of a resource", e); - } catch (IOException e) { - throw new DeviceManagementException("Error occurred while serializing file contents to a string", e); - } - } - } - - private Element parseFile(File file) throws DeviceManagementException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder; - try { - builder = factory.newDocumentBuilder(); - Document doc = builder.parse(file); - return doc.getDocumentElement(); - } catch (ParserConfigurationException e) { - throw new DeviceManagementException("Error occurred while creating a document builder to parse file '" + - file.getName() + "'", e); - } catch (SAXException | IOException e) { - throw new DeviceManagementException("Error occurred while parsing file '" + file.getName() + "'", e); - } - } - private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { DeviceManagementSchemaInitializer initializer = new DeviceManagementSchemaInitializer(config); log.info("Initializing device management repository database schema"); @@ -432,6 +351,20 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setConfigurationContextService(null); } + protected void setEmailSenderService(EmailSenderService emailSenderService) { + if (log.isDebugEnabled()) { + log.debug("Setting Email Sender Service"); + } + DeviceManagementDataHolder.getInstance().setEmailSenderService(emailSenderService); + } + + protected void unsetEmailSenderService(EmailSenderService emailSenderService) { + if (log.isDebugEnabled()) { + log.debug("Un-setting Email Sender Service"); + } + DeviceManagementDataHolder.getInstance().setEmailSenderService(null); + } + public static void registerStartupListener(DeviceManagerStartupListener startupListener) { startupListeners.add(startupListener); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java deleted file mode 100644 index 708198a0c47..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.internal; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; -import org.wso2.carbon.device.mgt.core.email.sender.EmailServiceProviderImpl; -import org.wso2.carbon.device.mgt.core.service.EmailService; -import org.wso2.carbon.device.mgt.core.service.EmailServiceImpl; -import org.wso2.carbon.utils.ConfigurationContextService; - -/** - * @scr.component component.name="org.wso2.carbon.device.emailmanager" immediate="true" - * @scr.reference name="configurationcontext.service" - * interface="org.wso2.carbon.utils.ConfigurationContextService" cardinality="1..1" - * policy="dynamic" bind="setConfigurationContextService" unbind="unsetConfigurationContextService" - */ -public class EmailServiceComponent { - - private static Log log = LogFactory.getLog(EmailServiceComponent.class); - - /** - * initialize the email service here service here. - * - * @param context - */ - protected void activate(ComponentContext context) { - try { - if (log.isDebugEnabled()) { - log.debug("Initializing email service bundle"); - } - - DeviceConfigurationManager.getInstance().initConfig(); - DeviceManagementConfig config = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); - /* Initializing Email Service Configurations */ - - EmailService emailServiceProvider = new EmailServiceProviderImpl(); - EmailServiceDataHolder.getInstance().setEmailServiceProvider(emailServiceProvider); ; - - this.registerServices(context); - - if (log.isDebugEnabled()) { - log.debug("Email management core bundle has been successfully initialized"); - } - } catch (Throwable e) { - log.error("Error occurred while initializing device management core bundle", e); - } - } - protected void setConfigurationContextService(ConfigurationContextService configurationContextService) { - EmailServiceDataHolder.getInstance().setConfigurationContextService(configurationContextService); - } - protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) { - EmailServiceDataHolder.getInstance().setConfigurationContextService(null); - } - - private void registerServices(ComponentContext componentContext) { - if (log.isDebugEnabled()) { - log.debug("Registering OSGi service Email Service Impl"); - } - /* Registering Email Service */ - BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(EmailService.class.getName(), new EmailServiceImpl(), null); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceDataHolder.java deleted file mode 100644 index e73b1d4e272..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceDataHolder.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.internal; - -import org.wso2.carbon.device.mgt.core.service.EmailService; -import org.wso2.carbon.utils.ConfigurationContextService; - -public class EmailServiceDataHolder { - - private static EmailServiceDataHolder thisInstance = new EmailServiceDataHolder(); - private ConfigurationContextService configurationContextService; - private EmailService emailServiceProvider; - - public static EmailServiceDataHolder getThisInstance() { - return thisInstance; - } - - public static void setThisInstance(EmailServiceDataHolder thisInstance) { - EmailServiceDataHolder.thisInstance = thisInstance; - } - - private EmailServiceDataHolder() { - } - - public static EmailServiceDataHolder getInstance() { - return thisInstance; - } - - public ConfigurationContextService getConfigurationContextService() { - if (configurationContextService == null) { - throw new IllegalStateException("ConfigurationContext service is not initialized properly"); - } - return configurationContextService; - } - - public void setConfigurationContextService(ConfigurationContextService configurationContextService) { - this.configurationContextService = configurationContextService; - } - public EmailService getEmailServiceProvider() { - return emailServiceProvider; - } - - public void setEmailServiceProvider(EmailService emailServiceProvider) { - this.emailServiceProvider = emailServiceProvider; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index e017ae5259c..0c307f6e0ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -18,15 +18,11 @@ package org.wso2.carbon.device.mgt.core.service; import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.DeviceManager; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; -import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.email.EmailContext; import java.util.List; @@ -43,94 +39,95 @@ public interface DeviceManagementProviderService extends OperationManager { /** * Method to retrieve all the devices with pagination support. * - * @param request PaginationRequest object holding the data for pagination + * @param request PaginationRequest object holding the data for pagination * @return PaginationResult - Result including the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * devices. + * devices. */ PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException; /** * Method to retrieve all the devices with pagination support. * - * @param request PaginationRequest object holding the data for pagination + * @param request PaginationRequest object holding the data for pagination * @return PaginationResult - Result including the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * devices. + * devices. */ PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException; - void sendEnrolmentInvitation(EmailContext emailContext) throws DeviceManagementException; + void sendEnrolmentInvitation(EmailMetaInfo metaInfo) throws DeviceManagementException; - void sendRegistrationEmail(EmailContext emailContext) throws DeviceManagementException; + void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException; FeatureManager getFeatureManager(String deviceType) throws DeviceManagementException; /** * Proxy method to get the tenant configuration of a given platform. * - * @param deviceType Device platform + * @param deviceType Device platform * @return Tenant configuration settings of the particular tenant and platform. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * configuration. + * configuration. */ TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException; /** * Method to get the list of devices owned by an user with paging information. * - * @param request PaginationRequest object holding the data for pagination + * @param request PaginationRequest object holding the data for pagination * @return List of devices owned by a particular user along with the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ PaginationResult getDevicesOfUser(PaginationRequest request) throws DeviceManagementException; /** * Method to get the list of devices filtered by the ownership with paging information. * - * @param request PaginationRequest object holding the data for pagination + * @param request PaginationRequest object holding the data for pagination * @return List of devices owned by a particular user along with the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ PaginationResult getDevicesByOwnership(PaginationRequest request) throws DeviceManagementException; /** * Method to get the list of devices owned by an user. * - * @param userName Username of the user + * @param userName Username of the user * @return List of devices owned by a particular user * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ List getDevicesOfUser(String userName) throws DeviceManagementException; /** * Method to get the list of devices owned by users of a particular user-role. * - * @param roleName Role name of the users + * @param roleName Role name of the users * @return List of devices owned by users of a particular role * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ List getAllDevicesOfRole(String roleName) throws DeviceManagementException; /** * Method to get the count of all types of devices. + * * @return device count * @throws DeviceManagementException If some unusual behaviour is observed while counting - * the devices + * the devices */ int getDeviceCount() throws DeviceManagementException; /** * Method to get the list of devices that matches with the given device name. * - * @param deviceName name of the device + * @param deviceName name of the device * @return List of devices that matches with the given device name. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ List getDevicesByName(String deviceName) throws DeviceManagementException; @@ -140,7 +137,7 @@ public interface DeviceManagementProviderService extends OperationManager { * @param request PaginationRequest object holding the data for pagination * @return List of devices in given status along with the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ PaginationResult getDevicesByName(PaginationRequest request) throws DeviceManagementException; @@ -161,7 +158,7 @@ public interface DeviceManagementProviderService extends OperationManager { * @param request PaginationRequest object holding the data for pagination * @return List of devices in given status along with the required parameters necessary to do pagination. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * device list */ PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException; @@ -189,7 +186,7 @@ public interface DeviceManagementProviderService extends OperationManager { Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; - List getAvailableDeviceTypes() throws DeviceManagementException; + List getAvailableDeviceTypes() throws DeviceManagementException; boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException; @@ -200,6 +197,6 @@ public interface DeviceManagementProviderService extends OperationManager { boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException; - void notifyOperationToDevices(Operation operation, List deviceIds)throws DeviceManagementException; + void notifyOperationToDevices(Operation operation, List deviceIds) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index e7c972bb8c9..2e274012aa8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -28,15 +28,16 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations; import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.email.*; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; -import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.email.sender.core.ContentProviderInfo; +import org.wso2.carbon.email.sender.core.EmailContext; +import org.wso2.carbon.email.sender.core.EmailSendingFailedException; +import org.wso2.carbon.email.sender.core.TypedValue; import org.wso2.carbon.user.api.UserStoreException; import java.sql.SQLException; @@ -49,13 +50,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private DeviceTypeDAO deviceTypeDAO; private EnrollmentDAO enrollmentDAO; private DeviceManagementPluginRepository pluginRepository; - private EmailContentProvider contentProvider; private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); public DeviceManagementProviderServiceImpl() { this.pluginRepository = new DeviceManagementPluginRepository(); - this.contentProvider = EmailContentProviderFactory.getContentProvider(); initDataAccessObjects(); /* Registering a listener to retrieve events when some device management service plugin is installed after * the component is done getting initialized */ @@ -443,8 +442,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + throw new DeviceManagementException( + "Error occurred while retrieving device list pertaining to the current tenant", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { @@ -512,58 +511,44 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void sendEnrolmentInvitation(EmailContext emailCtx) throws DeviceManagementException { + public void sendEnrolmentInvitation(EmailMetaInfo metaInfo) throws DeviceManagementException { Map, Object>> params = new HashMap<>(); - EmailConfigurations emailConfig = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getDeviceManagementConfigRepository().getEmailConfigurations(); - params.put(EmailConstants.FIRST_NAME, - new TypedValue, Object>(String.class, emailCtx.getProperty("first-name"))); - params.put(EmailConstants.DOWNLOAD_URL, - new TypedValue, Object>(String.class, - emailConfig.getlBHostPortPrefix() + emailConfig.getEnrollmentContextPath())); - params.put(EmailConstants.SERVER_BASE_URL_HTTPS, - new TypedValue, Object>(String.class, EmailUtil.getServerBaseHttpsUrl())); - params.put(EmailConstants.SERVER_BASE_URL_HTTP, - new TypedValue, Object>(String.class, EmailUtil.getServerBaseHttpUrl())); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME, + new TypedValue, Object>(String.class, metaInfo.getProperty("first-name"))); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTPS, + new TypedValue, Object>(String.class, DeviceManagerUtil.getServerBaseHttpsUrl())); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTP, + new TypedValue, Object>(String.class, DeviceManagerUtil.getServerBaseHttpUrl())); try { - EmailData data = contentProvider.getContent("user-enrollment", params); - EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailCtx.getRecipients(), data); - } catch (ContentProcessingInterruptedException e) { - throw new DeviceManagementException("Error occurred while processing contents of the " + - "enrollment invitation", e); + EmailContext ctx = + new EmailContext.EmailContextBuilder(new ContentProviderInfo("user-enrollment", params), + metaInfo.getRecipients()).build(); + DeviceManagementDataHolder.getInstance().getEmailSenderService().sendEmail(ctx); } catch (EmailSendingFailedException e) { throw new DeviceManagementException("Error occurred while sending enrollment invitation", e); } } @Override - public void sendRegistrationEmail(EmailContext emailCtx) throws DeviceManagementException { + public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException { Map, Object>> params = new HashMap<>(); - EmailConfigurations emailConfig = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getDeviceManagementConfigRepository().getEmailConfigurations(); - params.put(EmailConstants.FIRST_NAME, - new TypedValue, Object>(String.class, emailCtx.getProperty("first-name"))); - params.put(EmailConstants.DOWNLOAD_URL, - new TypedValue, Object>(String.class, - emailConfig.getlBHostPortPrefix() + emailConfig.getEnrollmentContextPath())); - params.put(EmailConstants.USERNAME, - new TypedValue, Object>(String.class, emailCtx.getProperty("username"))); - params.put(EmailConstants.PASSWORD, - new TypedValue, Object>(String.class, emailCtx.getProperty("password"))); - params.put(EmailConstants.DOMAIN, - new TypedValue, Object>(String.class, emailCtx.getProperty("domain"))); - params.put(EmailConstants.SERVER_BASE_URL_HTTPS, - new TypedValue, Object>(String.class, EmailUtil.getServerBaseHttpsUrl())); - params.put(EmailConstants.SERVER_BASE_URL_HTTP, - new TypedValue, Object>(String.class, EmailUtil.getServerBaseHttpUrl())); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME, + new TypedValue, Object>(String.class, metaInfo.getProperty("first-name"))); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.USERNAME, + new TypedValue, Object>(String.class, metaInfo.getProperty("username"))); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.PASSWORD, + new TypedValue, Object>(String.class, metaInfo.getProperty("password"))); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.DOMAIN, + new TypedValue, Object>(String.class, metaInfo.getProperty("domain"))); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTPS, + new TypedValue, Object>(String.class, DeviceManagerUtil.getServerBaseHttpsUrl())); + params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTP, + new TypedValue, Object>(String.class, DeviceManagerUtil.getServerBaseHttpUrl())); try { - EmailData data = contentProvider.getContent("user-registration", params); - EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailCtx.getRecipients(), data); - } catch (ContentProcessingInterruptedException e) { - throw new DeviceManagementException("Error occurred while processing contents of the " + - "enrollment invitation", e); + EmailContext ctx = + new EmailContext.EmailContextBuilder(new ContentProviderInfo("user-registration", params), + metaInfo.getRecipients()).build(); + DeviceManagementDataHolder.getInstance().getEmailSenderService().sendEmail(ctx); } catch (EmailSendingFailedException e) { throw new DeviceManagementException("Error occurred while sending user registration notification", e); } @@ -647,11 +632,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Map registeredTypes = pluginRepository.getAllDeviceManagementServices(); DeviceType deviceType; if (registeredTypes != null && deviceTypesInDatabase != null) { - for (int x = 0; x < deviceTypesInDatabase.size(); x++) { - if (registeredTypes.get(deviceTypesInDatabase.get(x).getName()) != null) { + for (DeviceType aDeviceTypesInDatabase : deviceTypesInDatabase) { + if (registeredTypes.get(aDeviceTypesInDatabase.getName()) != null) { deviceType = new DeviceType(); - deviceType.setId(deviceTypesInDatabase.get(x).getId()); - deviceType.setName(deviceTypesInDatabase.get(x).getName()); + deviceType.setId(aDeviceTypesInDatabase.getId()); + deviceType.setName(aDeviceTypesInDatabase.getName()); deviceTypesResponse.add(deviceType); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContext.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailMetaInfo.java similarity index 85% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContext.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailMetaInfo.java index a8af2c612f6..c5ac4af2672 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContext.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailMetaInfo.java @@ -16,16 +16,18 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.device.mgt.core.service; -import java.util.*; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; -public class EmailContext { +public class EmailMetaInfo { private Set recipients; private Properties properties; - public EmailContext(final Set recipients, final Properties properties) { + public EmailMetaInfo(final Set recipients, final Properties properties) { if (recipients == null) { throw new IllegalArgumentException("Recipient list cannot be null"); } @@ -40,7 +42,7 @@ public class EmailContext { this.properties = properties; } - public EmailContext(final String recipient, final Properties properties) { + public EmailMetaInfo(final String recipient, final Properties properties) { if (recipient == null || recipient.isEmpty()) { throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " + "recipient email address"); @@ -54,10 +56,6 @@ public class EmailContext { this.properties = properties; } - public EmailContext(final Set recipients) { - this(recipients, new Properties()); - } - public Set getRecipients() { return recipients; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailServiceImpl.java deleted file mode 100644 index 9bc29340b6c..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailServiceImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.service; - -import org.wso2.carbon.device.mgt.core.email.EmailData; -import org.wso2.carbon.device.mgt.core.email.EmailSendingFailedException; -import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; - -import java.util.Set; - -public class EmailServiceImpl implements EmailService { - - @Override - public void sendEmail(Set recipients, EmailData emailData) throws EmailSendingFailedException { - EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(recipients, emailData); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index bf2ee59a393..5d92efde927 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -31,12 +31,15 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.ConfigurationContextService; +import org.wso2.carbon.utils.NetworkUtils; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; -import java.sql.SQLException; import java.util.HashMap; import java.util.Hashtable; import java.util.List; @@ -167,4 +170,41 @@ public final class DeviceManagerUtil { return ctx.getTenantId(); } + public static String getServerBaseHttpsUrl() { + String hostName = "localhost"; + try { + hostName = NetworkUtils.getMgtHostName(); + } catch (Exception ignored) { + } + String mgtConsoleTransport = CarbonUtils.getManagementTransport(); + ConfigurationContextService configContextService = + DeviceManagementDataHolder.getInstance().getConfigurationContextService(); + int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport); + int httpsProxyPort = + CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), + mgtConsoleTransport); + if (httpsProxyPort > 0) { + port = httpsProxyPort; + } + return "https://" + hostName + ":" + port; + } + + public static String getServerBaseHttpUrl() { + String hostName = "localhost"; + try { + hostName = NetworkUtils.getMgtHostName(); + } catch (Exception ignored) { + } + ConfigurationContextService configContextService = + DeviceManagementDataHolder.getInstance().getConfigurationContextService(); + int port = CarbonUtils.getTransportPort(configContextService, "http"); + int httpProxyPort = + CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), + "http"); + if (httpProxyPort > 0) { + port = httpProxyPort; + } + return "http://" + hostName + ":" + port; + } + } diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/pom.xml b/components/email-sender/org.wso2.carbon.email.sender.core/pom.xml new file mode 100644 index 00000000000..910238f0ec2 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/pom.xml @@ -0,0 +1,159 @@ + + + + + + org.wso2.carbon.devicemgt + email-sender + 1.0.4-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.devicemgt + org.wso2.carbon.email.sender.core + 1.0.4-SNAPSHOT + bundle + WSO2 Carbon - Email Sender Core + WSO2 Carbon - Email Sender Core + http://wso2.org + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + 1.4.0 + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Email Management Core Bundle + org.wso2.carbon.email.sender.core.internal + + org.apache.axis2.*;version="${axis2.osgi.version.range}", + org.apache.axiom.*; version="${axiom.osgi.version.range}", + org.osgi.framework, + org.osgi.service.component, + org.apache.commons.logging, + javax.xml.*, + org.wso2.carbon.context, + org.wso2.carbon.utils.*, + org.wso2.carbon.registry.api, + org.w3c.dom, + org.apache.velocity, + org.apache.velocity.app, + org.apache.velocity.context, + org.apache.velocity.exception, + org.apache.velocity.runtime.resource, + org.apache.velocity.runtime.resource.loader, + org.apache.commons.io, + org.apache.axis2.transport.mail, + org.wso2.carbon.registry.core.service, + org.wso2.carbon.registry.core.session, + org.apache.commons.collections + + + !org.wso2.carbon.email.sender.core.internal, + org.wso2.carbon.email.sender.core.* + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18 + + + file:src/test/resources/log4j.properties + + + src/test/resources/testng.xml + + + + + + + + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + + + org.wso2.carbon + org.wso2.carbon.logging + + + org.wso2.carbon + org.wso2.carbon.utils + + + org.testng + testng + + + org.wso2.carbon + org.wso2.carbon.registry.api + + + org.wso2.carbon + org.wso2.carbon.base + + + org.apache.axis2.wso2 + axis2 + + + org.apache.velocity + velocity + + + commons-io.wso2 + commons-io + + + org.apache.axis2.transport + axis2-transport-mail + + + org.wso2.carbon + org.wso2.carbon.registry.core + + + commons-collections.wso2 + commons-collections + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/ContentProcessingInterruptedException.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProcessingInterruptedException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/ContentProcessingInterruptedException.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProcessingInterruptedException.java index e18db38211f..db54398a464 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/ContentProcessingInterruptedException.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProcessingInterruptedException.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; public class ContentProcessingInterruptedException extends Exception { diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProviderInfo.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProviderInfo.java new file mode 100644 index 00000000000..534f3f963d2 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/ContentProviderInfo.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, 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.email.sender.core; + +import java.util.Map; + +public class ContentProviderInfo { + + private String template; + private Map, Object>> params; + + public ContentProviderInfo(final String template, final Map, Object>> params) { + if (template == null || template.isEmpty()) { + throw new IllegalArgumentException("Template name cannot be null or empty"); + } + this.template = template; + if (params == null) { + throw new IllegalArgumentException("Place-holder parameter map cannot be null"); + } + this.params = params; + } + + public String getTemplate() { + return template; + } + + public Map, Object>> getParams() { + return params; + } + + public void addParam(String name, TypedValue, Object> param) { + params.put(name, param); + } + + public TypedValue, Object> getParam(String name) { + return params.get(name); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProvider.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProvider.java similarity index 95% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProvider.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProvider.java index bb1b48374b4..2f3ce81e54a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProvider.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProvider.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; import java.util.Map; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProviderFactory.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProviderFactory.java similarity index 94% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProviderFactory.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProviderFactory.java index eeaecb7515b..c9e13cf4d57 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailContentProviderFactory.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContentProviderFactory.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; public class EmailContentProviderFactory { diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContext.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContext.java new file mode 100644 index 00000000000..d6faede2f19 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailContext.java @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2016, 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.email.sender.core; + +import java.util.*; + +public class EmailContext { + + private Set recipients; + private Properties properties; + private ContentProviderInfo contentProviderInfo; + + private EmailContext(final ContentProviderInfo contentProviderInfo, final Set recipients, final Properties properties) { + if (contentProviderInfo == null) { + throw new IllegalArgumentException("Content provider information cannot be null"); + } + this.contentProviderInfo = contentProviderInfo; + if (recipients == null) { + throw new IllegalArgumentException("Recipient list cannot be null"); + } + if (recipients.size() == 0) { + throw new IllegalArgumentException("No recipient is configured. Recipient list should carry at " + + "least one recipient"); + } + this.recipients = recipients; + if (properties == null) { + throw new IllegalArgumentException("Email Context property bag cannot be null"); + } + this.properties = properties; + } + + private EmailContext(final ContentProviderInfo contentProviderInfo, final String recipient, final Properties properties) { + if (contentProviderInfo == null) { + throw new IllegalArgumentException("Content provider information cannot be null"); + } + this.contentProviderInfo = contentProviderInfo; + if (recipient == null || recipient.isEmpty()) { + throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " + + "recipient email address"); + } + this.recipients = new HashSet() {{ + add(recipient); + }}; + if (properties == null) { + throw new IllegalArgumentException("Email Context property bag cannot be null"); + } + this.properties = properties; + } + + public Set getRecipients() { + return recipients; + } + + public Properties getProperties() { + return properties; + } + + public String getProperty(String name) { + return (String) properties.get(name); + } + + public void addProperty(String name, String value) { + properties.put(name, value); + } + + public ContentProviderInfo getContentProviderInfo() { + return contentProviderInfo; + } + + public static class EmailContextBuilder { + + private Set recipients; + private ContentProviderInfo contentProviderInfo; + private Properties properties; + + public EmailContextBuilder(final ContentProviderInfo contentProviderInfo, Set recipients) { + if (contentProviderInfo == null) { + throw new IllegalArgumentException("Content provider information cannot be null"); + } + this.contentProviderInfo = contentProviderInfo; + if (recipients == null) { + throw new IllegalArgumentException("Recipient list cannot be null"); + } + if (recipients.size() == 0) { + throw new IllegalArgumentException("No recipient is configured. Recipient list should carry at " + + "least one recipient"); + } + this.recipients = recipients; + this.properties = new Properties(); + } + + public EmailContextBuilder(final ContentProviderInfo contentProviderInfo, final String recipient, + final Properties properties) { + if (contentProviderInfo == null) { + throw new IllegalArgumentException("Content provider information cannot be null"); + } + this.contentProviderInfo = contentProviderInfo; + if (recipient == null || recipient.isEmpty()) { + throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " + + "recipient email address"); + } + this.recipients = new HashSet() {{ + add(recipient); + }}; + if (properties == null) { + throw new IllegalArgumentException("Email Context property bag cannot be null"); + } + this.properties = properties; + } + + public EmailContextBuilder addProperty(String name, String value) { + properties.setProperty(name, value); + return this; + } + + public EmailContext build() { + return new EmailContext(contentProviderInfo, recipients, properties); + } + + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailData.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailData.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailData.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailData.java index 6ef02585d92..128d400a575 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailData.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailData.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfig.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfig.java new file mode 100644 index 00000000000..5c2104fb89d --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfig.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2016, 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.email.sender.core; + +import org.w3c.dom.Document; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.File; + +@XmlRootElement(name = "EmailSenderConfig") +public class EmailSenderConfig { + + private int minThreads; + private int maxThreads; + private int keepAliveDuration; + private int threadQueueCapacity; + + private static EmailSenderConfig config; + + private static final String EMAIL_SENDER_CONFIG_PATH = + CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "email-sender-config.xml"; + + private EmailSenderConfig() { + } + + public static EmailSenderConfig getInstance() { + if (config == null) { + throw new InvalidConfigurationStateException("Webapp Authenticator Configuration is not " + + "initialized properly"); + } + return config; + } + + @XmlElement(name = "MinThreads", required = true) + public int getMinThreads() { + return minThreads; + } + + public void setMinThreads(int minThreads) { + this.minThreads = minThreads; + } + + @XmlElement(name = "MaxThreads", required = true) + public int getMaxThreads() { + return maxThreads; + } + + public void setMaxThreads(int maxThreads) { + this.maxThreads = maxThreads; + } + + @XmlElement(name = "KeepAliveDuration", required = true) + public int getKeepAliveDuration() { + return keepAliveDuration; + } + + public void setKeepAliveDuration(int keepAliveDuration) { + this.keepAliveDuration = keepAliveDuration; + } + @XmlElement(name = "ThreadQueueCapacity", required = true) + public int getThreadQueueCapacity() { + return threadQueueCapacity; + } + + public void setThreadQueueCapacity(int threadQueueCapacity) { + this.threadQueueCapacity = threadQueueCapacity; + } + + public static void init() throws EmailSenderConfigurationFailedException { + try { + File emailSenderConfig = new File(EMAIL_SENDER_CONFIG_PATH); + Document doc = EmailSenderUtil.convertToDocument(emailSenderConfig); + + /* Un-marshaling Email Sender configuration */ + JAXBContext ctx = JAXBContext.newInstance(EmailSenderConfig.class); + Unmarshaller unmarshaller = ctx.createUnmarshaller(); + //unmarshaller.setSchema(getSchema()); + config = (EmailSenderConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while un-marshalling Email " + + "Sender Config", e); + } + } + +} diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfigurationFailedException.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfigurationFailedException.java new file mode 100644 index 00000000000..448ed2b64ec --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderConfigurationFailedException.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016, 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.email.sender.core; + +public class EmailSenderConfigurationFailedException extends Exception { + + private static final long serialVersionUID = -3151279312929070298L; + + public EmailSenderConfigurationFailedException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public EmailSenderConfigurationFailedException(String message, Throwable cause) { + super(message, cause); + } + + public EmailSenderConfigurationFailedException(String msg) { + super(msg); + } + + public EmailSenderConfigurationFailedException() { + super(); + } + + public EmailSenderConfigurationFailedException(Throwable cause) { + super(cause); + } + +} diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderUtil.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderUtil.java new file mode 100644 index 00000000000..c1f03a02bc1 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSenderUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016, 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.email.sender.core; + +import org.w3c.dom.Document; +import org.wso2.carbon.email.sender.core.internal.EmailSenderDataHolder; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.ConfigurationContextService; +import org.wso2.carbon.utils.NetworkUtils; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +public class EmailSenderUtil { + + public static Document convertToDocument(File file) throws EmailSenderConfigurationFailedException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new EmailSenderConfigurationFailedException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailSendingFailedException.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSendingFailedException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailSendingFailedException.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSendingFailedException.java index e0457259f74..1f9fd9bef1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/EmailSendingFailedException.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailSendingFailedException.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; public class EmailSendingFailedException extends Exception { diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/InvalidConfigurationStateException.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/InvalidConfigurationStateException.java new file mode 100644 index 00000000000..606503acfb2 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/InvalidConfigurationStateException.java @@ -0,0 +1,74 @@ +/* + * 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.email.sender.core; + +public class InvalidConfigurationStateException extends RuntimeException { + + private static final long serialVersionUID = -3151279311329070297L; + + private String errorMessage; + private int errorCode; + + public InvalidConfigurationStateException(int errorCode, String message) { + super(message); + this.errorCode = errorCode; + } + + public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) { + super(message, cause); + this.errorCode = errorCode; + } + + public int getErrorCode() { + return errorCode; + } + + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public InvalidConfigurationStateException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public InvalidConfigurationStateException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public InvalidConfigurationStateException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public InvalidConfigurationStateException() { + super(); + } + + public InvalidConfigurationStateException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/RegistryBasedResourceLoader.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java similarity index 98% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/RegistryBasedResourceLoader.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java index 9d035e4a458..7ba350e07c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/RegistryBasedResourceLoader.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; import org.apache.commons.collections.ExtendedProperties; import org.apache.velocity.exception.ResourceNotFoundException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/TypedValue.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/TypedValue.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/TypedValue.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/TypedValue.java index 11b5c2ed0ab..616ff441884 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/TypedValue.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/TypedValue.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; public class TypedValue { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/VelocityBasedEmailContentProvider.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/VelocityBasedEmailContentProvider.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/VelocityBasedEmailContentProvider.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/VelocityBasedEmailContentProvider.java index a35d5c448d0..156dc4dae64 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/VelocityBasedEmailContentProvider.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/VelocityBasedEmailContentProvider.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.email; +package org.wso2.carbon.email.sender.core; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -47,7 +47,7 @@ public class VelocityBasedEmailContentProvider implements EmailContentProvider { engine.setProperty("resource.loader", "registry"); engine.setProperty("velocimacro.library", ""); engine.setProperty("registry.resource.loader.class", - "org.wso2.carbon.device.mgt.core.email.RegistryBasedResourceLoader"); + "org.wso2.carbon.email.sender.core.RegistryBasedResourceLoader"); engine.init(); } diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java new file mode 100644 index 00000000000..19a73495961 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java @@ -0,0 +1,111 @@ +/* + * 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.email.sender.core.internal; + +import org.apache.axis2.context.ConfigurationContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.RegistryType; +import org.wso2.carbon.email.sender.core.EmailSenderConfigurationFailedException; +import org.wso2.carbon.registry.api.Collection; +import org.wso2.carbon.registry.api.Registry; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; +import org.wso2.carbon.utils.CarbonUtils; + +import java.io.File; +import java.io.FilenameFilter; + +public class EmailSenderAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver { + + private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "email-templates"; + private static final Log log = LogFactory.getLog(EmailSenderAxis2ConfigContextObserver.class); + + @Override + public void creatingConfigurationContext(int tenantId) { + + } + + @Override + public void createdConfigurationContext(ConfigurationContext configurationContext) { + try { + this.setupEmailTemplates(); + } catch (EmailSenderConfigurationFailedException e) { + log.error("Error occurred while setting up email templates", e); + } + } + + @Override + public void terminatingConfigurationContext(ConfigurationContext configurationContext) { + + } + + @Override + public void terminatedConfigurationContext(ConfigurationContext configurationContext) { + + } + + private void setupEmailTemplates() throws EmailSenderConfigurationFailedException { + File templateDir = + new File(CarbonUtils.getCarbonRepository() + "resources" + File.separator + "email-templates"); + if (!templateDir.exists()) { + if (log.isDebugEnabled()) { + log.debug("The directory that is expected to use as the container for all email templates is not " + + "available. Therefore, no template is uploaded to the registry"); + } + } + if (templateDir.canRead()) { + File[] templates = templateDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + name = name.toLowerCase(); + return name.endsWith(".vm"); + } + }); + try { + Registry registry = + CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); + if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { + Collection collection = registry.newCollection(); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); + for (File template : templates) { + Resource resource = registry.newResource(); + resource.setContent(template); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); + } + } else { + for (File template : templates) { + if (!registry.resourceExists( + EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName())) { + Resource resource = registry.newResource(); + resource.setContent(template); + registry.put( + EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); + } + } + } + } catch (RegistryException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e); + } + } + } + +} diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderDataHolder.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderDataHolder.java new file mode 100644 index 00000000000..fa7c4ee4515 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderDataHolder.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014, 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.email.sender.core.internal; + +import org.wso2.carbon.email.sender.core.service.EmailSenderService; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.utils.ConfigurationContextService; + +public class EmailSenderDataHolder { + + private RegistryService registryService; + private ConfigurationContextService configurationContextService; + private EmailSenderService emailServiceProvider; + + private static EmailSenderDataHolder thisInstance = new EmailSenderDataHolder(); + + private EmailSenderDataHolder() {} + + public static EmailSenderDataHolder getInstance() { + return thisInstance; + } + + public RegistryService getRegistryService() { + if (registryService == null) { + throw new IllegalStateException("Registry service is not initialized properly"); + } + return registryService; + } + + public void setRegistryService(RegistryService registryService) { + this.registryService = registryService; + } + + public ConfigurationContextService getConfigurationContextService() { + if (configurationContextService == null) { + throw new IllegalStateException("ConfigurationContext service is not initialized properly"); + } + return configurationContextService; + } + + public void setConfigurationContextService(ConfigurationContextService configurationContextService) { + this.configurationContextService = configurationContextService; + } + + public EmailSenderService getEmailServiceProvider() { + return emailServiceProvider; + } + + public void setEmailServiceProvider(EmailSenderService emailServiceProvider) { + this.emailServiceProvider = emailServiceProvider; + } + +} diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java new file mode 100644 index 00000000000..b166c5861bd --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2014, 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.email.sender.core.internal; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.email.sender.core.EmailSenderConfig; +import org.wso2.carbon.email.sender.core.EmailSenderConfigurationFailedException; +import org.wso2.carbon.email.sender.core.service.EmailSenderService; +import org.wso2.carbon.email.sender.core.service.EmailSenderServiceImpl; +import org.wso2.carbon.registry.api.Collection; +import org.wso2.carbon.registry.api.Registry; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.ConfigurationContextService; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FilenameFilter; +import java.io.IOException; + +/** + * @scr.component name="org.wso2.carbon.email.sender.EmailSenderServiceComponent" immediate="true" + * @scr.reference name="registry.service" + * interface="org.wso2.carbon.registry.core.service.RegistryService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRegistryService" + * unbind="unsetRegistryService" + * @scr.reference name="config.context.service" + * interface="org.wso2.carbon.utils.ConfigurationContextService" + * cardinality="0..1" + * policy="dynamic" + * bind="setConfigurationContextService" + * unbind="unsetConfigurationContextService" + */ +public class EmailSenderServiceComponent { + + private static Log log = LogFactory.getLog(EmailSenderServiceComponent.class); + + private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "/email-templates"; + + @SuppressWarnings("unused") + protected void activate(ComponentContext componentContext) { + try { + if (log.isDebugEnabled()) { + log.debug("Initializing email sender core bundle"); + } + /* Initializing email sende configuration */ + EmailSenderConfig.init(); + + /* Setting up default email templates */ + this.setupEmailTemplates(); + + /* Registering declarative service instances exposed by EmailSenderServiceComponent */ + this.registerServices(componentContext); + + if (log.isDebugEnabled()) { + log.debug("Email sender core bundle has been successfully initialized"); + } + } catch (Throwable e) { + log.error("Error occurred while initializing email sender core bundle", e); + } + } + + @SuppressWarnings("unused") + protected void deactivate(ComponentContext componentContext) { + //do nothing + } + + private void registerServices(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("Registering email sender service"); + } + EmailSenderService emailServiceProvider = new EmailSenderServiceImpl(); + EmailSenderDataHolder.getInstance().setEmailServiceProvider(emailServiceProvider); + componentContext.getBundleContext().registerService(EmailSenderService.class, emailServiceProvider, null); + } + + private void setupEmailTemplates() throws EmailSenderConfigurationFailedException { + File templateDir = + new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + + "resources" + File.separator + "email-templates"); + if (!templateDir.exists()) { + if (log.isDebugEnabled()) { + log.debug("The directory that is expected to use as the container for all email templates is not " + + "available. Therefore, no template is uploaded to the registry"); + } + } + if (templateDir.canRead()) { + File[] templates = templateDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + name = name.toLowerCase(); + return name.endsWith(".vm"); + } + }); + try { + Registry registry = + EmailSenderDataHolder.getInstance().getRegistryService().getConfigSystemRegistry(); + if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { + Collection collection = registry.newCollection(); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); + for (File template : templates) { + Resource resource = registry.newResource(); + String contents = FileUtils.readFileToString(template); + resource.setContent(contents.getBytes()); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); + } + } else { + /* Existence of a given resource is not checked consciously, before performing registry.put() below. + * The rationale is that, the only less expensive way that one can check if a resource exists is + * that through registry.resourceExists(), which only checks if 'some' resource exists at the given + * registry path. However, this does not capture scenarios where there can be updated contents to + * the same resource of which the path hasn't changed after it has been initialized for the first + * time. Therefore, whenever the server starts-up, all email templates are updated just to avoid + * the aforementioned problem */ + for (File template : templates) { + Resource resource = registry.newResource(); + String contents = FileUtils.readFileToString(template); + resource.setContent(contents.getBytes()); + registry.put( + EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); + } + } + } catch (RegistryException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e); + } catch (FileNotFoundException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while writing template file " + + "contents as an input stream of a resource", e); + } catch (IOException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while serializing file " + + "contents to a string", e); + } + } + } + + /** + * Sets Registry Service. + * + * @param registryService An instance of RegistryService + */ + protected void setRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("Setting Registry Service"); + } + EmailSenderDataHolder.getInstance().setRegistryService(registryService); + } + + /** + * Unsets Registry Service. + * + * @param registryService An instance of RegistryService + */ + protected void unsetRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("Un setting Registry Service"); + } + EmailSenderDataHolder.getInstance().setRegistryService(null); + } + + protected void setConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Setting ConfigurationContextService"); + } + EmailSenderDataHolder.getInstance().setConfigurationContextService(configurationContextService); + } + + protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Un-setting ConfigurationContextService"); + } + EmailSenderDataHolder.getInstance().setConfigurationContextService(null); + } + +} 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/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java similarity index 66% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/EmailService.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java index c1d0d2da2a5..8bde6c3bdfb 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/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java @@ -15,15 +15,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.wso2.carbon.device.mgt.core.service; +package org.wso2.carbon.email.sender.core.service; -import org.wso2.carbon.device.mgt.core.email.EmailData; -import org.wso2.carbon.device.mgt.core.email.EmailSendingFailedException; +import org.wso2.carbon.email.sender.core.EmailContext; +import org.wso2.carbon.email.sender.core.EmailSendingFailedException; -import java.util.Set; +public interface EmailSenderService { -public interface EmailService { - - void sendEmail(Set recipients, EmailData emailData) throws EmailSendingFailedException; + void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java similarity index 71% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java rename to components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java index cad75c266c4..393350445f7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.core.email.sender; +package org.wso2.carbon.email.sender.core.service; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.llom.util.AXIOMUtil; @@ -30,42 +30,47 @@ import org.apache.axis2.context.MessageContext; import org.apache.axis2.transport.mail.MailConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations; -import org.wso2.carbon.device.mgt.core.email.EmailData; -import org.wso2.carbon.device.mgt.core.email.EmailSendingFailedException; -import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; -import org.wso2.carbon.device.mgt.core.service.EmailService; +import org.wso2.carbon.email.sender.core.*; +import org.wso2.carbon.email.sender.core.internal.EmailSenderDataHolder; import org.wso2.carbon.utils.ConfigurationContextService; import javax.xml.stream.XMLStreamException; import java.util.HashMap; import java.util.Map; -import java.util.Set; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -public class EmailServiceProviderImpl implements EmailService { +public class EmailSenderServiceImpl implements EmailSenderService { private static ThreadPoolExecutor threadPoolExecutor; + private EmailContentProvider contentProvider; static { - EmailConfigurations emailConfig = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getDeviceManagementConfigRepository().getEmailConfigurations(); - - threadPoolExecutor = new ThreadPoolExecutor(emailConfig.getMinNumOfThread(), - emailConfig.getMaxNumOfThread(), emailConfig.getKeepAliveTime(), TimeUnit.SECONDS, - new LinkedBlockingQueue(emailConfig.getThreadQueueCapacity())); + EmailSenderConfig config = EmailSenderConfig.getInstance(); + threadPoolExecutor = new ThreadPoolExecutor(config.getMinThreads(), config.getMaxThreads(), + config.getKeepAliveDuration(), TimeUnit.SECONDS, + new LinkedBlockingQueue(config.getThreadQueueCapacity())); } private static final String EMAIL_URI_SCHEME = "mailto:"; - private static Log log = LogFactory.getLog(EmailServiceProviderImpl.class); + private static Log log = LogFactory.getLog(EmailSenderServiceImpl.class); + + public EmailSenderServiceImpl() { + this.contentProvider = EmailContentProviderFactory.getContentProvider(); + } @Override - public void sendEmail(Set recipients, EmailData emailData) throws EmailSendingFailedException { - for (String recipient : recipients) { + public void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException { + for (String recipient : emailCtx.getRecipients()) { + ContentProviderInfo info = emailCtx.getContentProviderInfo(); + EmailData emailData; + try { + emailData = contentProvider.getContent(info.getTemplate(), info.getParams()); + } catch (ContentProcessingInterruptedException e) { + throw new EmailSendingFailedException("Error occurred while retrieving email content to be " + + "sent for recipient '" + recipient + "'", e); + } threadPoolExecutor.submit(new EmailSender(recipient, emailData.getSubject(), emailData.getBody())); } } @@ -91,7 +96,7 @@ public class EmailServiceProviderImpl implements EmailService { } try { ConfigurationContextService configCtxService = - EmailServiceDataHolder.getInstance().getConfigurationContextService(); + EmailSenderDataHolder.getInstance().getConfigurationContextService(); if (configCtxService == null) { throw new IllegalStateException("Configuration Context Service is not available"); } diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/log4j.properties b/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/log4j.properties new file mode 100644 index 00000000000..dc3d465fc0c --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/log4j.properties @@ -0,0 +1,32 @@ +# +# Copyright 2009 WSO2, Inc. (http://wso2.com) +# +# Licensed 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. +# + +# +# This is the log4j configuration file used by WSO2 Carbon +# +# IMPORTANT : Please do not remove or change the names of any +# of the Appenders defined here. The layout pattern & log file +# can be changed using the WSO2 Carbon Management Console, and those +# settings will override the settings in this file. +# + +log4j.rootLogger=INFO, STD_OUT + +# Redirect log messages to console +log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender +log4j.appender.STD_OUT.Target=System.out +log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout +log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/testng.xml b/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/testng.xml new file mode 100644 index 00000000000..7b13a25f89b --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/test/resources/testng.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/velocity.log b/components/email-sender/org.wso2.carbon.email.sender.core/velocity.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/components/email-sender/pom.xml b/components/email-sender/pom.xml new file mode 100644 index 00000000000..b1cbb67385a --- /dev/null +++ b/components/email-sender/pom.xml @@ -0,0 +1,60 @@ + + + + + + + org.wso2.carbon.devicemgt + carbon-devicemgt + 1.0.4-SNAPSHOT + ../../pom.xml + + + 4.0.0 + org.wso2.carbon.devicemgt + email-sender + 1.0.4-SNAPSHOT + pom + WSO2 Carbon - Email Sender Component + http://wso2.org + + + org.wso2.carbon.email.sender.core + + + + + + + org.apache.felix + maven-scr-plugin + 1.7.2 + + + generate-scr-scrdescriptor + + scr + + + + + + + + diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml index fec4d783f66..3b8968bb278 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml @@ -115,6 +115,7 @@ org.wso2.carbon.core.server:${carbon.kernel.version} org.wso2.carbon.apimgt.core:${carbon.api.mgt.version} org.wso2.carbon.device.mgt.extensions:${carbon.device.mgt.version} + org.wso2.carbon.email.sender:${carbon.device.mgt.version} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 78c5aef8684..98aad0d61df 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -24,14 +24,6 @@ jdbc/DM_DS - - 8 - 100 - 20 - 1000 - https://localhost:9443 - /emm-web-agent/enrollment - https://localhost:9443 admin 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 deleted file mode 100644 index eabecf785b4..00000000000 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - Enroll your Device with WSO2 MDM -
Dear {first-name},
- - You have been registered to the WSO2 MDM. Below is the link to enroll. - - {downloadUrl} - - Best Regards, - - - WSO2 MDM Team. - - - http://www.wso2.com - -
- - Enroll your Device with WSO2 MDM -
Dear {first-name},
- -You have been registered to WSO2 MDM with following credentials. -Domain: {domain-name} -Username: {user-name} -Password: {password} -Below is the link to enroll. - - {downloadUrl} - - Best Regards, - - - WSO2 MDM Team. - - - http://www.wso2.com - -
-
\ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-enrollment.vm b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-enrollment.vm index a02931564e8..25b3b26a0c8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-enrollment.vm +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-enrollment.vm @@ -1,3 +1,20 @@ +#* + Copyright (c) 2016, 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. +*# You have been invited to enroll your device in WSO2 EMM @@ -22,7 +39,7 @@

You have been invited to enrol your device in WSO2 Enterprise Mobility Manager. - Click here to download the WSO2 EMM client application to begin device + Click here to download the WSO2 EMM client application to begin device enrolment.

diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-registration.vm b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-registration.vm index 1147bf12140..56e95625e06 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-registration.vm +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/email/templates/user-registration.vm @@ -1,3 +1,20 @@ +#* + Copyright (c) 2016, 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. +*# You have successfully been registered in WSO2 EMM @@ -22,7 +39,7 @@

You have been registered in WSO2 Enterprise Mobility Manager and invited to enrol your device. - Click here to download the WSO2 EMM client application to begin device + Click here to download the WSO2 EMM client application to begin device enrolment.

diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index e77945f6395..63d198338f8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -1,6 +1,5 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ -org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/notification-messages.xml,target:${installFolder}/../../conf/notification-messages.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/license-config.xml,target:${installFolder}/../../conf/etc/license-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/remote-appmanager-config.xml,target:${installFolder}/../../conf/etc/remote-appmanager-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ diff --git a/features/email-sender/org.wso2.carbon.email.sender.feature/pom.xml b/features/email-sender/org.wso2.carbon.email.sender.feature/pom.xml new file mode 100644 index 00000000000..d368589daf6 --- /dev/null +++ b/features/email-sender/org.wso2.carbon.email.sender.feature/pom.xml @@ -0,0 +1,107 @@ + + + + + + + org.wso2.carbon.devicemgt + email-sender-feature + 1.0.4-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.email.sender.feature + pom + 1.0.4-SNAPSHOT + WSO2 Carbon - Email Sender Feature + http://wso2.org + This feature contains the core bundles required for email sender related functionality + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.email.sender.core + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.email.sender + ../../../features/etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + org.eclipse.equinox.p2.type.group:false + + + + + org.wso2.carbon.devicemgt:org.wso2.carbon.email.sender.core:${carbon.device.mgt.version} + + + + org.wso2.carbon.core.server:${carbon.kernel.version} + + + + + + + + diff --git a/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/build.properties b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/build.properties new file mode 100644 index 00000000000..9c86577d768 --- /dev/null +++ b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom = true diff --git a/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/conf/email-sender-config.xml b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/conf/email-sender-config.xml new file mode 100644 index 00000000000..996cfacc2cb --- /dev/null +++ b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/conf/email-sender-config.xml @@ -0,0 +1,30 @@ + + + + + 8 + + 100 + + 20 + + 1000 + + + diff --git a/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/default.vm b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/default.vm new file mode 100644 index 00000000000..25e3f880ca9 --- /dev/null +++ b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/email/templates/default.vm @@ -0,0 +1,34 @@ +#* + Copyright (c) 2016, 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. +*# + + Hi There! + + + + Default Email Sender Template + + +

+ This template has to be customized to fit into any target context +
+ + + ]]> + +
diff --git a/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/p2.inf b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/p2.inf new file mode 100644 index 00000000000..d4dadf6a1e6 --- /dev/null +++ b/features/email-sender/org.wso2.carbon.email.sender.feature/src/main/resources/p2.inf @@ -0,0 +1,3 @@ +instructions.configure = \ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.email.sender_${feature.version}/conf/email-sender-config.xml,target:${installFolder}/../../conf/etc/email-sender-config.xml,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.email.sender_${feature.version}/email/templates,target:${installFolder}/../../../repository/resources/email-templates,overwrite:true);\ \ No newline at end of file diff --git a/features/email-sender/pom.xml b/features/email-sender/pom.xml new file mode 100644 index 00000000000..c404dced59b --- /dev/null +++ b/features/email-sender/pom.xml @@ -0,0 +1,41 @@ + + + + + + + org.wso2.carbon.devicemgt + carbon-devicemgt + 1.0.4-SNAPSHOT + ../../pom.xml + + + 4.0.0 + org.wso2.carbon.devicemgt + email-sender-feature + 1.0.4-SNAPSHOT + pom + WSO2 Carbon - Email Sender Feature + http://wso2.org + + + org.wso2.carbon.email.sender.feature + + + diff --git a/pom.xml b/pom.xml index 45ff421ffd3..87d87d5064a 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ components/certificate-mgt components/webapp-authenticator-framework components/identity-extensions + components/email-sender features/device-mgt features/apimgt-extensions features/policy-mgt @@ -48,6 +49,7 @@ features/certificate-mgt features/dynamic-client-registration features/oauth-extensions + features/email-sender @@ -251,6 +253,11 @@ org.wso2.carbon.identity.authenticator.backend.oauth ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt + org.wso2.carbon.email.sender.core + ${carbon.device.mgt.version} + @@ -1282,6 +1289,11 @@ commons-io ${commons-io.version} + + commons-collections.wso2 + commons-collections + ${commons-collections.version} + @@ -1398,6 +1410,11 @@ maven-surefire-plugin 2.18 + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + @@ -1586,6 +1603,7 @@ 3.1.0.wso2v2 1.7 2.4.0.wso2v1 + 3.2.0.wso2v1