Search in sources :

Example 11 with APIResource

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);
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 12 with APIResource

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;
}
Also used : Enumeration(java.util.Enumeration) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) Properties(java.util.Properties) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 13 with APIResource

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);
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource)

Example 14 with APIResource

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;
    }
}
Also used : GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 15 with APIResource

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;
}
Also used : APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) ArrayList(java.util.ArrayList) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)14 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)13 ArrayList (java.util.ArrayList)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 HashMap (java.util.HashMap)10 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)7 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)7 Map (java.util.Map)6 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 API (org.wso2.carbon.apimgt.api.model.API)5 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)5 List (java.util.List)4 JSONObject (org.json.simple.JSONObject)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 APIResource (org.wso2.carbon.apimgt.core.models.APIResource)4 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)4 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)4