Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#executeQueryOnStreamProcessor()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#executeQueryOnStreamProcessor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIAdminImpl.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public List<BotDetectionData> retrieveBotDetectionData() throws APIManagementException {

    List<BotDetectionData> botDetectionDatalist = new ArrayList<>();
    String appName = AlertMgtConstants.APIM_ALERT_BOT_DETECTION_APP;
    String query = SQLConstants.BotDataConstants.GET_BOT_DETECTED_DATA;

    JSONObject botDataJsonObject = APIUtil.executeQueryOnStreamProcessor(appName, query);
    if (botDataJsonObject != null) {
        JSONArray botDataJsonArray = (JSONArray) botDataJsonObject.get("records");
        if (botDataJsonArray != null && botDataJsonArray.size() != 0) {
            for (Object botData : botDataJsonArray) {
                JSONArray values = (JSONArray) botData;
                BotDetectionData botDetectionData = new BotDetectionData();
                botDetectionData.setCurrentTime((Long) values.get(0));
                botDetectionData.setMessageID((String) values.get(1));
                botDetectionData.setApiMethod((String) values.get(2));
                botDetectionData.setHeaderSet((String) values.get(3));
                botDetectionData.setMessageBody(extractBotDetectionDataContent((String) values.get(4)));
                botDetectionData.setClientIp((String) values.get(5));
                botDetectionDatalist.add(botDetectionData);
            }
        }
    }
    return botDetectionDatalist;
}
 
Example 2
Source File: PublisherAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public List<Map<String, String>> getAlertConfiguration(String userName, String alertName)
        throws APIManagementException {
    String domainName = MultitenantUtils.getTenantDomain(userName);
    String configPropertyName = AlertMgtConstants.alertTypeConfigMap.get(alertName);
    String query = "from ApiCreatorAlertConfiguration on apiCreatorTenantDomain=='" + domainName + "' and "
            + configPropertyName + "!=0 select apiName,apiVersion,apiCreator,apiCreatorTenantDomain, "
            + configPropertyName + "; ";
    JSONObject result = APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, query);
    List<Map<String, String>> alertConfigProps = new ArrayList<>();
    if (result != null && result.get("records") != null) {
        JSONArray records = (JSONArray) result.get("records");
        for (Object record : records) {
            JSONArray config = (JSONArray) record;
            Map<String, String> properties = new HashMap<>();
            properties.put(AlertMgtConstants.API_NAME_KEY, (String) config.get(0));
            properties.put(AlertMgtConstants.API_VERSION_KEY, (String) config.get(1));
            properties.put(AlertMgtConstants.API_CREATOR_KEY, (String) config.get(2));
            properties.put(AlertMgtConstants.API_CREATOR_TENANT_DOMAIN_KEY, (String) config.get(3));
            properties.put(configPropertyName, String.valueOf(config.get(4)));
            alertConfigProps.add(properties);
        }
    }
    return alertConfigProps;
}
 
Example 3
Source File: StoreAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public void addAlertConfiguration(String userName, String alertName, Map<String, String> configProperties)
        throws APIManagementException {

    String applicationId = configProperties.get(AlertMgtConstants.APPLICATION_ID_KEY);
    String apiName = configProperties.get(AlertMgtConstants.API_NAME_KEY);
    String apiVersion = configProperties.get(AlertMgtConstants.API_VERSION_KEY);
        String thresholdRequestCountPerMin = configProperties.get(AlertMgtConstants.REQUEST_COUNT_KEY);
    String query =
            "select '" + applicationId + "' as applicationId, '" + userName + "' as subscriber, '" + apiName
                    + "' as apiName, '" + apiVersion + "' as apiVersion, "
                    + Integer.valueOf(thresholdRequestCountPerMin)
                    + " as thresholdRequestCountPerMin update or insert into ApiSubAlertConf "
                    + "set ApiSubAlertConf.thresholdRequestCountPerMin = thresholdRequestCountPerMin "
                    + "on ApiSubAlertConf.applicationId == applicationId and "
                    + "ApiSubAlertConf.subscriber == subscriber and "
                    + "ApiSubAlertConf.apiName == apiName and ApiSubAlertConf.apiVersion == apiVersion";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, query);
}
 
