Search in sources :

Example 6 with NotificationException

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);
            }
        }
    }
}
Also used : NotificationDTO(org.wso2.carbon.apimgt.core.template.dto.NotificationDTO) NotificationConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations) NotificationExecutor(org.wso2.carbon.apimgt.core.executors.NotificationExecutor) NotificationException(org.wso2.carbon.apimgt.core.exception.NotificationException) Properties(java.util.Properties)

Example 7 with NotificationException

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());
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) OutputEventAdapterException(org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) OutputEventAdapterConfiguration(org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration)

Example 8 with NotificationException

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) HashSet(java.util.HashSet)

Example 9 with NotificationException

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;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 10 with NotificationException

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");
    }
}
Also used : NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) JSONParser(org.json.simple.parser.JSONParser) 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