use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.
the class APIProviderImpl method updateAPIProductSwagger.
@Override
public void updateAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
APIDefinition parser = new OAS3Parser();
SwaggerData updatedData = new SwaggerData(apiProduct);
String existingProductSwagger = getAPIDefinitionOfAPIProduct(apiProduct);
String updatedProductSwagger = parser.generateAPIDefinition(updatedData, existingProductSwagger);
updatedProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, updatedProductSwagger);
saveSwaggerDefinition(productId, updatedProductSwagger, orgId);
apiProduct.setDefinition(updatedProductSwagger);
}
use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.
the class APIMappingUtil method fromDTOtoAPIProduct.
public static APIProduct fromDTOtoAPIProduct(APIProductDTO dto, String provider) throws APIManagementException {
APIProduct product = new APIProduct();
APIProductIdentifier id = new APIProductIdentifier(APIUtil.replaceEmailDomain(provider), dto.getName(), // todo: replace this with dto.getVersion
APIConstants.API_PRODUCT_VERSION);
product.setID(id);
product.setUuid(dto.getId());
product.setDescription(dto.getDescription());
String context = dto.getContext();
if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) {
context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, "");
}
context = context.startsWith("/") ? context : ("/" + context);
String providerDomain = MultitenantUtils.getTenantDomain(provider);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(providerDomain) && dto.getId() == null) {
// Create tenant aware context for API
context = "/t/" + providerDomain + context;
}
product.setType(APIConstants.API_PRODUCT_IDENTIFIER_TYPE.replaceAll("\\s", ""));
product.setContext(context);
context = checkAndSetVersionParam(context);
product.setContextTemplate(context);
List<String> apiProductTags = dto.getTags();
Set<String> tagsToReturn = new HashSet<>(apiProductTags);
product.addTags(tagsToReturn);
if (dto.isEnableSchemaValidation() != null) {
product.setEnableSchemaValidation(dto.isEnableSchemaValidation());
}
product.setEnableStore(true);
if (dto.isResponseCachingEnabled() != null && dto.isResponseCachingEnabled()) {
product.setResponseCache(APIConstants.ENABLED);
} else {
product.setResponseCache(APIConstants.DISABLED);
}
if (dto.getCacheTimeout() != null) {
product.setCacheTimeout(dto.getCacheTimeout());
} else {
product.setCacheTimeout(APIConstants.API_RESPONSE_CACHE_TIMEOUT);
}
if (dto.getBusinessInformation() != null) {
product.setBusinessOwner(dto.getBusinessInformation().getBusinessOwner());
product.setBusinessOwnerEmail(dto.getBusinessInformation().getBusinessOwnerEmail());
product.setTechnicalOwner(dto.getBusinessInformation().getTechnicalOwner());
product.setTechnicalOwnerEmail(dto.getBusinessInformation().getTechnicalOwnerEmail());
}
Set<Tier> apiTiers = new HashSet<>();
List<String> tiersFromDTO = dto.getPolicies();
if (dto.getVisibility() != null) {
product.setVisibility(mapVisibilityFromDTOtoAPIProduct(dto.getVisibility()));
}
if (dto.getVisibleRoles() != null) {
String visibleRoles = StringUtils.join(dto.getVisibleRoles(), ',');
product.setVisibleRoles(visibleRoles);
}
if (dto.getVisibleTenants() != null) {
String visibleTenants = StringUtils.join(dto.getVisibleTenants(), ',');
product.setVisibleTenants(visibleTenants);
}
List<String> accessControlRoles = dto.getAccessControlRoles();
if (accessControlRoles == null || accessControlRoles.isEmpty()) {
product.setAccessControl(APIConstants.NO_ACCESS_CONTROL);
product.setAccessControlRoles("null");
} else {
product.setAccessControlRoles(StringUtils.join(accessControlRoles, ',').toLowerCase());
product.setAccessControl(APIConstants.API_RESTRICTED_VISIBILITY);
}
for (String tier : tiersFromDTO) {
apiTiers.add(new Tier(tier));
}
product.setAvailableTiers(apiTiers);
product.setProductLevelPolicy(dto.getApiThrottlingPolicy());
product.setGatewayVendor(dto.getGatewayVendor());
if (dto.getSubscriptionAvailability() != null) {
product.setSubscriptionAvailability(mapSubscriptionAvailabilityFromDTOtoAPIProduct(dto.getSubscriptionAvailability()));
}
List<APIInfoAdditionalPropertiesDTO> additionalProperties = dto.getAdditionalProperties();
if (additionalProperties != null) {
for (APIInfoAdditionalPropertiesDTO property : additionalProperties) {
if (property.isDisplay()) {
product.addProperty(property.getName() + APIConstants.API_RELATED_CUSTOM_PROPERTIES_SURFIX, property.getValue());
} else {
product.addProperty(property.getName(), property.getValue());
}
}
}
if (dto.getSubscriptionAvailableTenants() != null) {
product.setSubscriptionAvailableTenants(StringUtils.join(dto.getSubscriptionAvailableTenants(), ","));
}
String transports = StringUtils.join(dto.getTransport(), ',');
product.setTransports(transports);
List<APIProductResource> productResources = new ArrayList<APIProductResource>();
Set<String> verbResourceCombo = new HashSet<>();
for (ProductAPIDTO res : dto.getApis()) {
List<APIOperationsDTO> productAPIOperationsDTO = res.getOperations();
for (APIOperationsDTO resourceItem : productAPIOperationsDTO) {
if (!verbResourceCombo.add(resourceItem.getVerb() + resourceItem.getTarget())) {
throw new APIManagementException("API Product resource: " + resourceItem.getTarget() + ", with verb: " + resourceItem.getVerb() + " , is duplicated for id " + id, ExceptionCodes.from(ExceptionCodes.API_PRODUCT_DUPLICATE_RESOURCE, resourceItem.getTarget(), resourceItem.getVerb()));
}
URITemplate template = new URITemplate();
template.setHTTPVerb(resourceItem.getVerb());
template.setHttpVerbs(resourceItem.getVerb());
template.setResourceURI(resourceItem.getTarget());
template.setUriTemplate(resourceItem.getTarget());
template.setOperationPolicies(OperationPolicyMappingUtil.fromDTOToAPIOperationPoliciesList(resourceItem.getOperationPolicies()));
APIProductResource resource = new APIProductResource();
resource.setApiId(res.getApiId());
resource.setUriTemplate(template);
productResources.add(resource);
}
}
Set<Scope> scopes = getScopes(dto);
product.setScopes(scopes);
APICorsConfigurationDTO apiCorsConfigurationDTO = dto.getCorsConfiguration();
CORSConfiguration corsConfiguration;
if (apiCorsConfigurationDTO != null) {
corsConfiguration = new CORSConfiguration(apiCorsConfigurationDTO.isCorsConfigurationEnabled(), apiCorsConfigurationDTO.getAccessControlAllowOrigins(), apiCorsConfigurationDTO.isAccessControlAllowCredentials(), apiCorsConfigurationDTO.getAccessControlAllowHeaders(), apiCorsConfigurationDTO.getAccessControlAllowMethods());
} else {
corsConfiguration = APIUtil.getDefaultCorsConfiguration();
}
product.setCorsConfiguration(corsConfiguration);
product.setProductResources(productResources);
product.setApiSecurity(getSecurityScheme(dto.getSecurityScheme()));
product.setAuthorizationHeader(dto.getAuthorizationHeader());
// attach api categories to API model
setAPICategoriesToModel(dto, product, provider);
return product;
}
use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveOperationPolicySequenceForProducts.
private static GatewayContentDTO retrieveOperationPolicySequenceForProducts(APIProduct apiProduct, API api, String extractedLocation, String flow) throws APIManagementException {
Set<URITemplate> applicableURITemplates = new HashSet<>();
for (APIProductResource productResource : apiProduct.getProductResources()) {
if (productResource.getApiIdentifier().equals(api.getId())) {
applicableURITemplates.add(productResource.getUriTemplate());
}
}
String policySequence = null;
APIProductIdentifier apiProductIdentifier = apiProduct.getId();
String seqExt = APIUtil.getSequenceExtensionName(apiProductIdentifier.getName(), apiProductIdentifier.getVersion()).concat("--").concat(api.getUuid()).concat(SynapsePolicyAggregator.getSequenceExtensionFlow(flow));
try {
policySequence = SynapsePolicyAggregator.generatePolicySequenceForUriTemplateSet(applicableURITemplates, seqExt, flow, extractedLocation);
} catch (IOException e) {
throw new APIManagementException(e);
}
GatewayContentDTO operationPolicySequenceContentDto = new GatewayContentDTO();
if (StringUtils.isNotEmpty(policySequence)) {
try {
OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(policySequence.getBytes()));
if (omElement != null) {
if (omElement.getAttribute(new QName("name")) != null) {
omElement.getAttribute(new QName("name")).setAttributeValue(seqExt);
}
operationPolicySequenceContentDto.setName(seqExt);
operationPolicySequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
for (APIProductResource productResource : apiProduct.getProductResources()) {
if (productResource.getApiIdentifier().equals(api.getId())) {
switch(flow) {
case APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST:
productResource.setInSequenceName(seqExt);
break;
case APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE:
productResource.setOutSequenceName(seqExt);
break;
case APIConstants.OPERATION_SEQUENCE_TYPE_FAULT:
productResource.setFaultSequenceName(seqExt);
break;
}
}
}
return operationPolicySequenceContentDto;
}
} catch (Exception e) {
throw new APIManagementException(e);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveGatewayAPIDto.
public static GatewayAPIDTO retrieveGatewayAPIDto(APIProduct apiProduct, Environment environment, String tenantDomain, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
Map<String, APIDTO> apidtoMap = retrieveAssociatedApis(extractedFolderPath);
Map<String, APIDTO> associatedAPIsMap = convertAPIIdToDto(apidtoMap.values());
for (APIProductResource productResource : apiProduct.getProductResources()) {
String apiId = productResource.getApiId();
APIDTO apidto = associatedAPIsMap.get(apiId);
if (apidto != null) {
API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
productResource.setApiIdentifier(api.getId());
if (api.isAdvertiseOnly()) {
productResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
} else {
productResource.setEndpointConfig(api.getEndpointConfig());
}
if (StringUtils.isNotEmpty(api.getInSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_IN_EXT;
productResource.setInSequenceName(sequenceName);
}
if (StringUtils.isNotEmpty(api.getOutSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_OUT_EXT;
productResource.setOutSequenceName(sequenceName);
}
if (StringUtils.isNotEmpty(api.getFaultSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
productResource.setFaultSequenceName(sequenceName);
}
productResource.setProductIdentifier(apiProduct.getId());
productResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
}
}
APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(apiProduct, tenantDomain, clientCertificatesDTOList, convertAPIIdToDto(associatedAPIsMap.values()));
return createAPIGatewayDTOtoPublishAPI(environment, apiProduct, apiTemplateBuilder, tenantDomain, apidtoMap, clientCertificatesDTOList);
}
use of org.wso2.carbon.apimgt.api.model.APIProductResource in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPIProduct.
@Test
public void testResourceConfigContextForAPIProduct() throws Exception {
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier("admin", "TestAPIProduct", "1.0.0", UUID.randomUUID().toString());
APIProduct apiProduct = new APIProduct(apiProductIdentifier);
apiProduct.setProductResources(getAPIProductResources(apiProductIdentifier));
apiProduct.setType(APIConstants.API_PRODUCT);
ConfigContext configcontext = new APIConfigContext(apiProduct);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, apiProduct);
resourceConfigContext.validate();
VelocityContext context = resourceConfigContext.getContext();
Assert.assertNotNull(context.get("apiType"));
Assert.assertEquals(context.get("apiType"), APIConstants.API_PRODUCT);
Object aggregates = context.get("aggregates");
Assert.assertNotNull(aggregates);
Assert.assertTrue(aggregates instanceof List);
List<APIProductResource> apiProductResources = (List<APIProductResource>) aggregates;
Assert.assertTrue(assertAPIProductResourceList(apiProduct.getProductResources(), apiProductResources));
}
Aggregations