Search in sources :

Example 11 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifier method getNotifierSet.

/**
 * @param notificationDTO
 * @return a set of email ids
 * @throws NotificationException
 */
public Set<String> getNotifierSet(NotificationDTO notificationDTO) throws NotificationException {
    Set<Subscriber> subscriberList = (Set<Subscriber>) notificationDTO.getProperty(NotifierConstants.SUBSCRIBERS_PER_API);
    String claimsRetrieverImplClass = (String) notificationDTO.getProperty(NotifierConstants.CLAIMS_RETRIEVER_IMPL_CLASS);
    ClaimsRetriever claimsRetriever = null;
    Set<String> emailset = new HashSet<String>();
    try {
        for (Subscriber subscriber : subscriberList) {
            String tenantUserName = subscriber.getName();
            claimsRetriever = getClaimsRetriever(claimsRetrieverImplClass);
            claimsRetriever.init();
            String email = claimsRetriever.getClaims(tenantUserName).get(NotifierConstants.EMAIL_CLAIM);
            if (email != null && !email.isEmpty()) {
                emailset.add(email);
            }
        }
    } catch (IllegalAccessException e) {
        throw new NotificationException("Error while retrieving Email Claims ", e);
    } catch (InstantiationException e) {
        throw new NotificationException("Error while retrieving Email Claims ", e);
    } catch (ClassNotFoundException e) {
        throw new NotificationException("Cannot find claimsRetrieverImplClass ", e);
    } catch (APIManagementException e) {
        throw new NotificationException("Error while retrieving Email Claims ", e);
    }
    return emailset;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) HashSet(java.util.HashSet)

Example 12 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifier method loadMessageTemplate.

/**
 * Retrieves the message configurations from tenant-config.json and sets the notification properties to
 * NotificationDTO
 *
 * @param notificationDTO
 * @return NotificationDTO after setting meesage and title
 * @throws NotificationException
 */
public NotificationDTO loadMessageTemplate(NotificationDTO notificationDTO) throws NotificationException {
    APIIdentifier api = (APIIdentifier) notificationDTO.getProperties().get(NotifierConstants.API_KEY);
    APIIdentifier newApi = (APIIdentifier) notificationDTO.getProperties().get(NotifierConstants.NEW_API_KEY);
    String title = (String) notificationDTO.getProperty(NotifierConstants.TITLE_KEY);
    title = title.replaceAll("\\$1", newApi.getApiName());
    title = title.replaceAll("\\$2", newApi.getVersion());
    // Getting the message template from registry file
    String content = "";
    try {
        String template = (String) notificationDTO.getProperty(NotifierConstants.TEMPLATE_KEY);
        int tenantId = notificationDTO.getTenantID();
        Registry registry = getConfigSystemRegistry(tenantId);
        if (registry.resourceExists(template)) {
            if (log.isDebugEnabled()) {
                log.debug("Getting message template from registry resource : " + template);
            }
            Resource resource = registry.get(template);
            content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
        } else {
            content = template;
        }
    } catch (RegistryException e) {
        throw new NotificationException("Error while getting registry resource", e);
    }
    if (content != null && !content.isEmpty()) {
        content = content.replaceAll("\\$1", newApi.getApiName());
        content = content.replaceAll("\\$2", newApi.getVersion());
        content = content.replaceAll("\\$3", newApi.getProviderName());
        content = content.replaceAll("\\$4", api.getApiName());
        content = content.replaceAll("\\$5", api.getVersion());
    }
    notificationDTO.setTitle(title);
    notificationDTO.setMessage(content);
    return notificationDTO;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 13 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NotificationExecutorTest method testShouldSendNotificationWhenConfigurationExists.

@Test
public void testShouldSendNotificationWhenConfigurationExists() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    notificationDTO.setTenantID(TENANT_ID);
    Notifier notifier = Mockito.mock(Notifier.class);
    Mockito.doNothing().when(notifier).run();
    PowerMockito.when(APIUtil.class, "getTenantConfig", TENANT_DOMAIN).thenReturn(new JSONParser().parse(validNotifierConf));
    PowerMockito.when(APIUtil.class, "getClassInstance", "org.wso2.carbon.apimgt.impl.notification" + ".NewAPIVersionEmailNotifier").thenReturn(notifier);
    try {
        notificationExecutor.sendAsyncNotifications(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw an exception");
    }
}
Also used : NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) JSONParser(org.json.simple.parser.JSONParser) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenConfigurationNotFound.

@Test
public void testShouldNotThrowExceptionWhenConfigurationNotFound() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantID(TENANT_ID);
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(NotifierConstants.Notifications_KEY, new JSONArray());
    PowerMockito.when(APIUtil.class, "getTenantConfig", TENANT_DOMAIN).thenReturn(jsonObject);
    try {
        notificationExecutor.sendAsyncNotifications(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw an exception");
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenNotifierNotDefined.

@Test
public void testShouldNotThrowExceptionWhenNotifierNotDefined() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    notificationDTO.setTenantID(TENANT_ID);
    Notifier notifier = Mockito.mock(Notifier.class);
    Mockito.doNothing().when(notifier).run();
    PowerMockito.when(APIUtil.class, "getTenantConfig", TENANT_DOMAIN).thenReturn(new JSONParser().parse(invalidNotifierConf));
    try {
        notificationExecutor.sendAsyncNotifications(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw an exception");
    }
}
Also used : NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) JSONParser(org.json.simple.parser.JSONParser) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

NotificationException (org.wso2.carbon.apimgt.impl.notification.exception.NotificationException)13 Test (org.junit.Test)11 Properties (java.util.Properties)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 JSONObject (org.json.simple.JSONObject)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)5 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)4 ClaimsRetriever (org.wso2.carbon.apimgt.impl.token.ClaimsRetriever)4 Resource (org.wso2.carbon.registry.core.Resource)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 JSONArray (org.json.simple.JSONArray)3 Map (java.util.Map)2 JSONParser (org.json.simple.parser.JSONParser)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIMConfigurations (org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations)2 NotificationConfigurations (org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations)2 NotificationException (org.wso2.carbon.apimgt.core.exception.NotificationException)2 NotificationExecutor (org.wso2.carbon.apimgt.core.executors.NotificationExecutor)2