Search in sources :

Example 1 with SolaceTopicsObjectDTO

use of org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO in project carbon-apimgt by wso2.

the class SolaceStoreUtils method populateSolaceTopics.

/**
 * Populate Solace Topics from the response body
 *
 * @param solaceTopicsObjectDTO Solace Topic Object DTO
 * @param permissionsObject Json Object of the body
 * @param syntax      Protocol Syntax
 */
private static void populateSolaceTopics(SolaceTopicsObjectDTO solaceTopicsObjectDTO, org.json.JSONObject permissionsObject, String syntax) {
    SolaceTopicsDTO topicsDTO = new SolaceTopicsDTO();
    if (permissionsObject.getJSONArray("publish") != null) {
        List<String> publishTopics = new ArrayList<>();
        for (int j = 0; j < permissionsObject.getJSONArray("publish").length(); j++) {
            org.json.JSONObject channelObject = permissionsObject.getJSONArray("publish").getJSONObject(j);
            for (Object x : channelObject.keySet()) {
                org.json.JSONObject channel = channelObject.getJSONObject(x.toString());
                JSONArray channelPermissions = channel.getJSONArray("permissions");
                for (int k = 0; k < channelPermissions.length(); k++) {
                    if (!publishTopics.contains(channelPermissions.getString(k))) {
                        publishTopics.add(channelPermissions.getString(k));
                    }
                }
            }
        }
        topicsDTO.setPublishTopics(publishTopics);
    }
    if (permissionsObject.getJSONArray("subscribe") != null) {
        List<String> subscribeTopics = new ArrayList<>();
        for (int j = 0; j < permissionsObject.getJSONArray("subscribe").length(); j++) {
            org.json.JSONObject channelObject = permissionsObject.getJSONArray("subscribe").getJSONObject(j);
            for (Object x : channelObject.keySet()) {
                org.json.JSONObject channel = channelObject.getJSONObject(x.toString());
                JSONArray channelPermissions = channel.getJSONArray("permissions");
                for (int k = 0; k < channelPermissions.length(); k++) {
                    if (!subscribeTopics.contains(channelPermissions.getString(k))) {
                        subscribeTopics.add(channelPermissions.getString(k));
                    }
                }
            }
        }
        topicsDTO.setSubscribeTopics(subscribeTopics);
    }
    if (SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.equalsIgnoreCase(syntax)) {
        solaceTopicsObjectDTO.setMqttSyntax(topicsDTO);
    } else {
        solaceTopicsObjectDTO.setDefaultSyntax(topicsDTO);
    }
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) SolaceTopicsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsDTO)

Example 2 with SolaceTopicsObjectDTO

use of org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO in project carbon-apimgt by wso2.

the class SolaceStoreUtils method getSolaceDeployedEnvsInfo.

/**
 * Get SolaceDeployedEnvironmentDTO using admin APIs and map into DTOs to parse Devportal
 *
 * @param solaceEnvironment Solace environment Object
 * @param solaceOrganization Solace broker organization name
 * @param applicationUuid      Subscribed Application UUID
 * @return List of SolaceDeployedEnvironmentDTO to use in Devportal
 * @throws APIManagementException if error occurred when creating SolaceDeployedEnvironmentDTO
 */
