Search in sources :

Example 56 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class OASParserUtil method getAPIDefinition.

/**
 * This method returns api definition json for given api
 *
 * @param apiIdentifier api identifier
 * @param registry      user registry
 * @return api definition json as json string
 * @throws APIManagementException
 */
public static String getAPIDefinition(Identifier apiIdentifier, Registry registry) throws APIManagementException {
    String resourcePath = "";
    if (apiIdentifier instanceof APIIdentifier) {
        APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiIdentifier.getUUID());
        if (apiRevision != null && apiRevision.getApiUUID() != null) {
            resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
        } else {
            resourcePath = APIUtil.getOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
        }
    } else if (apiIdentifier instanceof APIProductIdentifier) {
        resourcePath = APIUtil.getAPIProductOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
    }
    JSONParser parser = new JSONParser();
    String apiDocContent = null;
    try {
        if (registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)) {
            Resource apiDocResource = registry.get(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME);
            apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
            parser.parse(apiDocContent);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Resource " + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
            }
        }
    } catch (RegistryException e) {
        handleException("Error while retrieving OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion(), e);
    } catch (ParseException e) {
        handleException("Error while parsing OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion() + " in " + resourcePath, e);
    }
    return apiDocContent;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Resource(org.wso2.carbon.registry.api.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 57 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class OASParserUtil method saveAPIDefinition.

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

Example 58 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class OASParserUtil method getAPIOpenAPIDefinitionTimeStamps.

/**
 * This method returns the timestamps for a given API
 *
 * @param apiIdentifier
 * @param registry
 * @return
 * @throws APIManagementException
 */
public static Map<String, String> getAPIOpenAPIDefinitionTimeStamps(APIIdentifier apiIdentifier, Registry registry) throws APIManagementException {
    Map<String, String> timeStampMap = new HashMap<String, String>();
    String resourcePath;
    APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiIdentifier.getUUID());
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
    } else {
        resourcePath = APIUtil.getOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
    }
    try {
        if (registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)) {
            Resource apiDocResource = registry.get(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME);
            Date lastModified = apiDocResource.getLastModified();
            Date createdTime = apiDocResource.getCreatedTime();
            if (lastModified != null) {
                timeStampMap.put("UPDATED_TIME", String.valueOf(lastModified.getTime()));
            } else {
                timeStampMap.put("CREATED_TIME", String.valueOf(createdTime.getTime()));
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Resource " + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
            }
        }
    } catch (RegistryException e) {
        handleException("Error while retrieving OpenAPI v2.0 or v3.0.0 updated time for " + apiIdentifier.getApiName() + '-' + apiIdentifier.getVersion(), e);
    }
    return timeStampMap;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) HashMap(java.util.HashMap) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Resource(org.wso2.carbon.registry.api.Resource) RegistryException(org.wso2.carbon.registry.api.RegistryException) Date(java.util.Date)

Example 59 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class GraphQLSchemaDefinition method saveGraphQLSchemaDefinition.

/**
 * 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);
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 60 with ResourcePath

use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.

the class AsyncApiParserUtil method getAPIDefinition.

/**
 * This method returns api definition json for given api
 *
 * @param apiIdentifier api identifier
 * @param registry user registry
 * @return api definition json as json string
 * @throws APIManagementException
 */
public static String getAPIDefinition(Identifier apiIdentifier, Registry registry) throws APIManagementException {
    String resourcePath = "";
    if (apiIdentifier instanceof APIIdentifier) {
        resourcePath = APIUtil.getAsyncAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
    }
    JSONParser parser = new JSONParser();
    String apiDocContent = null;
    try {
        if (registry.resourceExists(resourcePath + APIConstants.API_ASYNCAPI_DEFINITION_RESOURCE_NAME)) {
            Resource apiDocResource = registry.get(resourcePath + APIConstants.API_ASYNCAPI_DEFINITION_RESOURCE_NAME);
            apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
            parser.parse(apiDocContent);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Resource" + APIConstants.API_ASYNCAPI_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
            }
        }
    } catch (RegistryException e) {
        handleException("Error while retrieving AsyncAPI Definition for " + apiIdentifier.getName() + "-" + apiIdentifier.getVersion(), e);
    } catch (ParseException e) {
        handleException("Error while parsing AsyncAPI Definition for " + apiIdentifier.getName() + "-" + apiIdentifier.getVersion() + " in " + resourcePath, e);
    }
    return apiDocContent;
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)51 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)44 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)28 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 IOException (java.io.IOException)18 Registry (org.wso2.carbon.registry.core.Registry)16 Collection (org.wso2.carbon.registry.core.Collection)15 UserStoreException (org.wso2.carbon.user.api.UserStoreException)14 Test (org.junit.Test)13 Resource (org.wso2.carbon.registry.api.Resource)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ArrayList (java.util.ArrayList)11 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)11 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)11 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)11 JSONParser (org.json.simple.parser.JSONParser)10 ParseException (org.json.simple.parser.ParseException)10 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)10