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();
}
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;
}
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;
}
Aggregations