public static List<SolaceDeployedEnvironmentDTO> getSolaceDeployedEnvsInfo(Environment solaceEnvironment, String solaceOrganization, String applicationUuid) throws APIManagementException {
    Map<String, Environment> gatewayEnvironmentMap = APIUtil.getReadOnlyGatewayEnvironments();
    // Create solace admin APIs instance
    SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    HttpResponse response = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, "default");
    List<SolaceDeployedEnvironmentDTO> solaceEnvironments = new ArrayList<>();
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        try {
            String responseString = EntityUtils.toString(response.getEntity());
            org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
            // Get solace environments attached with the Solace application
            if (jsonObject.getJSONArray("environments") != null) {
                JSONArray environmentsArray = jsonObject.getJSONArray("environments");
                for (int i = 0; i < environmentsArray.length(); i++) {
                    SolaceDeployedEnvironmentDTO solaceDeployedEnvironmentsDTO = new SolaceDeployedEnvironmentDTO();
                    org.json.JSONObject environmentObject = environmentsArray.getJSONObject(i);
                    // Get details of Solace environment attached to the solace application
                    if (environmentObject.getString("name") != null) {
                        String environmentName = environmentObject.getString("name");
                        Environment gatewayEnvironment = gatewayEnvironmentMap.get(environmentName);
                        if (gatewayEnvironment != null) {
                            // Set Solace environment details
                            solaceDeployedEnvironmentsDTO.setEnvironmentName(gatewayEnvironment.getName());
                            solaceDeployedEnvironmentsDTO.setEnvironmentDisplayName(gatewayEnvironment.getDisplayName());
                            solaceDeployedEnvironmentsDTO.setOrganizationName(gatewayEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION));
                            boolean containsMQTTProtocol = false;
                            // Get messaging protocols from the response body
                            if (environmentObject.getJSONArray("messagingProtocols") != null) {
                                List<SolaceURLsDTO> endpointUrls = new ArrayList<>();
                                JSONArray protocolsArray = environmentObject.getJSONArray("messagingProtocols");
                                for (int j = 0; j < protocolsArray.length(); j++) {
                                    SolaceURLsDTO solaceURLsDTO = new SolaceURLsDTO();
                                    String protocol = protocolsArray.getJSONObject(j).getJSONObject("protocol").getString("name");
                                    if (SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.equalsIgnoreCase(protocol)) {
                                        containsMQTTProtocol = true;
                                    }
                                    String uri = protocolsArray.getJSONObject(j).getString("uri");
                                    solaceURLsDTO.setProtocol(protocol);
                                    solaceURLsDTO.setEndpointURL(uri);
                                    endpointUrls.add(solaceURLsDTO);
                                }
                                solaceDeployedEnvironmentsDTO.setSolaceURLs(endpointUrls);
                            }
                            // Get topic permissions from the solace application response body
                            if (environmentObject.getJSONObject("permissions") != null) {
                                org.json.JSONObject permissionsObject = environmentObject.getJSONObject("permissions");
                                SolaceTopicsObjectDTO solaceTopicsObjectDTO = new SolaceTopicsObjectDTO();
                                populateSolaceTopics(solaceTopicsObjectDTO, permissionsObject, "default");
                                // Handle the special case of MQTT protocol
                                if (containsMQTTProtocol) {
                                    HttpResponse responseForMqtt = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
                                    org.json.JSONObject permissionsObjectForMqtt = extractPermissionsFromSolaceApplicationGetResponse(responseForMqtt, i, gatewayEnvironmentMap);
                                    if (permissionsObjectForMqtt != null) {
                                        populateSolaceTopics(solaceTopicsObjectDTO, permissionsObjectForMqtt, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
                                    }
                                }
                                solaceDeployedEnvironmentsDTO.setSolaceTopicsObject(solaceTopicsObjectDTO);
                            }
                        }
                    }
                    solaceEnvironments.add(solaceDeployedEnvironmentsDTO);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return solaceEnvironments;
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) IOException(java.io.IOException) SolaceTopicsObjectDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) SolaceDeployedEnvironmentDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)

Example 3 with SolaceTopicsObjectDTO

use of org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO in project carbon-apimgt by wso2.

the class AdditionalSubscriptionInfoMappingUtil method mapSolaceTopicObjects.

/**
 * Map SolaceTopicsObjectDTO details from Solace package to DevPortal DTOs
 *
 * @param solaceTopicsObject SolaceTopicsObjectDTO object from Solace package
 * @return AdditionalSubscriptionInfoSolaceTopicsObjectDTO object
 */
private static AdditionalSubscriptionInfoSolaceTopicsObjectDTO mapSolaceTopicObjects(SolaceTopicsObjectDTO solaceTopicsObject) {
    AdditionalSubscriptionInfoSolaceTopicsObjectDTO solaceTopicsObjectDTO = new AdditionalSubscriptionInfoSolaceTopicsObjectDTO();
    // Set default syntax object
    org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsDTO defaultSyntaxObject = solaceTopicsObject.getDefaultSyntax();
    SolaceTopicsDTO storeDefaultSolaceTopicObject = new SolaceTopicsDTO();
    storeDefaultSolaceTopicObject.setPublishTopics(defaultSyntaxObject.getPublishTopics());
    storeDefaultSolaceTopicObject.setSubscribeTopics(defaultSyntaxObject.getSubscribeTopics());
    solaceTopicsObjectDTO.setDefaultSyntax(storeDefaultSolaceTopicObject);
    // Set mqtt syntax object
    org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsDTO mqttSyntaxObject = solaceTopicsObject.getMqttSyntax();
    SolaceTopicsDTO storeMQTTSolaceTopicObject = new SolaceTopicsDTO();
    storeMQTTSolaceTopicObject.setPublishTopics(mqttSyntaxObject.getPublishTopics());
    storeMQTTSolaceTopicObject.setSubscribeTopics(mqttSyntaxObject.getSubscribeTopics());
    solaceTopicsObjectDTO.setMqttSyntax(storeMQTTSolaceTopicObject);
    return solaceTopicsObjectDTO;
}
Also used : SolaceTopicsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SolaceTopicsDTO) AdditionalSubscriptionInfoSolaceTopicsObjectDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceTopicsObjectDTO)

Aggregations

ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 IOException (java.io.IOException)1 HttpResponse (org.apache.http.HttpResponse)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Environment (org.wso2.carbon.apimgt.api.model.Environment)1 AdditionalSubscriptionInfoSolaceTopicsObjectDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceTopicsObjectDTO)1 SolaceTopicsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.SolaceTopicsDTO)1 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)1 SolaceDeployedEnvironmentDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)1 SolaceTopicsDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsDTO)1 SolaceTopicsObjectDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO)1 SolaceURLsDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO)1