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