Search in sources :

Example 1 with SolaceURLsDTO

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

the class AdditionalSubscriptionInfoMappingUtil method mapSolaceURLsToStoreDTO.

/**
 * Sets the Endpoint URLs For Solace API according to the protocols
 *
 * @param organizationName Solace broker organization name
 * @param environmentName      Name of the  Solace environment
 * @param availableProtocols List of available protocols
 * @return List containing AdditionalSubscriptionInfoSolaceURLsDTO
 * @throws APIManagementException if error occurred when retrieving protocols URLs from Solace broker
 */
private static List<AdditionalSubscriptionInfoSolaceURLsDTO> mapSolaceURLsToStoreDTO(String organizationName, String environmentName, List<String> availableProtocols) throws APIManagementException {
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    Environment solaceEnvironment = null;
    // Get Solace broker environment details
    for (Map.Entry<String, Environment> entry : gatewayEnvironments.entrySet()) {
        if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
            solaceEnvironment = entry.getValue();
        }
    }
    if (solaceEnvironment != null) {
        List<SolaceURLsDTO> solaceURLsDTOEntries = SolaceStoreUtils.getSolaceURLsInfo(solaceEnvironment, organizationName, environmentName, availableProtocols);
        List<AdditionalSubscriptionInfoSolaceURLsDTO> solaceURLsDTOs = new ArrayList<>();
        for (SolaceURLsDTO entry : solaceURLsDTOEntries) {
            AdditionalSubscriptionInfoSolaceURLsDTO subscriptionInfoSolaceProtocolURLsDTO = new AdditionalSubscriptionInfoSolaceURLsDTO();
            subscriptionInfoSolaceProtocolURLsDTO.setProtocol(entry.getProtocol());
            subscriptionInfoSolaceProtocolURLsDTO.setEndpointURL(entry.getEndpointURL());
            solaceURLsDTOs.add(subscriptionInfoSolaceProtocolURLsDTO);
        }
        return solaceURLsDTOs;
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) Map(java.util.Map) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO)

Example 2 with SolaceURLsDTO

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

the class AdditionalSubscriptionInfoMappingUtil method fromAdditionalSubscriptionInfoToDTO.

/**
 * Converts a AdditionalSubscriptionInfo object into AdditionalSubscriptionInfoDTO
 *
 * @param subscription SubscribedAPI object
 * @param organization Identifier of the organization
 * @return SubscriptionDTO corresponds to SubscribedAPI object
 */
public static AdditionalSubscriptionInfoDTO fromAdditionalSubscriptionInfoToDTO(SubscribedAPI subscription, String organization) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    AdditionalSubscriptionInfoDTO additionalSubscriptionInfoDTO = new AdditionalSubscriptionInfoDTO();
    additionalSubscriptionInfoDTO.setSubscriptionId(subscription.getUUID());
    APIIdentifier apiId = subscription.getApiId();
    API api = null;
    if (apiId != null) {
        try {
            api = apiConsumer.getLightweightAPIByUUID(apiId.getUUID(), organization);
        } catch (APIManagementException e) {
            String msg = "User :" + username + " does not have access to the API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    additionalSubscriptionInfoDTO.setApiId(api.getUuid());
    // Set Application information
    Application application = subscription.getApplication();
    application = apiConsumer.getApplicationByUUID(application.getUUID());
    additionalSubscriptionInfoDTO.setApplicationId(subscription.getApplication().getUUID());
    additionalSubscriptionInfoDTO.setApplicationName(application.getName());
    additionalSubscriptionInfoDTO.setIsSolaceAPI(SolaceNotifierUtils.checkWhetherAPIDeployedToSolaceUsingRevision(api));
    if (additionalSubscriptionInfoDTO.isIsSolaceAPI()) {
        // Set Solace organization details if API is a Solace API
        additionalSubscriptionInfoDTO.setSolaceOrganization(SolaceNotifierUtils.getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment(api));
        Map<String, Environment> gatewayEnvironmentMap = APIUtil.getReadOnlyGatewayEnvironments();
        Environment solaceEnvironment = null;
        for (Map.Entry<String, Environment> entry : gatewayEnvironmentMap.entrySet()) {
            if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
                solaceEnvironment = entry.getValue();
            }
        }
        if (solaceEnvironment != null) {
            List<SolaceDeployedEnvironmentDTO> solaceDeployedEnvironmentsDTOS = SolaceStoreUtils.getSolaceDeployedEnvsInfo(solaceEnvironment, additionalSubscriptionInfoDTO.getSolaceOrganization(), application.getUUID());
            List<AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO> solaceEnvironments = new ArrayList<>();
            for (SolaceDeployedEnvironmentDTO solaceDeployedEnvironmentEntry : solaceDeployedEnvironmentsDTOS) {
                // Set Solace environment details
                AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO solaceDeployedEnvironmentsDTO = new AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO();
                solaceDeployedEnvironmentsDTO.setEnvironmentName(solaceDeployedEnvironmentEntry.getEnvironmentName());
                solaceDeployedEnvironmentsDTO.setEnvironmentDisplayName(solaceDeployedEnvironmentEntry.getEnvironmentDisplayName());
                solaceDeployedEnvironmentsDTO.setOrganizationName(solaceDeployedEnvironmentEntry.getOrganizationName());
                // Set Solace URLs
                List<AdditionalSubscriptionInfoSolaceURLsDTO> endpointUrls = new ArrayList<>();
                List<SolaceURLsDTO> solaceURLsDTOS = solaceDeployedEnvironmentEntry.getSolaceURLs();
                for (SolaceURLsDTO entry : solaceURLsDTOS) {
                    AdditionalSubscriptionInfoSolaceURLsDTO solaceURLsDTO = new AdditionalSubscriptionInfoSolaceURLsDTO();
                    solaceURLsDTO.setProtocol(entry.getProtocol());
                    solaceURLsDTO.setEndpointURL(entry.getEndpointURL());
                    endpointUrls.add(solaceURLsDTO);
                }
                solaceDeployedEnvironmentsDTO.setSolaceURLs(endpointUrls);
                // Set Solace Topic Objects
                solaceDeployedEnvironmentsDTO.setSolaceTopicsObject(mapSolaceTopicObjects(solaceDeployedEnvironmentEntry.getSolaceTopicsObject()));
                solaceEnvironments.add(solaceDeployedEnvironmentsDTO);
            }
            additionalSubscriptionInfoDTO.setSolaceDeployedEnvironments(solaceEnvironments);
        }
    }
    return additionalSubscriptionInfoDTO;
}
Also used : AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO) ArrayList(java.util.ArrayList) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO) AdditionalSubscriptionInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application) Map(java.util.Map) SolaceDeployedEnvironmentDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)