Example 4
Source File: StoreAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public List<Map<String, String>> getAlertConfiguration(String userName, String alertName)
        throws APIManagementException {

    String query = "from ApiSubAlertConf on subscriber == '" + userName + "' select applicationId ,apiName , "
            + "apiVersion, thresholdRequestCountPerMin;";
    JSONObject result = APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, query);
    List<Map<String, String>> alertConfigList = new ArrayList<>();
    if (result != null && result.get("records") != null) {
        JSONArray alertConfigs = (JSONArray) result.get("records");
        for (Object config : alertConfigs) {
            JSONArray alertConfig = (JSONArray) config;
            Map<String, String> configProperties = new HashMap<>();
            configProperties.put(AlertMgtConstants.APPLICATION_ID_KEY, (String)alertConfig.get(0));
            configProperties.put(AlertMgtConstants.API_NAME_KEY, (String)alertConfig.get(1));
            configProperties.put(AlertMgtConstants.API_VERSION_KEY, (String)alertConfig.get(2));
            configProperties.put(AlertMgtConstants.REQUEST_COUNT_KEY, String.valueOf(alertConfig.get(3)));
            alertConfigList.add(configProperties);
        }
    }
    return alertConfigList;
}
 
Example 5
Source File: PublisherAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(String userName, List<String> emailsList, List<AlertTypeDTO> alertTypeDTOList)
        throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Subscribing user: " + userName + "to alert types");
    }
    String emails = StringUtils.join(emailsList, ",");
    Map<String, String> alertTypesMap = AlertMgtUtils.alertTypesToMap(alertTypeDTOList);

    if (log.isDebugEnabled()) {
        log.debug("Persisting subscribing alert types " + alertTypesMap.get("ids") + "in database.");
    }

    String query =
            "select '" + userName + "' as userId, '" + alertTypesMap.get("names") + "' as alertTypes, '" + emails
                    + "' as emails, false as isSubscriber, true as isPublisher, "
                    + "false as isAdmin update or insert into ApimAlertStakeholderInfo "
                    + "set ApimAlertStakeholderInfo.userId = userId, "
                    + "ApimAlertStakeholderInfo.alertTypes = alertTypes , "
                    + "ApimAlertStakeholderInfo.emails = emails , "
                    + "ApimAlertStakeholderInfo.isSubscriber = isSubscriber, "
                    + "ApimAlertStakeholderInfo.isPublisher = isPublisher, "
                    + "ApimAlertStakeholderInfo.isAdmin = isAdmin on "
                    + "ApimAlertStakeholderInfo.userId == userId and "
                    + "ApimAlertStakeholderInfo.isPublisher == isPublisher";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);
    apiMgtDAO.addAlertTypesConfigInfo(userName, emails, alertTypesMap.get("ids"),
            AlertMgtConstants.PUBLISHER_AGENT);
}
 
Example 6
Source File: PublisherAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void unsubscribe(String userName) throws APIManagementException {
    apiMgtDAO.unSubscribeAlerts(userName, AlertMgtConstants.PUBLISHER_AGENT);
    String query = "delete ApimAlertStakeholderInfo on ApimAlertStakeholderInfo.userId == '" + userName + "' and "
            + "ApimAlertStakeholderInfo.isPublisher == true";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);
}
 
Example 7
Source File: PublisherAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void addAlertConfiguration(String userName, String alertName, Map<String, String> configProperties)
        throws APIManagementException {
    String apiName = configProperties.get(AlertMgtConstants.API_NAME_KEY);
    String apiVersion = configProperties.get(AlertMgtConstants.API_VERSION_KEY);
    String configPropertyName = AlertMgtConstants.alertTypeConfigMap.get(alertName);
    String configValue = configProperties.get(configPropertyName);
    String query = buildAddConfigQuery(userName, apiName, apiVersion, alertName, configValue);
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, query);
}
 
