use of org.wso2.carbon.apimgt.core.exception.NotificationException 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.core.exception.NotificationException 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());
}
}
}
use of org.wso2.carbon.apimgt.core.exception.NotificationException 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.core.exception.NotificationException 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.core.exception.NotificationException 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");
}
}
Aggregations