From bbbd199cf7cd3f9442fe4ee732ea06f5a4adaf28 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Fri, 1 Sep 2017 20:30:37 +0530 Subject: [PATCH] Resolved wso2/product-iots#1035 --- .../mgt/jaxrs/beans/BasicUserInfoWrapper.java | 54 ++++++++++++ .../impl/UserManagementServiceImpl.java | 23 +++++- .../DeviceManagementProviderService.java | 6 +- .../DeviceManagementProviderServiceImpl.java | 82 +++++++++++-------- .../pages/cdmf.page.user.create/create.hbs | 5 ++ .../public/js/bottomJs.js | 8 +- .../EmailTransportNotConfiguredException.java | 44 ++++++++++ .../core/service/EmailSenderService.java | 3 +- .../core/service/EmailSenderServiceImpl.java | 41 ++++++---- 9 files changed, 212 insertions(+), 54 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoWrapper.java create mode 100644 components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailTransportNotConfiguredException.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoWrapper.java new file mode 100644 index 00000000000..651b4766b33 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoWrapper.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2017, 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.jaxrs.beans; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(value = "BasicUserInfoWrapper", description = "This contains basic details of a set of users that matches " + + "a given criteria as a collection and a message if there's any.") +public class BasicUserInfoWrapper { + + @ApiModelProperty( + name = "basicUserInfo", + value = "Details of the User.", + required = true) + private BasicUserInfo basicUserInfo; + + @ApiModelProperty( + name = "message", + value = "Response message if there's any.") + private String message; + + public BasicUserInfo getBasicUserInfo() { + return basicUserInfo; + } + + public void setBasicUserInfo(BasicUserInfo basicUserInfo) { + this.basicUserInfo = basicUserInfo; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index e8272f6f88a..59977eb3494 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -25,11 +25,13 @@ import org.eclipse.wst.common.uriresolver.internal.util.URIEncoder; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfo; import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfoList; +import org.wso2.carbon.device.mgt.jaxrs.beans.BasicUserInfoWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.EnrollmentInvitation; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; @@ -155,10 +157,19 @@ public class UserManagementServiceImpl implements UserManagementService { props.setProperty("password", initialUserPassword); EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props); - dms.sendRegistrationEmail(metaInfo); + BasicUserInfoWrapper userInfoWrapper = new BasicUserInfoWrapper(); + String message; + try { + dms.sendRegistrationEmail(metaInfo); + message = "An invitation mail will be sent to this user to initiate device enrollment."; + } catch (ConfigurationManagementException e) { + message = "Mail Server is not configured. Email invitation will not be sent."; + } + userInfoWrapper.setBasicUserInfo(createdUserInfo); + userInfoWrapper.setMessage(message); return Response.created(new URI(API_BASE_PATH + "/" + URIEncoder.encode(userInfo.getUsername(), "UTF-8"))) .entity( - createdUserInfo).build(); + userInfoWrapper).build(); } catch (UserStoreException e) { String msg = "Error occurred while trying to add user '" + userInfo.getUsername() + "' to the " + "underlying user management system"; @@ -573,6 +584,10 @@ public class UserManagementServiceImpl implements UserManagementService { log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (ConfigurationManagementException e) { + String msg = "Error occurred while sending the email invitations. Mail server not configured."; + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); } @@ -608,6 +623,10 @@ public class UserManagementServiceImpl implements UserManagementService { log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (ConfigurationManagementException e) { + String msg = "Error occurred while sending the email invitations. Mail server not configured."; + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build(); } 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 e52d7f38c31..836fc38fac0 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 @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.MonitoringOperation; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; @@ -436,9 +437,10 @@ public interface DeviceManagementProviderService { HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; - void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException; + void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, + ConfigurationManagementException; - void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException; + void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; FeatureManager getFeatureManager(String deviceType) 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 d5f8707d4e8..4b90e8a16d3 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,9 +28,6 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; -import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; -import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; -import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.InitialOperationConfig; @@ -41,6 +38,7 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; @@ -55,6 +53,8 @@ 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.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; +import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; +import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; @@ -69,6 +69,7 @@ import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; 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.PluginInitializationListener; @@ -77,7 +78,9 @@ 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.EmailTransportNotConfiguredException; import org.wso2.carbon.email.sender.core.TypedValue; +import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.user.api.UserStoreException; import java.sql.SQLException; @@ -769,7 +772,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException { + public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, + ConfigurationManagementException { if (metaInfo == null) { String msg = "Received incomplete data to method sendEnrolmentInvitation"; log.error(msg); @@ -798,6 +802,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String msg = "Error occurred while sending enrollment invitation"; log.error(msg, ex); throw new DeviceManagementException(msg, ex); + } catch (EmailTransportNotConfiguredException ex) { + String msg = "Mail Server is not configured."; + throw new ConfigurationManagementException(msg, ex); } catch (Exception ex) { String msg = "Error occurred in setEnrollmentInvitation"; log.error(msg, ex); @@ -806,7 +813,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException { + public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException, + ConfigurationManagementException { if (metaInfo == null) { String msg = "Received incomplete request for sendRegistrationEmail"; log.error(msg); @@ -815,35 +823,41 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (log.isDebugEnabled()) { log.debug("Send registration email"); } - Map, Object>> params = new HashMap<>(); - 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 { - EmailContext ctx = - new EmailContext.EmailContextBuilder( - new ContentProviderInfo( - DeviceManagementConstants.EmailAttributes.USER_REGISTRATION_TEMPLATE, - params), - metaInfo.getRecipients()).build(); - DeviceManagementDataHolder.getInstance().getEmailSenderService().sendEmail(ctx); - } catch (EmailSendingFailedException e) { - String msg = "Error occurred while sending user registration notification." + e.getMessage(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in sendRegistrationEmail"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); + EmailSenderService emailSenderService = DeviceManagementDataHolder.getInstance().getEmailSenderService(); + if (emailSenderService != null) { + Map, Object>> params = new HashMap<>(); + 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 { + EmailContext ctx = + new EmailContext.EmailContextBuilder( + new ContentProviderInfo( + DeviceManagementConstants.EmailAttributes.USER_REGISTRATION_TEMPLATE, + params), + metaInfo.getRecipients()).build(); + emailSenderService.sendEmail(ctx); + } catch (EmailSendingFailedException e) { + String msg = "Error occurred while sending user registration notification." + e.getMessage(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (EmailTransportNotConfiguredException e) { + String msg = "Error occurred while sending user registration email." + e.getMessage(); + throw new ConfigurationManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred while sending Registration Email."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/create.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/create.hbs index 183e124bfa9..6dc712ad2cd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/create.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/create.hbs @@ -131,6 +131,11 @@ An invitation mail will be sent to this user to initiate device enrollment. + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/public/js/bottomJs.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/public/js/bottomJs.js index 28d4af3fdb2..236ee109831 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/public/js/bottomJs.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.user.create/public/js/bottomJs.js @@ -272,6 +272,7 @@ $(document).ready(function () { addUserFormData, function (data, textStatus, jqXHR) { if (jqXHR.status == 201) { + var response = JSON.parse(data); // Clearing user input fields. $("input#username").val(""); $("input#firstname").val(""); @@ -284,7 +285,12 @@ $(document).ready(function () { $("#user-create-form").addClass("hidden"); modalDialog.header('' + ' User was added successfully'); - modalDialog.content($("#modal-content-user-created").html()); + if (response.message) { + $("#modal-content-user-created-with-message").append("

