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()) {
}
}
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);
}
}
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");
}
}
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");
}
}
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");
}
}
Aggregations