Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#setResourcePermissions()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#setResourcePermissions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphQLSchemaDefinition.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method saves schema definition of GraphQL APIs in the registry
 *
 * @param api               API to be saved
 * @param schemaDefinition  Graphql API definition as String
 * @param registry          user registry
 * @throws APIManagementException
 */
public void saveGraphQLSchemaDefinition(API api, String schemaDefinition, Registry registry)
        throws APIManagementException {
    String apiName = api.getId().getApiName();
    String apiVersion = api.getId().getVersion();
    String apiProviderName = api.getId().getProviderName();
    String resourcePath = APIUtil.getGraphqlDefinitionFilePath(apiName, apiVersion, apiProviderName);
    try {
        String saveResourcePath = resourcePath + apiProviderName + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR +
                apiName + apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
        Resource resource;
        if (!registry.resourceExists(saveResourcePath)) {
            resource = registry.newResource();
        } else {
            resource = registry.get(saveResourcePath);
        }

        resource.setContent(schemaDefinition);
        resource.setMediaType(String.valueOf(ContentType.TEXT_PLAIN));
        registry.put(saveResourcePath, resource);
        if (log.isDebugEnabled()) {
            log.debug("Successfully imported the schema: " + schemaDefinition );
        }

        String[] visibleRoles = null;
        if (api.getVisibleRoles() != null) {
            visibleRoles = api.getVisibleRoles().split(",");
        }

        //Need to set anonymous if the visibility is public
        APIUtil.clearResourcePermissions(resourcePath, api.getId(), ((UserRegistry) registry).getTenantId());
        APIUtil.setResourcePermissions(apiProviderName, api.getVisibility(), visibleRoles, resourcePath);

    } catch (RegistryException e) {
        String errorMessage = "Error while adding Graphql Definition for " + apiName + '-' + apiVersion;
        log.error(errorMessage, e);
        handleException(errorMessage, e);
    }
}
 
Example 2
Source File: OASParserUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method saves api definition json in the registry
 *
 * @param api               API to be saved
 * @param apiDefinitionJSON API definition as JSON string
 * @param registry          user registry
 * @throws APIManagementException
 */
public static void saveAPIDefinition(API api, String apiDefinitionJSON, Registry registry)
        throws APIManagementException {
    String apiName = api.getId().getApiName();
    String apiVersion = api.getId().getVersion();
    String apiProviderName = api.getId().getProviderName();

    try {
        String resourcePath = APIUtil.getOpenAPIDefinitionFilePath(apiName, apiVersion, apiProviderName);
        resourcePath = resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
        Resource resource;
        if (!registry.resourceExists(resourcePath)) {
            resource = registry.newResource();
        } else {
            resource = registry.get(resourcePath);
        }
        resource.setContent(apiDefinitionJSON);
        resource.setMediaType("application/json");
        registry.put(resourcePath, resource);

        String[] visibleRoles = null;
        if (api.getVisibleRoles() != null) {
            visibleRoles = api.getVisibleRoles().split(",");
        }

        //Need to set anonymous if the visibility is public
        APIUtil.clearResourcePermissions(resourcePath, api.getId(), ((UserRegistry) registry).getTenantId());
        APIUtil.setResourcePermissions(apiProviderName, api.getVisibility(), visibleRoles, resourcePath);

    } catch (RegistryException e) {
        handleException("Error while adding Swagger Definition for " + apiName + '-' + apiVersion, e);
    }
}