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