Search in sources :

Example 1 with AdminAlertConfigurator

use of org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method getSubscribedAlertTypes.

/**
 * Retrieves all the admin alert subscriptions of the logged in user
 *
 * @param messageContext
 * @return
 */
public Response getSubscribedAlertTypes(MessageContext messageContext) {
    String fullyQualifiedUsername = getFullyQualifiedUsername(RestApiCommonUtil.getLoggedInUsername());
    try {
        AdminAlertConfigurator adminAlertConfigurator = (AdminAlertConfigurator) AlertConfigManager.getInstance().getAlertConfigurator(AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
        List<Integer> subscribedAlertTypes = adminAlertConfigurator.getSubscribedAlerts(fullyQualifiedUsername);
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> supportedAlertTypeDTOS = adminAlertConfigurator.getSupportedAlertTypes();
        // Filter out the subscribed alerts
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> subscribedAlertsList = supportedAlertTypeDTOS.stream().filter(supportedAlertTypes -> subscribedAlertTypes.stream().anyMatch(subscribedAlerts -> supportedAlertTypes.getId().equals(subscribedAlerts))).collect(Collectors.toList());
        // Retrieve the list of subscribed emails
        List<String> subscribedEmails = adminAlertConfigurator.getSubscribedEmailAddresses(fullyQualifiedUsername);
        AlertsSubscriptionDTO alertsSubscriptionDTO = new AlertsSubscriptionDTO();
        alertsSubscriptionDTO.setAlerts(AlertsMappingUtil.fromAlertTypesListToAlertTypeDTOList(subscribedAlertsList));
        alertsSubscriptionDTO.setEmailList(subscribedEmails);
        return Response.status(Response.Status.OK).entity(alertsSubscriptionDTO).build();
    } catch (AlertManagementException e) {
        return Response.status(Response.Status.NO_CONTENT).entity("API Manager analytics is not enabled").build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Internal Error occurred", e, log);
    }
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : MultitenantConstants(org.wso2.carbon.utils.multitenancy.MultitenantConstants) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) AlertMgtConstants(org.wso2.carbon.apimgt.impl.alertmgt.AlertMgtConstants) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) RestApiCommonUtil(org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil) AlertsMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.AlertsMappingUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) AlertConfigManager(org.wso2.carbon.apimgt.impl.alertmgt.AlertConfigManager) Map(java.util.Map) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) Collectors(java.util.stream.Collectors) AlertSubscriptionsApiService(org.wso2.carbon.apimgt.rest.api.admin.v1.AlertSubscriptionsApiService) BotDetectionAlertSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionDTO) List(java.util.List) BotDetectionAlertSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionListDTO) Response(javax.ws.rs.core.Response) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) BotDetectionMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.BotDetectionMappingUtil) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator)

Example 2 with AdminAlertConfigurator

use of org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator in project carbon-apimgt by wso2.

the class AlertTypesApiServiceImpl method getAdminAlertTypes.

