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;
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations