Merge pull request #947 from harshanL/master

Fix for wso2/product-iots#1035
4.x.x
Ruwan 7 years ago committed by GitHub
commit cb14a39e78

@ -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;
}
}

@ -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,18 @@ public class UserManagementServiceImpl implements UserManagementService {
props.setProperty("password", initialUserPassword);
EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props);
dms.sendRegistrationEmail(metaInfo);
return Response.created(new URI(API_BASE_PATH + "/" + URIEncoder.encode(userInfo.getUsername(), "UTF-8")))
.entity(
createdUserInfo).build();
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(userInfoWrapper).build();
} catch (UserStoreException e) {
String msg = "Error occurred while trying to add user '" + userInfo.getUsername() + "' to the " +
"underlying user management system";
@ -178,8 +188,7 @@ public class UserManagementServiceImpl implements UserManagementService {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while sending registration email to the user " +
userInfo.getUsername();
String msg = "Error occurred while sending registration email to the user " + userInfo.getUsername();
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
@ -578,6 +587,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();
}
@ -613,6 +626,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();
}

@ -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<Integer, Device> 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;

@ -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<String, TypedValue<Class<?>, Object>> params = new HashMap<>();
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("first-name")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.USERNAME,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("username")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.PASSWORD,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("password")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.DOMAIN,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("domain")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTPS,
new TypedValue<Class<?>, Object>(String.class, DeviceManagerUtil.getServerBaseHttpsUrl()));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTP,
new TypedValue<Class<?>, 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<String, TypedValue<Class<?>, Object>> params = new HashMap<>();
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("first-name")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.USERNAME,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("username")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.PASSWORD,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("password")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.DOMAIN,
new TypedValue<Class<?>, Object>(String.class, metaInfo.getProperty("domain")));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTPS,
new TypedValue<Class<?>, Object>(String.class, DeviceManagerUtil.getServerBaseHttpsUrl()));
params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.SERVER_BASE_URL_HTTP,
new TypedValue<Class<?>, 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);
}
}
}

@ -128,9 +128,14 @@
<i class="icon fw fw-error"></i><span></span>
</div>
<h4>
User added successfully.
An invitation mail will be sent to this user to initiate device enrollment.
</h4>
</div>
<div id="modal-content-user-created-with-message" class="hide">
<div id="notification-error-msg" class="alert alert-danger hidden" role="alert">
<i class="icon fw fw-error"></i><span></span>
</div>
</div>
<!-- /content -->
</div>
</div>

@ -275,6 +275,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("");
@ -287,7 +288,12 @@ $(document).ready(function () {
$("#user-create-form").addClass("hidden");
modalDialog.header('<span class="fw-stack">' +
'<i class="fw fw-info fw-stack-1x"></i> </span> User was added successfully');
modalDialog.content($("#modal-content-user-created").html());
if (response.message) {
$("#modal-content-user-created-with-message").append("<h4>" + response.message + "</h4>");
modalDialog.content($("#modal-content-user-created-with-message").html());
} else {
modalDialog.content($("#modal-content-user-created").html());
}
modalDialog.footer('<div class="buttons"> ' +
'<a href="/devicemgt/users" id="reset-password-yes-link" class="btn-operations"> OK' +
'</a></div>');

@ -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);
}
}

@ -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;
}

@ -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,25 +61,31 @@ public class EmailSenderServiceImpl implements EmailSenderService {
this.contentProvider = EmailContentProviderFactory.getContentProvider();
}
private boolean isMailServerConfigured() {
return (EmailSenderDataHolder.getInstance().getConfigurationContextService().getServerConfigContext().
getAxisConfiguration().getTransportOut(TRANSPORT_SENDER_NAME) != null);
}
@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){
log.warn("Email invitation is not sent as the email is not configured.");
} else {
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);
}
} 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);
}
}
@ -130,7 +137,5 @@ public class EmailSenderServiceImpl implements EmailSenderService {
"'", e);
}
}
}
}
}
Loading…
Cancel
Save