@Override
public Response getAdminAlertTypes(MessageContext messageContext) throws APIManagementException {
    try {
        AdminAlertConfigurator adminAlertConfigurator = (AdminAlertConfigurator) AlertConfigManager.getInstance().getAlertConfigurator(AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> alertTypes = adminAlertConfigurator.getSupportedAlertTypes();
        AlertTypesListDTO alertTypesListDTO = AlertsMappingUtil.fromAlertTypesListToAlertTypeListDTO(alertTypes);
        return Response.status(Response.Status.OK).entity(alertTypesListDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Internal Server Error Occurred while retrieving alert types", e, log);
    } catch (AlertManagementException e) {
        log.warn("API Manager Analytics is not enabled", e);
        return Response.status(Response.Status.NO_CONTENT).build();
    }
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator) AlertTypesListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypesListDTO)

Example 3 with AdminAlertConfigurator

use of org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method unsubscribeAllAlerts.

/**
 * Unsubscribe the user from all the admin alert types
 *
 * @param messageContext
 * @return
 */
@Override
public Response unsubscribeAllAlerts(MessageContext messageContext) {
    String fullyQualifiedUsername = getFullyQualifiedUsername(RestApiCommonUtil.getLoggedInUsername());
    try {
        AdminAlertConfigurator adminAlertConfigurator = (AdminAlertConfigurator) AlertConfigManager.getInstance().getAlertConfigurator(AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
        adminAlertConfigurator.unsubscribe(fullyQualifiedUsername);
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Internal Server Error occurred while un subscribing from alerts", e, log);
    } catch (AlertManagementException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Analytics not Enabled").build();
    }
    return Response.status(Response.Status.OK).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator)

Example 4 with AdminAlertConfigurator

use of org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method subscribeToAlerts.

/**
 * Subscribes the logged in user for requested admin alert types
 *
 * @param body
 * @param messageContext
 * @return
 */
@Override
public Response subscribeToAlerts(AlertsSubscriptionDTO body, MessageContext messageContext) {
    // Validate for empty list of emails
    List<String> emailsList = body.getEmailList();
    if (emailsList == null || emailsList.size() == 0) {
        RestApiUtil.handleBadRequest("Email list cannot be empty", log);
    }
    // Validate for empty list of alerts
    List<AlertTypeDTO> subscribingAlertDTOs = body.getAlerts();
    if (subscribingAlertDTOs == null || subscribingAlertDTOs.size() == 0) {
        RestApiUtil.handleBadRequest("Alert list should not be empty", log);
    }
    String fullyQualifiedUsername = getFullyQualifiedUsername(RestApiCommonUtil.getLoggedInUsername());
    try {
        AdminAlertConfigurator adminAlertConfigurator = (AdminAlertConfigurator) AlertConfigManager.getInstance().getAlertConfigurator(AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
        // Retrieve the supported alert types
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> supportedAlertTypes = adminAlertConfigurator.getSupportedAlertTypes();
        Map<String, org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> supportedAlertTypesMap = supportedAlertTypes.stream().collect(Collectors.toMap(org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO::getName, alertType -> alertType));
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> alertTypesToSubscribe = new ArrayList<>();
        // Validate the request alerts against supported alert types
        for (AlertTypeDTO subscribingAlertDTO : subscribingAlertDTOs) {
            if (supportedAlertTypesMap.containsKey(subscribingAlertDTO.getName())) {
                alertTypesToSubscribe.add(supportedAlertTypesMap.get(subscribingAlertDTO.getName()));
            } else {
                RestApiUtil.handleBadRequest("Unsupported alert type : " + subscribingAlertDTO.getName() + " is provided.", log);
                return null;
            }
        }
        adminAlertConfigurator.subscribe(fullyQualifiedUsername, emailsList, alertTypesToSubscribe);
        AlertsSubscriptionDTO subscribedAlerts = new AlertsSubscriptionDTO();
        subscribedAlerts.setAlerts(AlertsMappingUtil.fromAlertTypesListToAlertTypeDTOList(alertTypesToSubscribe));
        subscribedAlerts.setEmailList(emailsList);
        return Response.status(Response.Status.OK).entity(subscribedAlerts).build();
    } catch (AlertManagementException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity("API Manager analytics is not Enabled").build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while subscribing to alert types", e, log);
    }
    return null;
}
Also used : MultitenantConstants(org.wso2.carbon.utils.multitenancy.MultitenantConstants) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) AlertMgtConstants(org.wso2.carbon.apimgt.impl.alertmgt.AlertMgtConstants) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) RestApiCommonUtil(org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil) AlertsMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.AlertsMappingUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) AlertConfigManager(org.wso2.carbon.apimgt.impl.alertmgt.AlertConfigManager) Map(java.util.Map) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) Collectors(java.util.stream.Collectors) AlertSubscriptionsApiService(org.wso2.carbon.apimgt.rest.api.admin.v1.AlertSubscriptionsApiService) BotDetectionAlertSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionDTO) List(java.util.List) BotDetectionAlertSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionListDTO) Response(javax.ws.rs.core.Response) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) BotDetectionMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.BotDetectionMappingUtil) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) ArrayList(java.util.ArrayList) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 AdminAlertConfigurator (org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator)4 AlertManagementException (org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Response (javax.ws.rs.core.Response)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)2 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)2 ExceptionCodes (org.wso2.carbon.apimgt.api.ExceptionCodes)2 BotDetectionData (org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData)2 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)2 AlertConfigManager (org.wso2.carbon.apimgt.impl.alertmgt.AlertConfigManager)2 AlertMgtConstants (org.wso2.carbon.apimgt.impl.alertmgt.AlertMgtConstants)2 AlertSubscriptionsApiService (org.wso2.carbon.apimgt.rest.api.admin.v1.AlertSubscriptionsApiService)2 AlertTypeDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO)2