Search in sources :

Example 1 with SharedScopeUsage

use of org.wso2.carbon.apimgt.api.model.SharedScopeUsage in project carbon-apimgt by wso2.

the class ScopesApiServiceImpl method getSharedScopeUsages.

@Override
public Response getSharedScopeUsages(String scopeId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    if (StringUtils.isEmpty(scopeId)) {
        throw new APIManagementException("Scope Id cannot be null or empty", ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
    }
    SharedScopeUsage sharedScopeUsage = apiProvider.getSharedScopeUsage(scopeId, tenantId);
    SharedScopeUsageDTO sharedScopeUsageDTO = SharedScopeMappingUtil.fromSharedScopeUsageToDTO(sharedScopeUsage);
    return Response.ok().entity(sharedScopeUsageDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SharedScopeUsageDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsageDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SharedScopeUsage(org.wso2.carbon.apimgt.api.model.SharedScopeUsage)

Example 2 with SharedScopeUsage

use of org.wso2.carbon.apimgt.api.model.SharedScopeUsage in project carbon-apimgt by wso2.

the class SharedScopeMappingUtil method fromSharedScopeUsageToDTO.

/**
 * Converts SharedScopeUsage object into SharedScopeUsageDTO object.
 *
 * @param sharedScopeUsage SharedScopeUsage object
 * @return SharedScopeUsageDTO object
 */
public static SharedScopeUsageDTO fromSharedScopeUsageToDTO(SharedScopeUsage sharedScopeUsage) {
    SharedScopeUsageDTO sharedScopeUsageDTO = new SharedScopeUsageDTO();
    sharedScopeUsageDTO.setId(sharedScopeUsage.getId());
    sharedScopeUsageDTO.setName(sharedScopeUsage.getName());
    List<SharedScopeUsedAPIInfoDTO> usedAPIInfoDTOList = new ArrayList<>();
    for (API api : sharedScopeUsage.getApis()) {
        APIIdentifier apiIdentifier = api.getId();
        SharedScopeUsedAPIInfoDTO usedAPIInfoDTO = new SharedScopeUsedAPIInfoDTO();
        usedAPIInfoDTO.setName(apiIdentifier.getName());
        usedAPIInfoDTO.setVersion(apiIdentifier.getVersion());
        usedAPIInfoDTO.setProvider(apiIdentifier.getProviderName());
        usedAPIInfoDTO.setContext(api.getContext());
        List<SharedScopeUsedAPIResourceInfoDTO> usedAPIResourceInfoDTOList = new ArrayList<>();
        for (URITemplate uriTemplate : api.getUriTemplates()) {
            SharedScopeUsedAPIResourceInfoDTO usedAPIResourceInfoDTO = new SharedScopeUsedAPIResourceInfoDTO();
            usedAPIResourceInfoDTO.setTarget(uriTemplate.getUriTemplate());
            usedAPIResourceInfoDTO.setVerb(uriTemplate.getHTTPVerb());
            usedAPIResourceInfoDTOList.add(usedAPIResourceInfoDTO);
        }
        usedAPIInfoDTO.setUsedResourceList(usedAPIResourceInfoDTOList);
        usedAPIInfoDTOList.add(usedAPIInfoDTO);
    }
    sharedScopeUsageDTO.setUsedApiList(usedAPIInfoDTOList);
    return sharedScopeUsageDTO;
}
Also used : SharedScopeUsedAPIResourceInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsedAPIResourceInfoDTO) ArrayList(java.util.ArrayList) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SharedScopeUsageDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsageDTO) SharedScopeUsedAPIInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsedAPIInfoDTO)

Example 3 with SharedScopeUsage

use of org.wso2.carbon.apimgt.api.model.SharedScopeUsage in project carbon-apimgt by wso2.

the class ApiMgtDAO method getSharedScopeUsage.

/**
 * Get the API and URI usages of the given shared scope
 *
 * @param uuid Id of the shared scope
 * @param tenantId tenant Id
 * @return usgaes ofr the shaerd scope
 * @throws APIManagementException If an error occurs while getting the usage details
 */
public SharedScopeUsage getSharedScopeUsage(String uuid, int tenantId) throws APIManagementException {
    SharedScopeUsage sharedScopeUsage;
    List<API> usedApiList = new ArrayList<>();
    String sharedScopeName = getSharedScopeKeyByUUID(uuid);
    if (sharedScopeName != null) {
        sharedScopeUsage = new SharedScopeUsage();
        sharedScopeUsage.setId(uuid);
        sharedScopeUsage.setName(sharedScopeName);
    } else {
        throw new APIMgtResourceNotFoundException("Shared Scope not found for scope ID: " + uuid, ExceptionCodes.from(ExceptionCodes.SHARED_SCOPE_NOT_FOUND, uuid));
    }
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement psForApiUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_API_USAGE_BY_TENANT)) {
        psForApiUsage.setString(1, uuid);
        psForApiUsage.setInt(2, tenantId);
        try (ResultSet apiUsageResultSet = psForApiUsage.executeQuery()) {
            while (apiUsageResultSet.next()) {
                String provider = apiUsageResultSet.getString("API_PROVIDER");
                String apiName = apiUsageResultSet.getString("API_NAME");
                String version = apiUsageResultSet.getString("API_VERSION");
                APIIdentifier apiIdentifier = new APIIdentifier(provider, apiName, version);
                API usedApi = new API(apiIdentifier);
                usedApi.setContext(apiUsageResultSet.getString("CONTEXT"));
                try (PreparedStatement psForUriUsage = connection.prepareStatement(SQLConstants.GET_SHARED_SCOPE_URI_USAGE_BY_TENANT)) {
                    int apiId = apiUsageResultSet.getInt("API_ID");
                    Set<URITemplate> usedUriTemplates = new LinkedHashSet<>();
                    psForUriUsage.setString(1, uuid);
                    psForUriUsage.setInt(2, tenantId);
                    psForUriUsage.setInt(3, apiId);
                    try (ResultSet uriUsageResultSet = psForUriUsage.executeQuery()) {
                        while (uriUsageResultSet.next()) {
                            URITemplate usedUriTemplate = new URITemplate();
                            usedUriTemplate.setUriTemplate(uriUsageResultSet.getString("URL_PATTERN"));
                            usedUriTemplate.setHTTPVerb(uriUsageResultSet.getString("HTTP_METHOD"));
                            usedUriTemplates.add(usedUriTemplate);
                        }
                    }
                    usedApi.setUriTemplates(usedUriTemplates);
                    usedApiList.add(usedApi);
                } catch (SQLException e) {
                    handleException("Failed to retrieve Resource usages of shared scope with scope ID " + uuid, e);
                }
            }
        }
        if (sharedScopeUsage != null) {
            sharedScopeUsage.setApis(usedApiList);
        }
        return sharedScopeUsage;
    } catch (SQLException e) {
        handleException("Failed to retrieve API usages of shared scope with scope ID" + uuid, e);
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) SharedScopeUsage(org.wso2.carbon.apimgt.api.model.SharedScopeUsage) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

ArrayList (java.util.ArrayList)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 SharedScopeUsage (org.wso2.carbon.apimgt.api.model.SharedScopeUsage)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 SharedScopeUsageDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsageDTO)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedHashSet (java.util.LinkedHashSet)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 SharedScopeUsedAPIInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsedAPIInfoDTO)1 SharedScopeUsedAPIResourceInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsedAPIResourceInfoDTO)1