use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImpl method addAPIProductWithoutPublishingToGateway.
@Override
public Map<API, List<APIProductResource>> addAPIProductWithoutPublishingToGateway(APIProduct product) throws APIManagementException {
Map<API, List<APIProductResource>> apiToProductResourceMapping = new HashMap<>();
validateApiProductInfo(product);
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
if (log.isDebugEnabled()) {
log.debug("API Product details successfully added to the registry. API Product Name: " + product.getId().getName() + ", API Product Version : " + product.getId().getVersion() + ", API Product context : " + // todo: log context
"change");
}
List<APIProductResource> resources = product.getProductResources();
// list to hold resources which are actually in an existing api. If user has created an API product with invalid
// API or invalid resource of a valid API, that content will be removed .validResources array will have only
// legitimate apis
List<APIProductResource> validResources = new ArrayList<APIProductResource>();
for (APIProductResource apiProductResource : resources) {
API api;
String apiUUID;
if (apiProductResource.getProductIdentifier() != null) {
APIIdentifier productAPIIdentifier = apiProductResource.getApiIdentifier();
String emailReplacedAPIProviderName = APIUtil.replaceEmailDomain(productAPIIdentifier.getProviderName());
APIIdentifier emailReplacedAPIIdentifier = new APIIdentifier(emailReplacedAPIProviderName, productAPIIdentifier.getApiName(), productAPIIdentifier.getVersion());
apiUUID = apiMgtDAO.getUUIDFromIdentifier(emailReplacedAPIIdentifier, product.getOrganization());
api = getAPIbyUUID(apiUUID, product.getOrganization());
} else {
apiUUID = apiProductResource.getApiId();
api = getAPIbyUUID(apiUUID, product.getOrganization());
// if API does not exist, getLightweightAPIByUUID() method throws exception.
}
if (api != null) {
validateApiLifeCycleForApiProducts(api);
if (api.getSwaggerDefinition() != null) {
api.setSwaggerDefinition(getOpenAPIDefinition(apiUUID, product.getOrganization()));
}
if (!apiToProductResourceMapping.containsKey(api)) {
apiToProductResourceMapping.put(api, new ArrayList<>());
}
List<APIProductResource> apiProductResources = apiToProductResourceMapping.get(api);
apiProductResources.add(apiProductResource);
apiProductResource.setApiIdentifier(api.getId());
apiProductResource.setProductIdentifier(product.getId());
if (api.isAdvertiseOnly()) {
apiProductResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
} else {
apiProductResource.setEndpointConfig(api.getEndpointConfig());
}
apiProductResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
URITemplate uriTemplate = apiProductResource.getUriTemplate();
Map<String, URITemplate> templateMap = apiMgtDAO.getURITemplatesForAPI(api);
if (uriTemplate == null) {
// if no resources are define for the API, we ingore that api for the product
} else {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getResourceURI();
if (templateMap.containsKey(key)) {
// Since the template ID is not set from the request, we manually set it.
uriTemplate.setId(templateMap.get(key).getId());
// request has a valid API id and a valid resource. we add it to valid resource map
validResources.add(apiProductResource);
} else {
// ignore
log.warn("API with id " + apiProductResource.getApiId() + " does not have a resource " + uriTemplate.getResourceURI() + " with http method " + uriTemplate.getHTTPVerb());
}
}
}
}
// set the valid resources only
product.setProductResources(validResources);
// now we have validated APIs and it's resources inside the API product. Add it to database
String provider = APIUtil.replaceEmailDomain(product.getId().getProviderName());
// Set version timestamp
product.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
// Create registry artifact
String apiProductUUID = createAPIProduct(product);
product.setUuid(apiProductUUID);
// Add to database
apiMgtDAO.addAPIProduct(product, product.getOrganization());
return apiToProductResourceMapping;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImpl method checkResourceThrottlingTiersInURITemplates.
private void checkResourceThrottlingTiersInURITemplates(Set<URITemplate> uriTemplates, String tenantDomain) throws APIManagementException {
Map<String, Tier> tierMap = APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, tenantDomain);
if (tierMap != null) {
for (URITemplate template : uriTemplates) {
if (template.getThrottlingTier() != null && !tierMap.containsKey(template.getThrottlingTier())) {
String message = "Invalid x-throttling tier " + template.getThrottlingTier() + " found in api definition for resource " + template.getHTTPVerb() + " " + template.getUriTemplate();
log.error(message);
throw new APIManagementException(message);
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImpl method updateAPIResources.
/**
* Update resources of the API including local scopes and resource to scope attachments.
*
* @param api API
* @param tenantId Tenant Id
* @throws APIManagementException If fails to update local scopes of the API.
*/
private void updateAPIResources(API api, int tenantId) throws APIManagementException {
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
APIIdentifier apiIdentifier = api.getId();
// Get the new URI templates for the API
Set<URITemplate> uriTemplates = api.getUriTemplates();
// Get the existing local scope keys attached for the API
Set<String> oldLocalScopeKeys = apiMgtDAO.getAllLocalScopeKeysForAPI(api.getUuid(), tenantId);
// Get the existing URI templates for the API
Set<URITemplate> oldURITemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
// Get the new local scope keys from URI templates
Set<Scope> newLocalScopes = getScopesToRegisterFromURITemplates(api.getId().getApiName(), api.getOrganization(), uriTemplates);
Set<String> newLocalScopeKeys = newLocalScopes.stream().map(Scope::getKey).collect(Collectors.toSet());
// Get the existing versioned local scope keys attached for the API
Set<String> oldVersionedLocalScopeKeys = apiMgtDAO.getVersionedLocalScopeKeysForAPI(api.getUuid(), tenantId);
// Get the existing versioned local scope keys which needs to be removed (not updated) from the current updating
// API and remove them from the oldLocalScopeKeys set before sending to KM, so that they will not be removed
// from KM and can be still used by other versioned APIs.
Iterator oldLocalScopesItr = oldLocalScopeKeys.iterator();
while (oldLocalScopesItr.hasNext()) {
String oldLocalScopeKey = (String) oldLocalScopesItr.next();
// if the scope is used in versioned APIs and it is not in new local scope key set
if (oldVersionedLocalScopeKeys.contains(oldLocalScopeKey) && !newLocalScopeKeys.contains(oldLocalScopeKey)) {
// remove from old local scope key set which will be send to KM
oldLocalScopesItr.remove();
}
}
apiMgtDAO.updateURITemplates(api, tenantId);
if (log.isDebugEnabled()) {
log.debug("Successfully updated the URI templates of API: " + apiIdentifier + " in the database");
}
// Update the resource scopes of the API in KM.
// Need to remove the old local scopes and register new local scopes and, update the resource scope mappings
// using the updated URI templates of the API.
deleteScopes(oldLocalScopeKeys, tenantId);
addScopes(newLocalScopes, tenantId);
Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
if (keyManager != null) {
try {
keyManager.updateResourceScopes(api, oldLocalScopeKeys, newLocalScopes, oldURITemplates, uriTemplates);
if (log.isDebugEnabled()) {
log.debug("Successfully updated the resource scopes of API: " + apiIdentifier + " in Key Manager " + keyManagerDtoEntry.getKey() + " .");
}
} catch (APIManagementException e) {
log.error("Error while updating resource to scope attachment in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImpl method getRemovedProductResources.
@Override
public List<APIResource> getRemovedProductResources(Set<URITemplate> updatedUriTemplates, API existingAPI) {
Set<URITemplate> existingUriTemplates = existingAPI.getUriTemplates();
List<APIResource> removedReusedResources = new ArrayList<>();
for (URITemplate existingUriTemplate : existingUriTemplates) {
// If existing URITemplate is used by any API Products
if (!existingUriTemplate.retrieveUsedByProducts().isEmpty()) {
String existingVerb = existingUriTemplate.getHTTPVerb();
String existingPath = existingUriTemplate.getUriTemplate();
boolean isReusedResourceRemoved = true;
for (URITemplate updatedUriTemplate : updatedUriTemplates) {
String updatedVerb = updatedUriTemplate.getHTTPVerb();
String updatedPath = updatedUriTemplate.getUriTemplate();
// Check if existing reused resource is among updated resources
if (existingVerb.equalsIgnoreCase(updatedVerb) && existingPath.equalsIgnoreCase(updatedPath)) {
isReusedResourceRemoved = false;
break;
}
}
// Existing reused resource is not among updated resources
if (isReusedResourceRemoved) {
APIResource removedResource = new APIResource(existingVerb, existingPath);
removedReusedResources.add(removedResource);
}
}
}
return removedReusedResources;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIProviderImpl method setOperationPoliciesToURITemplates.
@Override
public void setOperationPoliciesToURITemplates(String apiId, Set<URITemplate> uriTemplates) throws APIManagementException {
Set<URITemplate> uriTemplatesWithPolicies = apiMgtDAO.getURITemplatesWithOperationPolicies(apiId);
if (!uriTemplatesWithPolicies.isEmpty()) {
// This is a temporary map to keep operation policies list of URI Templates against the URI mapping ID
Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplatesWithPolicies) {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
if (!operationPolicies.isEmpty()) {
operationPoliciesMap.put(key, operationPolicies);
}
}
for (URITemplate uriTemplate : uriTemplates) {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
if (operationPoliciesMap.containsKey(key)) {
uriTemplate.setOperationPolicies(operationPoliciesMap.get(key));
}
}
}
}
Aggregations