Example 3 with SolaceURLsDTO

use of org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO 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 4 with SolaceURLsDTO

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

the class SolaceStoreUtils method getSolaceURLsInfo.

/**
 * Get SolaceUrlsInfo using admin APIs and map into DTOs to parse Devportal
 *
 * @param solaceEnvironment Solace environment Object
 * @param organizationName Solace broker organization name
 * @param environmentName      Name of the  Solace environment
 * @param availableProtocols List of available protocols
 * @return List of SolaceURLsDTO
 * @throws APIManagementException if error occurred when creating
 */
public static List<SolaceURLsDTO> getSolaceURLsInfo(Environment solaceEnvironment, String organizationName, String environmentName, List<String> availableProtocols) throws APIManagementException {
    // Create solace admin APIs instance
    SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    List<SolaceURLsDTO> solaceURLsDTOs = new ArrayList<>();
    HttpResponse response = solaceAdminApis.environmentGET(organizationName, environmentName);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String responseString = null;
        try {
            responseString = EntityUtils.toString(response.getEntity());
            org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
            JSONArray protocols = jsonObject.getJSONArray("messagingProtocols");
            for (int i = 0; i < protocols.length(); i++) {
                org.json.JSONObject protocolDetails = protocols.getJSONObject(i);
                String protocolName = protocolDetails.getJSONObject("protocol").getString("name");
                // Get solace protocol URLs for available protocols
                if (availableProtocols.contains(protocolName)) {
                    String endpointURI = protocolDetails.getString("uri");
                    SolaceURLsDTO subscriptionInfoSolaceProtocolURLsDTO = new SolaceURLsDTO();
                    subscriptionInfoSolaceProtocolURLsDTO.setProtocol(protocolName);
                    subscriptionInfoSolaceProtocolURLsDTO.setEndpointURL(endpointURI);
                    solaceURLsDTOs.add(subscriptionInfoSolaceProtocolURLsDTO);
                }
            }
        } catch (IOException e) {
            throw new APIManagementException("Error occurred when retrieving protocols URLs from Solace " + "admin apis");
        }
    }
    return solaceURLsDTOs;
}
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) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

ArrayList (java.util.ArrayList)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 SolaceURLsDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO)4 Environment (org.wso2.carbon.apimgt.api.model.Environment)3 IOException (java.io.IOException)2 Map (java.util.Map)2 HttpResponse (org.apache.http.HttpResponse)2 JSONArray (org.json.JSONArray)2 AdditionalSubscriptionInfoSolaceURLsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO)2 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)2 SolaceDeployedEnvironmentDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 AdditionalSubscriptionInfoDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoDTO)1 AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO)1 SolaceTopicsObjectDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO)1