use of org.wso2.carbon.apimgt.core.models.APIResource in project carbon-apimgt by wso2.
the class APIProviderImpl method registerOrUpdateResourceInKeyManager.
/**
* Notify the key manager with API update or addition
*
* @param api API
* @param tenantDomain
* @throws APIManagementException when error occurs when register/update API at Key Manager side
*/
private void registerOrUpdateResourceInKeyManager(API api, String tenantDomain) throws APIManagementException {
// get new key manager instance for resource registration.
Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
if (keyManager != null) {
try {
Map registeredResource = keyManager.getResourceByApiId(api.getId().toString());
if (registeredResource == null) {
boolean isNewResourceRegistered = keyManager.registerNewResource(api, null);
if (!isNewResourceRegistered) {
log.warn("APIResource registration is failed while adding the API- " + api.getId().getApiName() + "-" + api.getId().getVersion() + " into Key Manager : " + keyManagerDtoEntry.getKey());
}
} else {
// update APIResource.
String resourceId = (String) registeredResource.get("resourceId");
if (resourceId == null) {
handleException("APIResource update is failed because of empty resourceID.");
}
keyManager.updateRegisteredResource(api, registeredResource);
}
} catch (APIManagementException e) {
log.error("API Resource Registration failed in Key Manager " + keyManagerDtoEntry.getKey(), e);
}
}
}
}
use of org.wso2.carbon.apimgt.core.models.APIResource in project carbon-apimgt by wso2.
the class APIUtil method setResourceProperties.
/**
* To set the resource properties to the API.
*
* @param api API that need to set the resource properties.
* @param registry Registry to get the resource from.
* @param artifactPath Path of the API artifact.
* @return Updated API.
* @throws RegistryException Registry Exception.
*/
private static API setResourceProperties(API api, Registry registry, String artifactPath) throws RegistryException {
Resource apiResource = registry.get(artifactPath);
Properties properties = apiResource.getProperties();
if (properties != null) {
Enumeration propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
String propertyName = (String) propertyNames.nextElement();
if (log.isDebugEnabled()) {
log.debug("API '" + api.getId().toString() + "' " + "has the property " + propertyName);
}
if (propertyName.startsWith(APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX)) {
api.addProperty(propertyName.substring(APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX.length()), apiResource.getProperty(propertyName));
}
}
}
api.setAccessControl(apiResource.getProperty(APIConstants.ACCESS_CONTROL));
String accessControlRoles = null;
String displayPublisherRoles = apiResource.getProperty(APIConstants.DISPLAY_PUBLISHER_ROLES);
if (displayPublisherRoles == null) {
String publisherRoles = apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
if (publisherRoles != null) {
accessControlRoles = APIConstants.NULL_USER_ROLE_LIST.equals(apiResource.getProperty(APIConstants.PUBLISHER_ROLES)) ? null : apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
}
} else {
accessControlRoles = APIConstants.NULL_USER_ROLE_LIST.equals(displayPublisherRoles) ? null : displayPublisherRoles;
}
api.setAccessControlRoles(accessControlRoles);
return api;
}
use of org.wso2.carbon.apimgt.core.models.APIResource in project carbon-apimgt by wso2.
the class APIUtil method getActualEpPswdFromHiddenProperty.
/**
* This method is used to get the actual endpoint password of an API from the hidden property
* in the case where the handler APIEndpointPasswordRegistryHandler is enabled in registry.xml
*
* @param api The API
* @param registry The registry object
* @return The actual password of the endpoint if exists
* @throws RegistryException Throws if the api resource doesn't exist
*/
private static String getActualEpPswdFromHiddenProperty(API api, Registry registry) throws RegistryException {
String apiPath = APIUtil.getAPIPath(api.getId());
Resource apiResource = registry.get(apiPath);
return apiResource.getProperty(APIConstants.REGISTRY_HIDDEN_ENDPOINT_PROPERTY);
}
use of org.wso2.carbon.apimgt.core.models.APIResource in project carbon-apimgt by wso2.
the class APIUtil method getAPIArtifact.
/**
* Util method to return the artifact from a registry resource path
*
* @param apiIdentifier
* @param registry
* @return
* @throws APIManagementException
*/
public static GenericArtifact getAPIArtifact(APIIdentifier apiIdentifier, Registry registry) throws APIManagementException {
String apiPath = APIUtil.getAPIPath(apiIdentifier);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when getting generic artifact for API " + apiIdentifier.getApiName();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
try {
Resource apiResource = registry.get(apiPath);
String artifactId = apiResource.getUUID();
if (artifactId == null) {
throw new APIManagementException("artifact id is null for : " + apiPath);
}
return artifactManager.getGenericArtifact(artifactId);
} catch (RegistryException e) {
handleException("Failed to get API artifact from : " + apiPath, e);
return null;
}
}
use of org.wso2.carbon.apimgt.core.models.APIResource in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getUsedProductResources.
/**
* Get resources of an API that are reused by API Products
*
* @param api API
* @return List of resources reused by API Products
*/
private List<APIResource> getUsedProductResources(API api) {
List<APIResource> usedProductResources = new ArrayList<>();
Set<URITemplate> uriTemplates = api.getUriTemplates();
for (URITemplate uriTemplate : uriTemplates) {
// If existing URITemplate is used by any API Products
if (!uriTemplate.retrieveUsedByProducts().isEmpty()) {
APIResource apiResource = new APIResource(uriTemplate.getHTTPVerb(), uriTemplate.getUriTemplate());
usedProductResources.add(apiResource);
}
}
return usedProductResources;
}
Aggregations