Example 8
Source File: PublisherAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void removeAlertConfiguration(String userName, String alertName, Map<String, String> configProperties)
        throws APIManagementException {
    String apiName = configProperties.get(AlertMgtConstants.API_NAME_KEY);
    String apiVersion = configProperties.get(AlertMgtConstants.API_VERSION_KEY);
    String alertConfigDeleteQuery = buildAlertConfigDeleteQuery(apiName, apiVersion, userName, alertName);
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, alertConfigDeleteQuery);
}
 
Example 9
Source File: AdminAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribe for admin alerts
 *
 * @param userName          : The username of the user, who is subscribing.
 * @param emailsList        : The list of emails which needs to be subscribed.
 * @param alertTypeDTOList: The list of Alert types which needs to be subscribed.
 * @throws APIManagementException
 */
@Override public void subscribe(String userName, List<String> emailsList, List<AlertTypeDTO> alertTypeDTOList)
        throws APIManagementException {

    String emails = StringUtils.join(emailsList, ",");
    Map<String, String> alertTypesMap = AlertMgtUtils.alertTypesToMap(alertTypeDTOList);

    if (log.isDebugEnabled()) {
        log.debug(
                "Subscribing user: " + userName + " for alert types: " + alertTypesMap.get("ids") + " with emails: "
                        + emails);
    }

    String query =
            "select '" + userName + "' as userId, '" + alertTypesMap.get("names") + "' as alertTypes, '" + emails
                    + "' as emails, false as isSubscriber, false as isPublisher, "
                    + "true as isAdmin update or insert into ApimAlertStakeholderInfo "
                    + "set ApimAlertStakeholderInfo.userId = userId, "
                    + "ApimAlertStakeholderInfo.alertTypes = alertTypes , "
                    + "ApimAlertStakeholderInfo.emails = emails , "
                    + "ApimAlertStakeholderInfo.isSubscriber = isSubscriber, "
                    + "ApimAlertStakeholderInfo.isPublisher = isPublisher, "
                    + "ApimAlertStakeholderInfo.isAdmin = isAdmin on "
                    + "ApimAlertStakeholderInfo.userId == userId and "
                    + "ApimAlertStakeholderInfo.isPublisher == isPublisher";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);
    //Persist the alert subscription in database
    apiMgtDAO.addAlertTypesConfigInfo(userName, emails, alertTypesMap.get("ids"),
            AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
}
 
Example 10
Source File: AdminAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Unsubscribe from all admin alerts
 *
 * @param userName : The name of the user who needs to be unsubscribed.
 * @throws APIManagementException
 */
@Override public void unsubscribe(String userName) throws APIManagementException {

    if (log.isDebugEnabled()) {
        log.debug("Unsubscribing user: " + userName + " for all alert types");
    }

    //Removing the existing alert subscription information from the database
    apiMgtDAO.unSubscribeAlerts(userName, AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
    String query = "delete ApimAlertStakeholderInfo on ApimAlertStakeholderInfo.userId == '" + userName + "' and "
            + "ApimAlertStakeholderInfo.isAdmin == true";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);
}
 
Example 11
Source File: StoreAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(String userName, List<String> emailsList, List<AlertTypeDTO> alertTypeDTOList)
        throws APIManagementException {

    if (log.isDebugEnabled()) {
        log.debug("Subscribing user: " + userName + "to alert types");
    }
    String emails = StringUtils.join(emailsList, ",");
    Map<String, String> alertTypesMap = AlertMgtUtils.alertTypesToMap(alertTypeDTOList);

    if (log.isDebugEnabled()) {
        log.debug("Persisting subscribing alert types in database.");
    }

    String query =
            "select '" + userName + "' as userId, '" + alertTypesMap.get("names") + "' as alertTypes, '" + emails
                    + "' as emails, true as isSubscriber, false as isPublisher, "
                    + "false as isAdmin update or insert into ApimAlertStakeholderInfo "
                    + "set ApimAlertStakeholderInfo.userId = userId, "
                    + "ApimAlertStakeholderInfo.alertTypes = alertTypes , "
                    + "ApimAlertStakeholderInfo.emails = emails , "
                    + "ApimAlertStakeholderInfo.isSubscriber = isSubscriber, "
                    + "ApimAlertStakeholderInfo.isPublisher = isPublisher, "
                    + "ApimAlertStakeholderInfo.isAdmin = isAdmin on "
                    + "ApimAlertStakeholderInfo.userId == userId and "
                    + "ApimAlertStakeholderInfo.isSubscriber == isSubscriber";

    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);
    apiMgtDAO.addAlertTypesConfigInfo(userName, emails, alertTypesMap.get("ids"), AlertMgtConstants.STORE_AGENT);
}
 
