use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveGraphQLSchemaDefinition.
@Override
public void saveGraphQLSchemaDefinition(Organization org, String apiId, String schemaDefinition) throws GraphQLPersistenceException {
boolean tenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
BasicAPI api = getbasicAPIInfo(apiId, registry);
if (api == null) {
throw new GraphQLPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
}
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion + RegistryConstants.PATH_SEPARATOR;
String saveResourcePath = path + api.apiProvider + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + api.apiName + api.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);
}
// Need to set anonymous if the visibility is public
RegistryPersistenceUtil.clearResourcePermissions(saveResourcePath, new APIIdentifier(api.apiProvider, api.apiName, api.apiVersion), ((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.apiProvider, api.visibility, api.visibleRoles, saveResourcePath);
} catch (RegistryException | APIManagementException | APIPersistenceException e) {
throw new GraphQLPersistenceException("Error while adding Graphql Definition for api " + apiId, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getGraphQLSchema.
@Override
public String getGraphQLSchema(Organization org, String apiId) throws GraphQLPersistenceException {
boolean tenantFlowStarted = false;
String schemaDoc = null;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
BasicAPI api = getbasicAPIInfo(apiId, registry);
if (api == null) {
throw new GraphQLPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
}
String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
int prependIndex = apiPath.lastIndexOf("/api");
String apiSourcePath = apiPath.substring(0, prependIndex);
String schemaName = api.apiProvider + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + api.apiName + api.apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
String schemaResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + schemaName;
if (registry.resourceExists(schemaResourcePath)) {
Resource schemaResource = registry.get(schemaResourcePath);
schemaDoc = IOUtils.toString(schemaResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
}
} catch (APIPersistenceException | RegistryException | IOException e) {
throw new GraphQLPersistenceException("Error while accessing graphql schema definition ", e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
return schemaDoc;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testGetGraphQLSchema.
@Test
public void testGetGraphQLSchema() throws GraphQLPersistenceException, RegistryException {
Registry registry = Mockito.mock(Registry.class);
GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
String apiUUID = artifact.getId();
String apiProviderName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR;
String schemaName = apiProviderName + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
String schemaResourePath = path + schemaName;
String apiPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + "api";
PowerMockito.when(GovernanceUtils.getArtifactPath(registry, apiUUID)).thenReturn(apiPath);
String schema = "{\n" + " hero {\n" + " name\n" + " friends {\n" + " name\n" + " }\n" + " }\n" + "}";
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
Mockito.when(registry.resourceExists(schemaResourePath)).thenReturn(true);
Resource oasResource = new ResourceImpl();
oasResource.setContent(schema.getBytes());
Mockito.when(registry.get(schemaResourePath)).thenReturn(oasResource);
String def = apiPersistenceInstance.getGraphQLSchema(org, apiUUID);
Assert.assertEquals("API graphql schema does not match", schema, def);
}
Aggregations