use of org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPIProductbyUUID.
public APIProduct getAPIProductbyUUID(String uuid, String organization) throws APIManagementException {
try {
Organization org = new Organization(organization);
PublisherAPIProduct publisherAPIProduct = apiPersistenceInstance.getPublisherAPIProduct(org, uuid);
if (publisherAPIProduct != null) {
APIProduct product = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
product.setID(new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion(), uuid));
checkAccessControlPermission(userNameWithoutChange, product.getAccessControl(), product.getAccessControlRoles());
populateRevisionInformation(product, uuid);
populateAPIProductInformation(uuid, organization, product);
populateAPIStatus(product);
populateAPITier(product);
return product;
} else {
String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException | OASPersistenceException | ParseException e) {
String msg = "Failed to get API Product";
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException in project carbon-apimgt by wso2.
the class AbstractAPIManager method populateAPIInformation.
protected void populateAPIInformation(String uuid, String organization, API api) throws APIManagementException, OASPersistenceException, ParseException, AsyncSpecPersistenceException {
Organization org = new Organization(organization);
// UUID
if (api.getUuid() == null) {
api.setUuid(uuid);
}
api.setOrganization(organization);
// environment
String environmentString = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
// workflow status
APIIdentifier apiId = api.getId();
WorkflowDTO workflow;
String currentApiUuid = uuid;
if (api.isRevision() && api.getRevisionedApiId() != null) {
currentApiUuid = api.getRevisionedApiId();
}
workflow = APIUtil.getAPIWorkflowStatus(currentApiUuid, WF_TYPE_AM_API_STATE);
if (workflow != null) {
WorkflowStatus status = workflow.getStatus();
api.setWorkflowStatus(status.toString());
}
// TODO try to use a single query to get info from db
int internalId = apiMgtDAO.getAPIID(currentApiUuid);
apiId.setId(internalId);
apiMgtDAO.setServiceStatusInfoToAPI(api, internalId);
// api level tier
String apiLevelTier;
if (api.isRevision()) {
apiLevelTier = apiMgtDAO.getAPILevelTier(api.getRevisionedApiId(), api.getUuid());
} else {
apiLevelTier = apiMgtDAO.getAPILevelTier(internalId);
}
api.setApiLevelPolicy(apiLevelTier);
// available tier
String tiers = null;
Set<Tier> tiersSet = api.getAvailableTiers();
Set<String> tierNameSet = new HashSet<String>();
for (Tier t : tiersSet) {
tierNameSet.add(t.getName());
}
if (api.getAvailableTiers() != null) {
tiers = String.join("||", tierNameSet);
}
Map<String, Tier> definedTiers = APIUtil.getTiers(APIUtil.getInternalOrganizationId(organization));
Set<Tier> availableTier = APIUtil.getAvailableTiers(definedTiers, tiers, api.getId().getApiName());
api.setAvailableTiers(availableTier);
// Scopes
Map<String, Scope> scopeToKeyMapping = APIUtil.getAPIScopes(currentApiUuid, organization);
api.setScopes(new LinkedHashSet<>(scopeToKeyMapping.values()));
// templates
String resourceConfigsString;
if (api.getSwaggerDefinition() != null) {
resourceConfigsString = api.getSwaggerDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getOASDefinition(org, uuid);
}
api.setSwaggerDefinition(resourceConfigsString);
if (resourceConfigsString == null) {
if (api.getAsyncApiDefinition() != null) {
resourceConfigsString = api.getAsyncApiDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getAsyncDefinition(org, uuid);
}
api.setAsyncApiDefinition(resourceConfigsString);
}
if (api.getType() != null && APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
api.setGraphQLSchema(getGraphqlSchemaDefinition(uuid, organization));
}
JSONParser jsonParser = new JSONParser();
JSONObject paths = null;
if (resourceConfigsString != null) {
JSONObject resourceConfigsJSON = (JSONObject) jsonParser.parse(resourceConfigsString);
paths = (JSONObject) resourceConfigsJSON.get(APIConstants.SWAGGER_PATHS);
}
Set<URITemplate> uriTemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
for (URITemplate uriTemplate : uriTemplates) {
String uTemplate = uriTemplate.getUriTemplate();
String method = uriTemplate.getHTTPVerb();
List<Scope> oldTemplateScopes = uriTemplate.retrieveAllScopes();
List<Scope> newTemplateScopes = new ArrayList<>();
if (!oldTemplateScopes.isEmpty()) {
for (Scope templateScope : oldTemplateScopes) {
Scope scope = scopeToKeyMapping.get(templateScope.getKey());
newTemplateScopes.add(scope);
}
}
uriTemplate.addAllScopes(newTemplateScopes);
uriTemplate.setResourceURI(api.getUrl());
uriTemplate.setResourceSandboxURI(api.getSandboxUrl());
// AWS Lambda: set arn & timeout to URI template
if (paths != null) {
JSONObject path = (JSONObject) paths.get(uTemplate);
if (path != null) {
JSONObject operation = (JSONObject) path.get(method.toLowerCase());
if (operation != null) {
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME)) {
uriTemplate.setAmznResourceName((String) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME));
}
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)) {
uriTemplate.setAmznResourceTimeout(((Long) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)).intValue());
}
}
}
}
}
if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) {
for (URITemplate template : uriTemplates) {
template.setMediationScript(template.getAggregatedMediationScript());
}
}
api.setUriTemplates(uriTemplates);
// CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
}
// set category
List<APICategory> categories = api.getApiCategories();
if (categories != null) {
List<String> categoriesOfAPI = new ArrayList<String>();
for (APICategory apiCategory : categories) {
categoriesOfAPI.add(apiCategory.getName());
}
List<APICategory> categoryList = new ArrayList<>();
if (!categoriesOfAPI.isEmpty()) {
// category array retrieved from artifact has only the category name, therefore we need to fetch
// categories
// and fill out missing attributes before attaching the list to the api
List<APICategory> allCategories = APIUtil.getAllAPICategoriesOfOrganization(organization);
// todo-category: optimize this loop with breaks
for (String categoryName : categoriesOfAPI) {
for (APICategory category : allCategories) {
if (categoryName.equals(category.getName())) {
categoryList.add(category);
break;
}
}
}
}
api.setApiCategories(categoryList);
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getOASDefinition.
@Override
public String getOASDefinition(Organization org, String apiId) throws OASPersistenceException {
String apiTenantDomain = org.getName();
String definition = null;
boolean tenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(apiTenantDomain);
Registry registryType = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted;
GenericArtifact apiArtifact = getAPIArtifact(apiId, registryType);
if (apiArtifact != null) {
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String apiPath = GovernanceUtils.getArtifactPath(registryType, apiId);
int prependIndex = apiPath.lastIndexOf("/api");
String apiSourcePath = apiPath.substring(0, prependIndex);
String definitionPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
if (registryType.resourceExists(definitionPath)) {
Resource apiDocResource = registryType.get(definitionPath);
definition = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
return definition;
}
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Failed to get swagger documentation of API : " + apiId;
throw new OASPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
return definition;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testGetOASDefinition.
@Test
public void testGetOASDefinition() throws OASPersistenceException, 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 definitionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(apiProviderName) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
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 definition = "{\"swagger\":\"2.0\",\"info\":{\"description\":\"This is a sample server Petstore server\"}}";
Organization org = new Organization(SUPER_TENANT_DOMAIN);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
Mockito.when(registry.resourceExists(definitionPath)).thenReturn(true);
Resource oasResource = new ResourceImpl();
oasResource.setContent(definition.getBytes());
Mockito.when(registry.get(definitionPath)).thenReturn(oasResource);
String def = apiPersistenceInstance.getOASDefinition(org, apiUUID);
Assert.assertEquals("API oas definition does not match", definition, def);
}
use of org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException in project carbon-apimgt by wso2.
the class APIConsumerImpl method getAPIorAPIProductByUUID.
@Override
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String organization) throws APIManagementException {
try {
Organization org = new Organization(organization);
DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
if (devPortalApi != null) {
checkVisibilityPermission(userNameWithoutChange, devPortalApi.getVisibility(), devPortalApi.getVisibleRoles());
if (APIConstants.API_PRODUCT.equalsIgnoreCase(devPortalApi.getType())) {
APIProduct apiProduct = APIMapper.INSTANCE.toApiProduct(devPortalApi);
apiProduct.setID(new APIProductIdentifier(devPortalApi.getProviderName(), devPortalApi.getApiName(), devPortalApi.getVersion()));
populateAPIProductInformation(uuid, organization, apiProduct);
populateAPIStatus(apiProduct);
apiProduct = addTiersToAPI(apiProduct, organization);
return new ApiTypeWrapper(apiProduct);
} else {
API api = APIMapper.INSTANCE.toApi(devPortalApi);
populateDevPortalAPIInformation(uuid, organization, api);
populateDefaultVersion(api);
populateAPIStatus(api);
api = addTiersToAPI(api, organization);
return new ApiTypeWrapper(api);
}
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException | OASPersistenceException | ParseException e) {
String msg = "Failed to get API";
throw new APIManagementException(msg, e);
}
}
Aggregations