Search in sources :

Example 1 with NotificationException

use of org.wso2.carbon.apimgt.core.exception.NotificationException in project carbon-apimgt by wso2.

the class NotificationExecutor method sendAsyncNotifications.

/**
 * Executes the notifer classes in separtate threads.
 *
 * @param notificationDTO
 * @throws org.wso2.carbon.apimgt.core.exception.NotificationException
 */
public void sendAsyncNotifications(NotificationDTO notificationDTO) throws NotificationException {
    List<NotifierConfigurations> notifierConfigurations = new APIMConfigurations().getNotificationConfigurations().getNewVersionNotifierConfiguration().getNotifierConfigurations();
    if (notificationDTO.getType().equalsIgnoreCase(NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION)) {
        for (NotifierConfigurations listItem : notifierConfigurations) {
            String executorClass = listItem.getExecutorClass();
            Map property = listItem.getPropertyList();
            Properties prop = notificationDTO.getProperties();
            prop.putAll(property);
            notificationDTO.setProperties(prop);
            // starting Notifier threads
            if (executorClass != null && !executorClass.isEmpty()) {
                Notifier notifier;
                try {
                    notifier = (Notifier) APIUtils.getClassForName(executorClass).newInstance();
                } catch (InstantiationException e) {
                    throw new NotificationException("Instantiation Error while Initializing the notifier class", e);
                } catch (IllegalAccessException e) {
                    throw new NotificationException("IllegalAccess Error while Initializing the notifier class", e);
                } catch (ClassNotFoundException e) {
                    throw new NotificationException("ClassNotFound Error while Initializing the notifier class", e);
                }
                notifier.setNotificationDTO(notificationDTO);
                executor.execute(notifier);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Class " + executorClass + " Empty Or Null");
                }
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Notification Type Does Not match with " + NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
        }
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }
}
Also used : APIMConfigurations(org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations) NotificationException(org.wso2.carbon.apimgt.core.exception.NotificationException) NotifierConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotifierConfigurations) Properties(java.util.Properties) Map(java.util.Map) Notifier(org.wso2.carbon.apimgt.core.impl.Notifier)

Example 2 with NotificationException

use of org.wso2.carbon.apimgt.core.exception.NotificationException 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 3 with NotificationException

use of org.wso2.carbon.apimgt.core.exception.NotificationException in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method testShouldNotThrowExceptionWhenRetrievingNotifiers.

@Test
public void testShouldNotThrowExceptionWhenRetrievingNotifiers() throws APIManagementException {
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifierWrapper(registry, claimsRetriever);
    ClaimsRetriever claimsRetriever = Mockito.mock(ClaimsRetriever.class);
    Mockito.doNothing().when(claimsRetriever).init();
    try {
        emailNotifier.getNotifierSet(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : NewAPIVersionEmailNotifierWrapper(org.wso2.carbon.apimgt.impl.notification.util.NewAPIVersionEmailNotifierWrapper) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) Test(org.junit.Test)

Example 4 with NotificationException

use of org.wso2.carbon.apimgt.core.exception.NotificationException in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method testShouldLoadMessageTemplateWhenDtoValid.

@Test
public void testShouldLoadMessageTemplateWhenDtoValid() throws RegistryException {
    Resource resource = Mockito.mock(Resource.class);
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {

        @Override
        protected Registry getConfigSystemRegistry(int tenantId) throws RegistryException {
            return registry;
        }
    };
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true).thenReturn(false);
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn("<html>$1-$2-$3</html>".getBytes());
    try {
        NotificationDTO notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
        // should use dto template if resource not found in registry
        notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Test(org.junit.Test)

Example 5 with NotificationException

use of org.wso2.carbon.apimgt.core.exception.NotificationException 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)

Aggregations

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