Example 12
Source File: StoreAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void unsubscribe(String userName) throws APIManagementException {

    apiMgtDAO.unSubscribeAlerts(userName, AlertMgtConstants.STORE_AGENT);
    String query = "delete ApimAlertStakeholderInfo on ApimAlertStakeholderInfo.userId == '" + userName + "' and "
            + "ApimAlertStakeholderInfo.isSubscriber == true";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_STAKEHOLDER_ALERT_APP, query);

}
 
Example 13
Source File: StoreAlertConfigurator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void removeAlertConfiguration(String userName, String alertName, Map<String, String> configProperties)
        throws APIManagementException {

    String applicationId = configProperties.get(AlertMgtConstants.APPLICATION_ID_KEY);
    String apiName = configProperties.get(AlertMgtConstants.API_NAME_KEY);
    String apiVersion = configProperties.get(AlertMgtConstants.API_VERSION_KEY);

    String query = "delete ApiSubAlertConf on ApiSubAlertConf.applicationId == '"
            + applicationId + "' and ApiSubAlertConf.apiName == '" + apiName
            + "' and ApiSubAlertConf.subscriber == '" + userName
            + "' and ApiSubAlertConf.apiVersion == '" + apiVersion + "'";
    APIUtil.executeQueryOnStreamProcessor(AlertMgtConstants.APIM_ALERT_CONFIG_APP, query);
}
 
Example 14
Source File: AlertTypesPublisher.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public void saveAndPublishAlertTypesEvent(String checkedAlertList, String emailList, String userName, String agent,
        String checkedAlertListValues) throws APIManagementException {

    if (!enabled || skipEventReceiverConnection) {
        throw new APIManagementException("Data publisher is not enabled");
    }

    if (publisher == null) {
        this.initializeDataPublisher();
    }
    String conditionClause = "";
    ApiMgtDAO apiMgtDAO = getApiMgtDao();
    //data persist in the database.
    apiMgtDAO.addAlertTypesConfigInfo(userName, emailList, checkedAlertList, agent);
    //set DTO
    AlertTypeDTO alertTypeDTO = new AlertTypeDTO();
    alertTypeDTO.setAlertTypes(checkedAlertListValues);
    alertTypeDTO.setEmails(emailList);
    alertTypeDTO.setUserName(userName);
    if ("publisher".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isPublisher == isPublisher";
        alertTypeDTO.setPublisher(true);
        alertTypeDTO.setSubscriber(false);
        alertTypeDTO.setAdmin(false);
    } else if ("subscriber".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isSubscriber == isSubscriber";
        alertTypeDTO.setSubscriber(true);
        alertTypeDTO.setPublisher(false);
        alertTypeDTO.setAdmin(false);
    } else if ("admin-dashboard".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isAdmin == isAdmin";
        alertTypeDTO.setSubscriber(false);
        alertTypeDTO.setPublisher(false);
        alertTypeDTO.setAdmin(true);
    }
    String appName = "APIM_STAKEHOLDER_ALERT";
    String query = "select '" + alertTypeDTO.getUserName() + "' as userId, '" + alertTypeDTO.getAlertTypes()
            + "' as alertTypes, '" + alertTypeDTO.getEmails() + "' as emails, " + alertTypeDTO.isSubscriber()
            + " as isSubscriber, " + alertTypeDTO.isPublisher() + " as isPublisher, " + alertTypeDTO.isAdmin()
            + " as isAdmin update or insert into ApimAlertStakeholderInfo set ApimAlertStakeholderInfo.userId = userId, "
            + "ApimAlertStakeholderInfo.alertTypes = alertTypes , ApimAlertStakeholderInfo.emails = emails ,"
            + " ApimAlertStakeholderInfo.isSubscriber = isSubscriber, ApimAlertStakeholderInfo.isPublisher = isPublisher, "
            + "ApimAlertStakeholderInfo.isAdmin = isAdmin on ApimAlertStakeholderInfo.userId == userId and "
            + conditionClause;
    APIUtil.executeQueryOnStreamProcessor(appName, query);

}
 