" + response.message + "

"); + modalDialog.content($("#modal-content-user-created-with-message").html()); + } else { + modalDialog.content($("#modal-content-user-created").html()); + } modalDialog.footer('
' + ' OK' + '
'); diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailTransportNotConfiguredException.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailTransportNotConfiguredException.java new file mode 100644 index 00000000000..9b753723895 --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/EmailTransportNotConfiguredException.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017, 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 EmailTransportNotConfiguredException extends Exception { + + private static final long serialVersionUID = -3151279311929070294L; + + public EmailTransportNotConfiguredException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public EmailTransportNotConfiguredException(String message, Throwable cause) { + super(message, cause); + } + + public EmailTransportNotConfiguredException(String msg) { + super(msg); + } + + public EmailTransportNotConfiguredException() { + super(); + } + + public EmailTransportNotConfiguredException(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/service/EmailSenderService.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java index 8bde6c3bdfb..de84b7aee28 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderService.java @@ -19,9 +19,10 @@ package org.wso2.carbon.email.sender.core.service; import org.wso2.carbon.email.sender.core.EmailContext; import org.wso2.carbon.email.sender.core.EmailSendingFailedException; +import org.wso2.carbon.email.sender.core.EmailTransportNotConfiguredException; public interface EmailSenderService { - void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException; + void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException, EmailTransportNotConfiguredException; } diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java index 8e129a7ed1d..93b2c2a06a4 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java @@ -45,6 +45,7 @@ public class EmailSenderServiceImpl implements EmailSenderService { private static ThreadPoolExecutor threadPoolExecutor; private EmailContentProvider contentProvider; + private static final String TRANSPORT_SENDER_NAME = "mailto"; static { EmailSenderConfig config = EmailSenderConfig.getInstance(); @@ -60,23 +61,35 @@ public class EmailSenderServiceImpl implements EmailSenderService { this.contentProvider = EmailContentProviderFactory.getContentProvider(); } + private boolean isMailServerConfigured() { + if(EmailSenderDataHolder.getInstance().getConfigurationContextService() + .getServerConfigContext().getAxisConfiguration().getTransportOut(TRANSPORT_SENDER_NAME) != null) { + return true; + } else { + return false; + } + } + @Override - public void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException { - for (String recipient : emailCtx.getRecipients()) { - ContentProviderInfo info = emailCtx.getContentProviderInfo(); - EmailData emailData; - String transportSenderName = "mailto"; - try { - emailData = contentProvider.getContent(info.getTemplate(), info.getParams()); - if(EmailSenderDataHolder.getInstance().getConfigurationContextService() - .getServerConfigContext().getAxisConfiguration().getTransportOut(transportSenderName) == null){ - throw new EmailSendingFailedException("Email transport is not configured."); + public void sendEmail(EmailContext emailCtx) throws EmailSendingFailedException, + EmailTransportNotConfiguredException { + if (this.isMailServerConfigured()) { + for (String recipient : emailCtx.getRecipients()) { + ContentProviderInfo info = emailCtx.getContentProviderInfo(); + EmailData emailData; + try { + emailData = contentProvider.getContent(info.getTemplate(), info.getParams()); + threadPoolExecutor.submit(new EmailSender(recipient, emailData.getSubject(), emailData.getBody())); + } catch (ContentProcessingInterruptedException e) { + throw new EmailSendingFailedException("Error occurred while retrieving email content to be " + + "sent for recipient '" + recipient + "'", e); } - } 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())); + } else { + String msg = "Email sender transport is not configured. Please configure the 'mailto' sender" + + " transport in axis2.xml."; + log.warn(msg); + throw new EmailTransportNotConfiguredException(msg); } }