Search in sources :

Example 6 with NotificationDTO

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

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenNotificationTypeNotRegistered.

@Test
public void testShouldNotThrowExceptionWhenNotificationTypeNotRegistered() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), "new_subscription");
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    notificationDTO.setTenantID(TENANT_ID);
    Notifier notifier = Mockito.mock(Notifier.class);
    Mockito.doNothing().when(notifier).run();
    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 7 with NotificationDTO

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

the class NotificationExecutorTest method testShouldThrowExceptionsWhenRegistryErrorOccurs.

@Test(expected = NotificationException.class)
public void testShouldThrowExceptionsWhenRegistryErrorOccurs() 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).thenThrow(APIManagementException.class);
    notificationExecutor.sendAsyncNotifications(notificationDTO);
    Assert.fail("Should throw an exception");
}
Also used : APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with NotificationDTO

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

the class NotificationExecutor method sendAsyncNotifications.

/**
 * Executes the notifer classes in separtate threads.
 * @param notificationDTO
 * @throws NotificationException
 */
public void sendAsyncNotifications(NotificationDTO notificationDTO) throws NotificationException {
    JSONObject notificationConfig;
    String notificationType = notificationDTO.getType();
    try {
        notificationConfig = APIUtil.getTenantConfig(notificationDTO.getTenantDomain());
        JSONArray notificationArray = (JSONArray) notificationConfig.get(NotifierConstants.Notifications_KEY);
        for (Object notification : notificationArray) {
            JSONObject notificationJson = (JSONObject) notification;
            String notifierType = (String) notificationJson.get(NotifierConstants.TYPE_KEY);
            if (notificationType.equals(notifierType)) {
                JSONArray notifierArray = (JSONArray) notificationJson.get("Notifiers");
                for (Object notifier : notifierArray) {
                    JSONObject jsonNotifier = (JSONObject) notifier;
                    String notifierClass = (String) jsonNotifier.get("Class");
                    Map map = (Map) jsonNotifier;
                    Properties prop = notificationDTO.getProperties();
                    prop.putAll(map);
                    notificationDTO.setProperties(prop);
                    // starting Notifier threads
                    if (notifierClass != null && !notifierClass.isEmpty()) {
                        Notifier notfier = (Notifier) APIUtil.getClassInstance(notifierClass);
                        notfier.setNotificationDTO(notificationDTO);
                        notfier.setTenantDomain(notificationDTO.getTenantDomain());
                        Thread notificationThread = new Thread(notfier);
                        notificationThread.start();
                    }
                }
            }
        }
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | APIManagementException e) {
        throw new NotificationException("Error while Initializing the notifier class", e);
    }
}
Also used : JSONArray(org.json.simple.JSONArray) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Properties(java.util.Properties) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

Example 9 with NotificationDTO

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

the class APIPublisherImpl method sendNotification.

private void sendNotification(String apiId, String apiName, String newVersion) throws APIManagementException {
    Set<String> subscriberList;
    NotificationConfigurations notificationConfigurations = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getNotificationConfigurations();
    // check notification Enabled
    if (notificationConfigurations.getNotificationEnable()) {
        subscriberList = getSubscribersByAPIId(apiId);
        // Notifications are sent only if there are subscribers
        if (subscriberList.size() > 0) {
            try {
                Properties prop = new Properties();
                prop.put(NotifierConstants.NEW_API_VERSION, newVersion);
                prop.put(NotifierConstants.API_NAME, apiName);
                prop.put(NotifierConstants.SUBSCRIBERS_PER_API, subscriberList);
                NotificationDTO notificationDTO = new NotificationDTO(prop, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
                new NotificationExecutor().sendAsyncNotifications(notificationDTO);
            } catch (NotificationException e) {
                String msg = "Error occurred while sending Async Notifications";
                log.error(msg, e);
            }
        }
    }
}
Also used : NotificationDTO(org.wso2.carbon.apimgt.core.template.dto.NotificationDTO) NotificationConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations) NotificationExecutor(org.wso2.carbon.apimgt.core.executors.NotificationExecutor) NotificationException(org.wso2.carbon.apimgt.core.exception.NotificationException) Properties(java.util.Properties)

Example 10 with NotificationDTO

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

the class NewAPIVersionEmailNotifier method sendNotifications.

@Override
public void sendNotifications(NotificationDTO notificationDTO) throws NotificationException {
    APIIdentifier api = (APIIdentifier) notificationDTO.getProperties().get(NotifierConstants.API_KEY);
    Set<Subscriber> subscriberList = (Set<Subscriber>) notificationDTO.getProperty(NotifierConstants.SUBSCRIBERS_PER_API);
    Map<String, String> emailProperties = null;
    // Notifications are sent only if there are subscribers
    if (subscriberList.size() > 0) {
        Set<String> notifierSet = getNotifierSet(notificationDTO);
        notificationDTO.setNotifierSet(notifierSet);
        notificationDTO = loadMessageTemplate(notificationDTO);
        emailProperties = getEmailProperties(notificationDTO);
        if (emailProperties != null) {
            String tenantDomain = getTenantDomain();
            String adapterName = NotifierConstants.ADAPTER_NAME + tenantDomain;
            String message = notificationDTO.getMessage();
            try {
                synchronized (Notifier.class) {
                    if (!adapterList.contains(adapterName)) {
                        OutputEventAdapterConfiguration outputEventAdapterConfiguration = createOutputEventAdapterConfiguration(adapterName, NotifierConstants.EMAIL_ADAPTER_TYPE);
                        createOutputEventAdapterService(outputEventAdapterConfiguration);
                        getOutputEventAdapterTypes();
                        adapterList.add(adapterName);
                    }
                }
                publishNotification(emailProperties, adapterName, message);
                log.info("notification sent to Email Adapter ");
            } catch (OutputEventAdapterException e) {
                throw new NotificationException("Adapter Creation Failed ", e);
            }
        } else {
            log.info("Empty email list. Please set subscriber's email addresses");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("No exiting Subscribers to send notifications for " + api.getApiName() + api.getVersion());
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) OutputEventAdapterException(org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) OutputEventAdapterConfiguration(org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration)

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