Example 15
Source File: AlertTypesPublisher.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method will delete all the data relating to the alert subscription by given user Name.
 *
 * @param userName logged in users name.
 */
public void unSubscribe(String userName, String agent) throws APIManagementException {

    if (!enabled || skipEventReceiverConnection) {
        throw new APIManagementException("Data publisher is not enabled");
    }

    if (publisher == null) {
        this.initializeDataPublisher();
    }
    String conditionClause = "";
    ApiMgtDAO apiMgtDAO = getApiMgtDao();
    //data persist in the database.
    apiMgtDAO.unSubscribeAlerts(userName, agent);
    //set DTO
    AlertTypeDTO alertTypeDTO = new AlertTypeDTO();
    alertTypeDTO.setAlertTypes("");
    alertTypeDTO.setEmails("");
    alertTypeDTO.setUserName(userName);
    if ("publisher".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isPublisher == true";
        alertTypeDTO.setPublisher(true);
        alertTypeDTO.setSubscriber(false);
        alertTypeDTO.setAdmin(false);
    } else if ("subscriber".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isSubscriber == true";
        alertTypeDTO.setSubscriber(true);
        alertTypeDTO.setPublisher(false);
        alertTypeDTO.setAdmin(false);
    } else if ("admin-dashboard".equals(agent)) {
        conditionClause = "ApimAlertStakeholderInfo.isAdmin == true";
        alertTypeDTO.setSubscriber(false);
        alertTypeDTO.setPublisher(false);
        alertTypeDTO.setAdmin(true);
    }
    String appName = "APIM_STAKEHOLDER_ALERT";
    String query =
            "delete ApimAlertStakeholderInfo  on ApimAlertStakeholderInfo.userId == '" + alertTypeDTO.getUserName()
                    + "' and " + conditionClause;
    APIUtil.executeQueryOnStreamProcessor(appName, query);

}
 
Example 16
Source File: ReportGenUtil.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Get microgateway request summary report as a pdf.
 * @param username
 * @param date month with year. Format should be yyyy-mm (ex: 2018-07)
 * @return inputstream InputStream of the pdf
 * @throws APIManagementException exception
 */
public static InputStream getMicroGatewayRequestSummaryReport(String username, String date)
        throws APIManagementException {
    InputStream pdfInputStream = null;
    // get data
    String value = "0"; //default 
    String appName = "APIM_ACCESS_SUMMARY";
    String query = "from ApiUserPerAppAgg on gatewayType=='MICRO' within '" + date 
            + "-** **:**:**' per 'months' SELECT sum(totalRequestCount) as sum;";
    
    JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(appName, query);
    JSONArray jarray =  (JSONArray) jsonObj.get("records");
    if(jarray != null && jarray.size() != 0) {
        JSONArray val = (JSONArray) jarray.get(0);
        Long result = (Long) val.get(0);
        value = String.valueOf(result);
    }

    //build data object to pass for generation
    TableData table = new TableData();
    String[] columnHeaders = { "", "Date", "Number of requests" };
    table.setColumnHeaders(columnHeaders);
    
    List<RowEntry> rowData = new ArrayList<RowEntry>();
    RowEntry entry = new RowEntry();
    entry.setEntry("1");
    entry.setEntry(date);
    entry.setEntry(value);
    rowData.add(entry);
    table.setRows(rowData);

    // generate the pdf
    ReportGenerator generator = new ReportGenerator();

    try {
        pdfInputStream =  generator.generateMGRequestSummeryPDF(table);
    } catch (COSVisitorException | IOException e) {
        String msg = "Error while generating the pdf for micro gateway request summary";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return pdfInputStream;
}