use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAPIorAPIProductByUUID.
/**
* Get API or APIProduct by registry artifact id
*
* @param uuid Registry artifact id
* @param requestedTenantDomain tenantDomain for the registry
* @return ApiTypeWrapper wrapping the API or APIProduct of the provided artifact id
* @throws APIManagementException
*/
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String requestedTenantDomain) throws APIManagementException {
boolean tenantFlowStarted = false;
try {
Registry registry;
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (APIConstants.WSO2_ANONYMOUS_USER.equals(this.username)) {
registry = getRegistryService().getGovernanceUserRegistry(this.username, id);
} else if (this.tenantDomain != null && !this.tenantDomain.equals(requestedTenantDomain)) {
registry = getRegistryService().getGovernanceSystemRegistry(id);
} else {
registry = this.registry;
}
} else {
registry = this.registry;
}
GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(uuid);
if (apiArtifact != null) {
String type = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
if (APIConstants.API_PRODUCT.equals(type)) {
APIProduct apiProduct = getApiProduct(registry, apiArtifact);
String productTenantDomain = getTenantDomain(apiProduct.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(apiProduct.getVisibility())) {
return new ApiTypeWrapper(apiProduct);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(productTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API Product : " + apiProduct.getId().getName());
}
return new ApiTypeWrapper(apiProduct);
} else {
API api = getApiForPublishing(registry, apiArtifact);
String apiTenantDomain = getTenantDomain(api.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(api.getVisibility())) {
return new ApiTypeWrapper(api);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(apiTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API : " + api.getId().getApiName());
}
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 (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Failed to get API";
throw new APIManagementException(msg, e);
} finally {
if (tenantFlowStarted) {
endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ContentSearchResultNameComparator method compare.
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof API && o2 instanceof API) {
API api1 = (API) o1;
API api2 = (API) o2;
return nameComparator.compare(api1, api2);
} else if (o1 instanceof APIProduct && o2 instanceof APIProduct) {
APIProduct apiProduct1 = (APIProduct) o1;
APIProduct apiProduct2 = (APIProduct) o2;
return productNameComparator.compare(apiProduct1, apiProduct2);
} else if (o1 instanceof API && o2 instanceof APIProduct) {
API api = (API) o1;
APIProduct apiProduct = (APIProduct) o2;
return api.getId().getApiName().compareToIgnoreCase(apiProduct.getId().getName());
} else if (o1 instanceof APIProduct && o2 instanceof API) {
APIProduct apiProduct = (APIProduct) o1;
API api = (API) o2;
return apiProduct.getId().getName().compareToIgnoreCase(api.getId().getName());
} else if (o1 instanceof API && o2 instanceof Map.Entry) {
API api = (API) o1;
Documentation doc = getDocumentFromEntry(o2);
return api.getId().getApiName().compareToIgnoreCase(doc.getName());
} else if (o1 instanceof Map.Entry && o2 instanceof API) {
API api = (API) o2;
Documentation doc = getDocumentFromEntry(o1);
return api.getId().getApiName().compareToIgnoreCase(doc.getName());
} else if (o1 instanceof APIProduct && o2 instanceof Map.Entry) {
APIProduct apiProduct = (APIProduct) o1;
Documentation doc = getDocumentFromEntry(o2);
return apiProduct.getId().getName().compareToIgnoreCase(doc.getName());
} else if (o1 instanceof Map.Entry && o2 instanceof APIProduct) {
APIProduct apiProduct = (APIProduct) o2;
Documentation doc = getDocumentFromEntry(o1);
return apiProduct.getId().getName().compareToIgnoreCase(doc.getName());
} else {
Documentation doc1;
Documentation doc2;
if (((Map.Entry) o1).getValue() instanceof APIProduct && ((Map.Entry) o2).getValue() instanceof APIProduct) {
Map.Entry<Documentation, APIProduct> documentEntry1 = (Map.Entry<Documentation, APIProduct>) o1;
doc1 = documentEntry1.getKey();
Map.Entry<Documentation, APIProduct> documentEntry2 = (Map.Entry<Documentation, APIProduct>) o2;
doc2 = documentEntry2.getKey();
} else if (((Map.Entry) o1).getValue() instanceof API && ((Map.Entry) o2).getValue() instanceof APIProduct) {
Map.Entry<Documentation, API> documentEntry1 = (Map.Entry<Documentation, API>) o1;
doc1 = documentEntry1.getKey();
Map.Entry<Documentation, APIProduct> documentEntry2 = (Map.Entry<Documentation, APIProduct>) o2;
doc2 = documentEntry2.getKey();
} else if (((Map.Entry) o1).getValue() instanceof APIProduct && ((Map.Entry) o2).getValue() instanceof API) {
Map.Entry<Documentation, APIProduct> documentEntry1 = (Map.Entry<Documentation, APIProduct>) o1;
doc1 = documentEntry1.getKey();
Map.Entry<Documentation, API> documentEntry2 = (Map.Entry<Documentation, API>) o2;
doc2 = documentEntry2.getKey();
} else {
Map.Entry<Documentation, API> documentEntry1 = (Map.Entry<Documentation, API>) o1;
doc1 = documentEntry1.getKey();
Map.Entry<Documentation, API> documentEntry2 = (Map.Entry<Documentation, API>) o2;
doc2 = documentEntry2.getKey();
}
return doc1.getName().compareToIgnoreCase(doc2.getName());
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getPublisherAPIProduct.
@Override
public PublisherAPIProduct getPublisherAPIProduct(Organization org, String apiProductId) throws APIPersistenceException {
boolean tenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(org.getName());
tenantFlowStarted = holder.isTenantFlowStarted();
Registry registry = holder.getRegistry();
GenericArtifact apiArtifact = getAPIArtifact(apiProductId, registry);
if (apiArtifact != null) {
APIProduct apiProduct = RegistryPersistenceUtil.getAPIProduct(apiArtifact, registry);
String definitionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(apiProduct.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + apiProduct.getId().getName() + RegistryConstants.PATH_SEPARATOR + apiProduct.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
if (registry.resourceExists(definitionPath)) {
Resource apiDocResource = registry.get(definitionPath);
String apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
apiProduct.setDefinition(apiDocContent);
}
PublisherAPIProduct pubApi = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
pubApi.setApiProductName(apiProduct.getId().getName());
pubApi.setProviderName(apiProduct.getId().getProviderName());
pubApi.setVersion(apiProduct.getId().getVersion());
if (log.isDebugEnabled()) {
log.debug("API Product for id " + apiProductId + " : " + pubApi.toString());
}
return pubApi;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + apiProductId + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (RegistryException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} catch (APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class RegistryPersistenceUtil method getAPIProduct.
/**
* Retrieves api product artifact from registry
*
* @param artifact
* @param registry
* @return APIProduct
* @throws org.wso2.carbon.apimgt.api.APIManagementException
*/
public static APIProduct getAPIProduct(GovernanceArtifact artifact, Registry registry) throws APIManagementException {
APIProduct apiProduct;
try {
String artifactPath = GovernanceUtils.getArtifactPath(registry, artifact.getId());
String providerName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
String productName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String productVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(providerName, productName, productVersion);
apiProduct = new APIProduct(apiProductIdentifier);
setResourceProperties(apiProduct, registry, artifactPath);
// set uuid
apiProduct.setUuid(artifact.getId());
apiProduct.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
apiProduct.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
apiProduct.setState(getLcStateFromArtifact(artifact));
apiProduct.setThumbnailUrl(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
apiProduct.setVisibility(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY));
apiProduct.setVisibleRoles(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES));
apiProduct.setVisibleTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS));
apiProduct.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
apiProduct.setBusinessOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL));
apiProduct.setTechnicalOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER));
apiProduct.setTechnicalOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL));
apiProduct.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
apiProduct.setSubscriptionAvailableTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
apiProduct.setEnvironments(getEnvironments(artifact.getAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS)));
apiProduct.setTransports(artifact.getAttribute(APIConstants.API_OVERVIEW_TRANSPORTS));
apiProduct.setApiSecurity(artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY));
apiProduct.setAuthorizationHeader(artifact.getAttribute(APIConstants.API_OVERVIEW_AUTHORIZATION_HEADER));
apiProduct.setCorsConfiguration(getCorsConfigurationFromArtifact(artifact));
apiProduct.setCreatedTime(registry.get(artifactPath).getCreatedTime());
apiProduct.setLastUpdated(registry.get(artifactPath).getLastModified());
apiProduct.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
apiProduct.setGatewayVendor(artifact.getAttribute(APIConstants.API_GATEWAY_VENDOR));
String tenantDomainName = MultitenantUtils.getTenantDomain(replaceEmailDomainBack(providerName));
apiProduct.setTenantDomain(tenantDomainName);
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomainName);
String tiers = artifact.getAttribute(APIConstants.API_OVERVIEW_TIER);
Set<Tier> availableTiers = new HashSet<Tier>();
if (tiers != null) {
String[] tiersArray = tiers.split("\\|\\|");
for (String tierName : tiersArray) {
availableTiers.add(new Tier(tierName));
}
}
apiProduct.setAvailableTiers(availableTiers);
// We set the context template here
apiProduct.setContextTemplate(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
apiProduct.setEnableSchemaValidation(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENABLE_JSON_SCHEMA)));
apiProduct.setEnableStore(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE)));
apiProduct.setTestKey(artifact.getAttribute(APIConstants.API_OVERVIEW_TESTKEY));
apiProduct.setResponseCache(artifact.getAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING));
int cacheTimeout = APIConstants.API_RESPONSE_CACHE_TIMEOUT;
try {
cacheTimeout = Integer.parseInt(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT));
} catch (NumberFormatException e) {
if (log.isDebugEnabled()) {
log.debug("Error in converting cache time out due to " + e.getMessage());
}
}
apiProduct.setCacheTimeout(cacheTimeout);
Set<String> tags = new HashSet<String>();
Tag[] tag = registry.getTags(artifactPath);
for (Tag tag1 : tag) {
tags.add(tag1.getTagName());
}
apiProduct.addTags(tags);
/*
*/
// set data and status related to monetization
apiProduct.setMonetizationStatus(Boolean.parseBoolean(artifact.getAttribute(APIConstants.Monetization.API_MONETIZATION_STATUS)));
String monetizationInfo = artifact.getAttribute(APIConstants.Monetization.API_MONETIZATION_PROPERTIES);
if (StringUtils.isNotBlank(monetizationInfo)) {
JSONParser parser = new JSONParser();
JSONObject jsonObj = (JSONObject) parser.parse(monetizationInfo);
apiProduct.setMonetizationProperties(jsonObj);
}
apiProduct.setApiCategories(getAPICategoriesFromAPIGovernanceArtifact(artifact, tenantId));
} catch (GovernanceException e) {
String msg = "Failed to get API Product for artifact ";
throw new APIManagementException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to get LastAccess time or Rating";
throw new APIManagementException(msg, e);
} catch (UserStoreException e) {
String msg = "Failed to get User Realm of API Product Provider";
throw new APIManagementException(msg, e);
} catch (ParseException e) {
String msg = "Failed to get parse monetization information.";
throw new APIManagementException(msg, e);
}
return apiProduct;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class PersistenceHelper method getSampleAPIProductArtifact.
public static GenericArtifact getSampleAPIProductArtifact() throws GovernanceException {
GenericArtifact artifact = new GenericArtifactImpl(new QName("", "APIProductTest", ""), "application/vnd.wso2-api+xml");
artifact.setAttribute("overview_transports", "http,https");
artifact.setAttribute("monetizationProperties", "{}");
artifact.setAttribute("overview_authorizationHeader", "Authorization");
artifact.setAttribute("overview_visibleTenants", null);
artifact.setAttribute("overview_name", "APIProductTest");
artifact.setAttribute("overview_apiSecurity", "oauth2,oauth_basic_auth_api_key_mandatory,basic_auth,api_key");
artifact.setAttribute("overview_environments", "Default");
artifact.setAttribute("overview_subscriptionAvailability", "all_tenants");
artifact.setAttribute("overview_context", "/prodcontext");
artifact.setAttribute("overview_visibility", "restricted");
artifact.setAttribute("overview_cacheTimeout", "300");
artifact.setAttribute("overview_provider", "admin");
artifact.setAttribute("overview_visibleRoles", "admin");
artifact.setAttribute("apiCategories_categoryName", "testcategory");
artifact.setAttribute("overview_contextTemplate", "/prodcontext/{version}");
artifact.setAttribute("overview_thumbnail", null);
artifact.setAttribute("overview_description", "sample product");
artifact.setAttribute("overview_technicalOwner", null);
artifact.setAttribute("overview_type", "APIProduct");
artifact.setAttribute("overview_technicalOwnerEmail", null);
artifact.setAttribute("overview_corsConfiguration", "{\"corsConfigurationEnabled\":false," + "\"accessControlAllowOrigins\":[\"*\"],\"accessControlAllowCredentials\":false," + "\"accessControlAllowHeaders\":[\"authorization\",\"Access-Control-Allow-Origin\",\"Content-Type\"," + "\"SOAPAction\",\"apikey\",\"testKey\"],\"accessControlAllowMethods\":[\"GET\",\"PUT\",\"POST\"," + "\"DELETE\",\"PATCH\",\"OPTIONS\"]}");
artifact.setAttribute("overview_versionType", "context");
artifact.setAttribute("overview_status", "PUBLISHED");
artifact.setAttribute("overview_businessOwner", "productOwner");
artifact.setAttribute("overview_version", "1.0.0");
artifact.setAttribute("overview_tenants", null);
artifact.setAttribute("overview_responseCaching", "Disabled");
artifact.setAttribute("overview_tier", "Bronze||Gold");
artifact.setAttribute("overview_businessOwnerEmail", "owner@test.com");
artifact.setAttribute("isMonetizationEnabled", "false");
artifact.setAttribute("overview_enableStore", "true");
artifact.setAttribute("overview_enableSchemaValidation", "false");
artifact.setId("88e758b7-6924-4e9f-8882-431070b6492b");
return artifact;
}
Aggregations