Skip to content

Commit

Permalink
Add native function to send emails
Browse files Browse the repository at this point in the history
  • Loading branch information
pulasthi7 committed May 16, 2018
1 parent 735da05 commit f388f4b
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.iws
*.ipr
.idea
.DS_Store

# Mobile Tools for Java (J2ME)
.mtj.tmp/

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018, 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.wso2.carbon.identity.conditional.auth.functions</groupId>
<artifactId>identity-conditional-auth-functions</artifactId>
<version>0.1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>org.wso2.carbon.identity.conditional.auth.functions.notification</artifactId>
<name>Conditional Authentication - Notification Related Functions</name>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>commons-lang.wso2</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.event</artifactId>
</dependency>

<!--Test Dependencies-->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Private-Package>
org.wso2.carbon.identity.conditional.auth.functions.notification.internal
</Private-Package>
<Export-Package>
!org.wso2.carbon.identity.conditional.auth.functions.notification.internal,
org.wso2.carbon.identity.conditional.auth.functions.notification
</Export-Package>
<Import-Package>
org.apache.commons.lang,
org.apache.commons.logging,
org.osgi.service.component,
org.osgi.service.component.annotations,
org.wso2.carbon,
org.wso2.carbon.core.util,
org.wso2.carbon.identity.application.authentication.framework,
org.wso2.carbon.identity.application.authentication.framework.exception,
org.wso2.carbon.identity.application.authentication.framework.model,
org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js,
org.wso2.carbon.identity.event,
org.wso2.carbon.identity.event.event,
org.wso2.carbon.identity.event.services,
org.wso2.carbon.registry.core.service,
org.wso2.carbon.user.core.service
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018, 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.identity.conditional.auth.functions.notification;

import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser;

import java.util.Map;

@FunctionalInterface
public interface SendEmailFunction {

/**
* Send an email to the given user using a provided template.
* @param user The user object to whom the mail is send
* @param templateId The email template id, Which is configured under email templates.
* @param paramMap Placeholder value map
* @return <code>true</code> if the email is successfully queued to be sent. <code>false</code> If the mail
* couldn't be queued due to any error.
*/
boolean sendMail(JsAuthenticatedUser user, String templateId, Map<String, String> paramMap);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2018, 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.identity.conditional.auth.functions.notification;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser;
import org.wso2.carbon.identity.conditional.auth.functions.notification.internal.NotificationFunctionServiceHolder;
import org.wso2.carbon.identity.event.IdentityEventConstants;
import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.event.Event;

import java.util.HashMap;
import java.util.Map;

/**
* Contains the sendEmail() function implementation.
*/
public class SendEmailFunctionImpl implements SendEmailFunction {

private static final String TEMPLATE_TYPE = "TEMPLATE_TYPE";

private static final Log LOG = LogFactory.getLog(SendEmailFunctionImpl.class);

@Override
public boolean sendMail(JsAuthenticatedUser user, String templateId, Map<String, String> paramMap) {

String eventName = IdentityEventConstants.Event.TRIGGER_NOTIFICATION;

HashMap<String, Object> properties = new HashMap<>(paramMap);
properties.put(IdentityEventConstants.EventProperty.USER_NAME, user.getWrapped().getUserName());
properties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, user.getWrapped().getTenantDomain());
properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, user.getWrapped().getUserStoreDomain());
properties.put(TEMPLATE_TYPE, templateId);

Event identityMgtEvent = new Event(eventName, properties);
try {
NotificationFunctionServiceHolder.getInstance().getIdentityEventService().handleEvent(identityMgtEvent);
} catch (IdentityEventException e) {
LOG.error(String.format("Error when sending notification of template %s to user %s", templateId, user
.getWrapped().toFullQualifiedUsername()), e);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2018, 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.identity.conditional.auth.functions.notification.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.conditional.auth.functions.notification.SendEmailFunction;
import org.wso2.carbon.identity.conditional.auth.functions.notification.SendEmailFunctionImpl;
import org.wso2.carbon.identity.event.services.IdentityEventService;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;

/**
* OSGi declarative services component which handles registration and de-registration of notifications related
* conditional auth functions.
*/

@Component(
name = "identity.conditional.auth.functions.notification.component",
immediate = true
)
public class NotificationFunctionServiceComponent {

private static final Log LOG = LogFactory.getLog(NotificationFunctionServiceComponent.class);

@Activate
protected void activate(ComponentContext ctxt) {

SendEmailFunction sendEmailFunction = new SendEmailFunctionImpl();
JsFunctionRegistry jsFunctionRegistry = NotificationFunctionServiceHolder.getInstance().getJsFunctionRegistry();
jsFunctionRegistry.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "sendEmail", sendEmailFunction);
}

@Deactivate
protected void deactivate(ComponentContext ctxt) {

JsFunctionRegistry jsFunctionRegistry = NotificationFunctionServiceHolder.getInstance().getJsFunctionRegistry();
if (jsFunctionRegistry != null) {
jsFunctionRegistry.deRegister(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "sendEmail");
}
}

@Reference(
name = "user.realmservice.default",
service = RealmService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetRealmService"
)
protected void setRealmService(RealmService realmService) {

if (LOG.isDebugEnabled()) {
LOG.debug("RealmService is set in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setRealmService(realmService);
}

protected void unsetRealmService(RealmService realmService) {

if (LOG.isDebugEnabled()) {
LOG.debug("RealmService is unset in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setRealmService(null);
}

@Reference(
name = "registry.service",
service = RegistryService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetRegistryService"
)
protected void setRegistryService(RegistryService registryService) {

if (LOG.isDebugEnabled()) {
LOG.debug("RegistryService is set in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setRegistryService(registryService);
}

protected void unsetRegistryService(RegistryService registryService) {

if (LOG.isDebugEnabled()) {
LOG.debug("RegistryService is unset in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setRegistryService(null);
}

@Reference(
service = JsFunctionRegistry.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetJsFunctionRegistry"
)
public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

NotificationFunctionServiceHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistry);
}

public void unsetJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

NotificationFunctionServiceHolder.getInstance().setJsFunctionRegistry(null);
}

@Reference(
service = IdentityEventService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetIdentityEventService"
)
protected void setIdentityEventService(IdentityEventService identityEventService) {

if (LOG.isDebugEnabled()) {
LOG.debug("IdentityEventService is set in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setIdentityEventService(identityEventService);
}

protected void unsetIdentityEventService(IdentityEventService identityEventService) {

if (LOG.isDebugEnabled()) {
LOG.debug("IdentityEventService is unset in the conditional authentication notification functions bundle");
}
NotificationFunctionServiceHolder.getInstance().setIdentityEventService(null);
}
}
Loading

0 comments on commit f388f4b

Please sign in to comment.