use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.
the class PublisherCommonUtils method addAPIProductWithGeneratedSwaggerDefinition.
/**
* Add API Product with the generated swagger from the DTO.
*
* @param apiProductDTO API Product DTO
* @param username Username
* @param organization Identifier of the organization
* @return Created API Product object
* @throws APIManagementException Error while creating the API Product
* @throws FaultGatewaysException Error while adding the API Product to gateway
*/
public static APIProduct addAPIProductWithGeneratedSwaggerDefinition(APIProductDTO apiProductDTO, String username, String organization) throws APIManagementException, FaultGatewaysException {
username = StringUtils.isEmpty(username) ? RestApiCommonUtil.getLoggedInUsername() : username;
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
// if not add product
String provider = apiProductDTO.getProvider();
String context = apiProductDTO.getContext();
if (!StringUtils.isBlank(provider) && !provider.equals(username)) {
if (!APIUtil.hasPermission(username, APIConstants.Permissions.APIM_ADMIN)) {
if (log.isDebugEnabled()) {
log.debug("User " + username + " does not have admin permission (" + APIConstants.Permissions.APIM_ADMIN + ") hence provider (" + provider + ") overridden with current user (" + username + ")");
}
provider = username;
}
} else {
// Set username in case provider is null or empty
provider = username;
}
List<String> tiersFromDTO = apiProductDTO.getPolicies();
Set<Tier> definedTiers = apiProvider.getTiers();
List<String> invalidTiers = PublisherCommonUtils.getInvalidTierNames(definedTiers, tiersFromDTO);
if (!invalidTiers.isEmpty()) {
throw new APIManagementException("Specified tier(s) " + Arrays.toString(invalidTiers.toArray()) + " are invalid", ExceptionCodes.TIER_NAME_INVALID);
}
if (apiProductDTO.getAdditionalProperties() != null) {
String errorMessage = PublisherCommonUtils.validateAdditionalProperties(apiProductDTO.getAdditionalProperties());
if (!errorMessage.isEmpty()) {
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.INVALID_ADDITIONAL_PROPERTIES, apiProductDTO.getName()));
}
}
if (apiProductDTO.getVisibility() == null) {
// set the default visibility to PUBLIC
apiProductDTO.setVisibility(APIProductDTO.VisibilityEnum.PUBLIC);
}
if (apiProductDTO.getAuthorizationHeader() == null) {
apiProductDTO.setAuthorizationHeader(APIUtil.getOAuthConfigurationFromAPIMConfig(APIConstants.AUTHORIZATION_HEADER));
}
if (apiProductDTO.getAuthorizationHeader() == null) {
apiProductDTO.setAuthorizationHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT);
}
// Remove the /{version} from the context.
if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) {
context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, "");
}
// Make sure context starts with "/". ex: /pizzaProduct
context = context.startsWith("/") ? context : ("/" + context);
// Check whether the context already exists
if (apiProvider.isContextExist(context)) {
throw new APIManagementException("Error occurred while adding API Product. API Product with the context " + context + " already " + "exists.", ExceptionCodes.from(ExceptionCodes.API_PRODUCT_CONTEXT_ALREADY_EXISTS, context));
}
// Set default gatewayVendor
if (apiProductDTO.getGatewayVendor() == null) {
apiProductDTO.setGatewayVendor(APIConstants.WSO2_GATEWAY_ENVIRONMENT);
}
APIProduct productToBeAdded = APIMappingUtil.fromDTOtoAPIProduct(apiProductDTO, provider);
productToBeAdded.setOrganization(organization);
if (!APIConstants.PROTOTYPED.equals(productToBeAdded.getState())) {
productToBeAdded.setState(APIConstants.CREATED);
}
APIProductIdentifier createdAPIProductIdentifier = productToBeAdded.getId();
Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.addAPIProductWithoutPublishingToGateway(productToBeAdded);
APIProduct createdProduct = apiProvider.getAPIProduct(createdAPIProductIdentifier);
apiProvider.addAPIProductSwagger(createdProduct.getUuid(), apiToProductResourceMapping, createdProduct, organization);
createdProduct = apiProvider.getAPIProduct(createdAPIProductIdentifier);
return createdProduct;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.
the class ExportUtils method addAPIProductMetaInformationToArchive.
/**
* Retrieve meta information of the API Product to export and store it in the archive directory.
* URL template information are stored in swagger.json definition while rest of the required
* data are in api.json
*
* @param archivePath Folder path to export meta information
* @param apiProductDtoToReturn APIProductDTO to be exported
* @param exportFormat Export format of file
* @param apiProvider API Provider
* @throws APIImportExportException If an error occurs while exporting meta information
*/
public static void addAPIProductMetaInformationToArchive(String archivePath, APIProductDTO apiProductDtoToReturn, ExportFormat exportFormat, APIProvider apiProvider) throws APIImportExportException {
CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.DEFINITIONS_DIRECTORY);
try {
String formattedSwaggerJson = apiProvider.getAPIDefinitionOfAPIProduct(APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToReturn, apiProductDtoToReturn.getProvider()));
CommonUtil.writeToYamlOrJson(archivePath + ImportExportConstants.SWAGGER_DEFINITION_LOCATION, exportFormat, formattedSwaggerJson);
if (log.isDebugEnabled()) {
log.debug("Meta information retrieved successfully for API Product: " + apiProductDtoToReturn.getName());
}
CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.API_PRODUCT_FILE_LOCATION, exportFormat, ImportExportConstants.TYPE_API_PRODUCT, apiProductDtoToReturn);
} catch (APIManagementException e) {
throw new APIImportExportException("Error while retrieving Swagger definition for API Product: " + apiProductDtoToReturn.getName(), e);
} catch (IOException e) {
throw new APIImportExportException("Error while saving as YAML for API Product: " + apiProductDtoToReturn.getName(), e);
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.
the class ExportUtils method addDependentAPIsToArchive.
/**
* Retrieve dependent APIs by checking the resources of the API Product and store those in the archive directory.
*
* @param archivePath Temp location to save the API artifacts
* @param apiProductDtoToReturn API Product DTO which the resources should be considered
* @param userName User name of the requester
* @param provider API Product Provider
* @param exportFormat Export format of the API meta data, could be yaml or json
* @param isStatusPreserved Whether API status is preserved while export
* @param organization Organization
* @throws APIImportExportException If an error occurs while creating the directory or extracting the archive
* @throws APIManagementException If an error occurs while retrieving API related resources
*/
public static void addDependentAPIsToArchive(String archivePath, APIProductDTO apiProductDtoToReturn, ExportFormat exportFormat, APIProvider provider, String userName, Boolean isStatusPreserved, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIImportExportException, APIManagementException {
String apisDirectoryPath = archivePath + File.separator + ImportExportConstants.APIS_DIRECTORY;
CommonUtil.createDirectory(apisDirectoryPath);
List<ProductAPIDTO> apisList = apiProductDtoToReturn.getApis();
for (ProductAPIDTO productAPIDTO : apisList) {
String apiProductRequesterDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
API api = provider.getAPIbyUUID(productAPIDTO.getApiId(), apiProductRequesterDomain);
APIDTO apiDtoToReturn = APIMappingUtil.fromAPItoDTO(api, preserveCredentials, null);
File dependentAPI = exportApi(provider, api.getId(), apiDtoToReturn, api, userName, exportFormat, isStatusPreserved, preserveDocs, StringUtils.EMPTY, organization);
CommonUtil.extractArchive(dependentAPI, apisDirectoryPath);
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.
the class APIControllerUtil method injectEnvParamsToAPIProduct.
/**
* This method will be used to add extracted environment parameters to the imported API Product DTO object.
*
* @param importedApiProductDto API Product DTO object to be imported
* @param envParams Env params object with required parameters
* @return APIProductDTO Updated API Product DTO Object
*/
public static APIProductDTO injectEnvParamsToAPIProduct(APIProductDTO importedApiProductDto, JsonObject envParams, String pathToArchive) throws APIManagementException {
if (envParams == null || envParams.isJsonNull()) {
return importedApiProductDto;
}
APIProduct importedApiProduct = APIMappingUtil.fromDTOtoAPIProduct(importedApiProductDto, importedApiProductDto.getProvider());
// handle mutualSSL certificates
handleMutualSslCertificates(envParams, null, importedApiProductDto, importedApiProduct.getId(), pathToArchive);
// handle available subscription policies
JsonElement policies = envParams.get(ImportExportConstants.POLICIES_FIELD);
if (policies != null && !policies.isJsonNull()) {
handleSubscriptionPolicies(policies, null, importedApiProductDto);
}
return importedApiProductDto;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO in project carbon-apimgt by wso2.
the class ImportExportAPIServiceImpl method exportAPIProduct.
@Override
public File exportAPIProduct(String apiId, String revisionUUID, boolean preserveStatus, ExportFormat format, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIManagementException, APIImportExportException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String userName = RestApiCommonUtil.getLoggedInUsername();
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiId);
APIProduct product = apiProvider.getAPIProductbyUUID(revisionUUID, organization);
APIProductDTO apiProductDtoToReturn = APIMappingUtil.fromAPIProducttoDTO(product);
return ExportUtils.exportApiProduct(apiProvider, apiProductIdentifier, apiProductDtoToReturn, userName, format, preserveStatus, preserveDocs, preserveCredentials, organization);
}
Aggregations