use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class APIMappingUtil method getScopes.
/**
* This method returns the oauth scopes according to the given list of scopes.
*
* @param apiDTO list of APIScopes
* @return scope set
*/
public static Set<Scope> getScopes(APIDTO apiDTO) {
Set<Scope> scopeSet = new LinkedHashSet<>();
for (APIScopeDTO apiScopeDTO : apiDTO.getScopes()) {
Scope scope = new Scope();
ScopeDTO scopeDTO = apiScopeDTO.getScope();
scope.setKey(scopeDTO.getName());
scope.setName(scopeDTO.getDisplayName());
scope.setDescription(scopeDTO.getDescription());
scope.setRoles(String.join(",", scopeDTO.getBindings()));
scopeSet.add(scope);
}
return scopeSet;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class APIMappingUtil method getMediationPolicyAttributes.
/**
* Returns uuid of the specified mediation policy.
*
* @param sequenceName mediation sequence name
* @param direction in/out/fault
* @param dto APIDetailedDTO contains details of the exporting API
* @return UUID of sequence or null
*/
private static Map<String, String> getMediationPolicyAttributes(String sequenceName, String direction, APIDTO dto) {
APIIdentifier apiIdentifier = new APIIdentifier(dto.getProvider(), dto.getName(), dto.getVersion());
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
return APIUtil.getMediationPolicyAttributes(sequenceName, tenantId, direction, apiIdentifier);
} catch (UserStoreException e) {
log.error("Error occurred while reading tenant information ", e);
} catch (APIManagementException e) {
log.error("Error occurred while getting the uuid of the mediation sequence", e);
}
return null;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class APIMappingUtil method setEndpointURLsForAwsAPIs.
private static List<APIEndpointURLsDTO> setEndpointURLsForAwsAPIs(ApiTypeWrapper model, String organization) throws APIManagementException {
APIDTO apidto;
apidto = fromAPItoDTO(model.getApi(), organization);
JsonElement configElement = new JsonParser().parse(apidto.getApiDefinition());
// swaggerDefinition as a json object
JsonObject configObject = configElement.getAsJsonObject();
JsonArray servers = configObject.getAsJsonArray("servers");
JsonObject server = servers.get(0).getAsJsonObject();
String url = server.get("url").getAsString();
JsonObject variables = server.getAsJsonObject("variables");
JsonObject basePath = variables.getAsJsonObject("basePath");
String stageName = basePath.get("default").getAsString();
String serverUrl = url.replace("/{basePath}", stageName);
if (serverUrl == null) {
serverUrl = "Could not find server URL";
}
APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
List<APIEndpointURLsDTO> endpointUrls = new ArrayList<>();
APIURLsDTO apiurLsDTO = new APIURLsDTO();
apiurLsDTO.setHttps(serverUrl);
apiEndpointURLsDTO.setUrLs(apiurLsDTO);
endpointUrls.add(apiEndpointURLsDTO);
return endpointUrls;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPItoDTO.
public static APIDTO fromAPItoDTO(API model, String organization) throws APIManagementException {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
APIDTO dto = new APIDTO();
dto.setName(model.getId().getApiName());
dto.setVersion(model.getId().getVersion());
String providerName = model.getId().getProviderName();
dto.setProvider(APIUtil.replaceEmailDomainBack(providerName));
dto.setId(model.getUUID());
dto.setContext(model.getContext());
dto.setDescription(model.getDescription());
dto.setIsDefaultVersion(model.isPublishedDefaultVersion());
dto.setLifeCycleStatus(model.getStatus());
dto.setType(model.getType());
dto.setAvgRating(String.valueOf(model.getRating()));
Set<Scope> scopes = model.getScopes();
Map<String, ScopeInfoDTO> uniqueScope = new HashMap<>();
for (Scope scope : scopes) {
if (!uniqueScope.containsKey(scope.getKey())) {
ScopeInfoDTO scopeInfoDTO = new ScopeInfoDTO().key(scope.getKey()).name(scope.getName()).description(scope.getDescription());
if (StringUtils.isNotBlank(scope.getRoles())) {
scopeInfoDTO.roles(Arrays.asList(scope.getRoles().trim().split(",")));
}
uniqueScope.put(scope.getKey(), scopeInfoDTO);
}
}
dto.setScopes(new ArrayList<>(uniqueScope.values()));
if (null != model.getLastUpdated()) {
Date lastUpdateDate = model.getLastUpdated();
Timestamp timeStamp = new Timestamp(lastUpdateDate.getTime());
dto.setLastUpdatedTime(String.valueOf(timeStamp));
}
String createdTimeStamp = model.getCreatedTime();
if (null != createdTimeStamp) {
Date date = new Date(Long.valueOf(createdTimeStamp));
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String dateFormatted = formatter.format(date);
dto.setCreatedTime(dateFormatted);
}
String apiDefinition = null;
if (model.isAsync()) {
// for asyncAPI retrieve asyncapi.yml specification
apiDefinition = apiConsumer.getAsyncAPIDefinition(model.getUuid(), organization);
} else {
// retrieve open API definition
if (model.getSwaggerDefinition() != null) {
apiDefinition = model.getSwaggerDefinition();
} else {
apiDefinition = apiConsumer.getOpenAPIDefinition(model.getUuid(), organization);
}
}
dto.setApiDefinition(apiDefinition);
if (APIConstants.APITransportType.GRAPHQL.toString().equals(model.getType())) {
List<APIOperationsDTO> operationList = new ArrayList<>();
for (URITemplate template : model.getUriTemplates()) {
APIOperationsDTO operation = new APIOperationsDTO();
operation.setTarget(template.getUriTemplate());
operation.setVerb(template.getHTTPVerb());
operationList.add(operation);
}
dto.setOperations(operationList);
}
Set<String> apiTags = model.getTags();
List<String> tagsToReturn = new ArrayList<>();
tagsToReturn.addAll(apiTags);
dto.setTags(tagsToReturn);
// set the monetization status of this API (enabled or disabled)
APIMonetizationInfoDTO monetizationInfoDTO = new APIMonetizationInfoDTO();
monetizationInfoDTO.enabled(model.getMonetizationStatus());
dto.setMonetization(monetizationInfoDTO);
Set<org.wso2.carbon.apimgt.api.model.Tier> apiTiers = model.getAvailableTiers();
List<APITiersDTO> tiersToReturn = new ArrayList<>();
int tenantId = 0;
if (!StringUtils.isBlank(organization)) {
tenantId = APIUtil.getInternalOrganizationId(organization);
}
Set<String> deniedTiers = apiConsumer.getDeniedTiers(tenantId);
for (org.wso2.carbon.apimgt.api.model.Tier currentTier : apiTiers) {
if (!deniedTiers.contains(currentTier.getName())) {
APITiersDTO apiTiersDTO = new APITiersDTO();
apiTiersDTO.setTierName(currentTier.getName());
apiTiersDTO.setTierPlan(currentTier.getTierPlan());
// monetization attributes are applicable only for commercial tiers
if (APIConstants.COMMERCIAL_TIER_PLAN.equalsIgnoreCase(currentTier.getTierPlan())) {
APIMonetizationAttributesDTO monetizationAttributesDTO = new APIMonetizationAttributesDTO();
if (MapUtils.isNotEmpty(currentTier.getMonetizationAttributes())) {
Map<String, String> monetizationAttributes = currentTier.getMonetizationAttributes();
// check for the billing plan (fixed or price per request)
if (!StringUtils.isBlank(monetizationAttributes.get(APIConstants.Monetization.FIXED_PRICE))) {
monetizationAttributesDTO.setFixedPrice(monetizationAttributes.get(APIConstants.Monetization.FIXED_PRICE));
} else if (!StringUtils.isBlank(monetizationAttributes.get(APIConstants.Monetization.PRICE_PER_REQUEST))) {
monetizationAttributesDTO.setPricePerRequest(monetizationAttributes.get(APIConstants.Monetization.PRICE_PER_REQUEST));
}
monetizationAttributesDTO.setCurrencyType(monetizationAttributes.get(APIConstants.Monetization.CURRENCY) != null ? monetizationAttributes.get(APIConstants.Monetization.CURRENCY) : StringUtils.EMPTY);
monetizationAttributesDTO.setBillingCycle(monetizationAttributes.get(APIConstants.Monetization.BILLING_CYCLE) != null ? monetizationAttributes.get(APIConstants.Monetization.BILLING_CYCLE) : StringUtils.EMPTY);
}
apiTiersDTO.setMonetizationAttributes(monetizationAttributesDTO);
}
tiersToReturn.add(apiTiersDTO);
}
}
dto.setTiers(tiersToReturn);
dto.setTransport(Arrays.asList(model.getTransports().split(",")));
APIBusinessInformationDTO apiBusinessInformationDTO = new APIBusinessInformationDTO();
apiBusinessInformationDTO.setBusinessOwner(model.getBusinessOwner());
apiBusinessInformationDTO.setBusinessOwnerEmail(model.getBusinessOwnerEmail());
apiBusinessInformationDTO.setTechnicalOwner(model.getTechnicalOwner());
apiBusinessInformationDTO.setTechnicalOwnerEmail(model.getTechnicalOwnerEmail());
dto.setBusinessInformation(apiBusinessInformationDTO);
if (!StringUtils.isBlank(model.getThumbnailUrl())) {
dto.setHasThumbnail(true);
}
if (model.getAdditionalProperties() != null) {
JSONObject additionalProperties = model.getAdditionalProperties();
List<APIAdditionalPropertiesDTO> additionalPropertiesList = new ArrayList<>();
for (Object propertyKey : additionalProperties.keySet()) {
APIAdditionalPropertiesDTO additionalPropertiesDTO = new APIAdditionalPropertiesDTO();
String key = (String) propertyKey;
int index = key.lastIndexOf(APIConstants.API_RELATED_CUSTOM_PROPERTIES_SURFIX);
additionalPropertiesDTO.setValue((String) additionalProperties.get(key));
if (index > 0) {
additionalPropertiesDTO.setName(key.substring(0, index));
additionalPropertiesDTO.setDisplay(true);
additionalPropertiesList.add(additionalPropertiesDTO);
}
}
dto.setAdditionalProperties(additionalPropertiesList);
}
dto.setWsdlUri(model.getWsdlUrl());
if (model.getEnvironmentList() != null) {
List<String> environmentListToReturn = new ArrayList<>();
environmentListToReturn.addAll(model.getEnvironmentList());
dto.setEnvironmentList(environmentListToReturn);
}
dto.setAuthorizationHeader(model.getAuthorizationHeader());
if (model.getApiSecurity() != null) {
dto.setSecurityScheme(Arrays.asList(model.getApiSecurity().split(",")));
}
dto.setAdvertiseInfo(extractAdvertiseInfo(model));
String apiTenant = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(model.getId().getProviderName()));
String subscriptionAvailability = model.getSubscriptionAvailability();
String subscriptionAllowedTenants = model.getSubscriptionAvailableTenants();
dto.setIsSubscriptionAvailable(isSubscriptionAvailable(apiTenant, subscriptionAvailability, subscriptionAllowedTenants));
List<APICategory> apiCategories = model.getApiCategories();
List<String> categoryNamesList = new ArrayList<>();
if (apiCategories != null && !apiCategories.isEmpty()) {
for (APICategory category : apiCategories) {
categoryNamesList.add(category.getName());
}
}
dto.setCategories(categoryNamesList);
dto.setKeyManagers(model.getKeyManagers());
if (model.getGatewayVendor() != null) {
dto.setGatewayVendor(model.getGatewayVendor());
} else {
dto.setGatewayVendor("wso2");
}
if (model.getAsyncTransportProtocols() != null) {
dto.setAsyncTransportProtocols(Arrays.asList(model.getAsyncTransportProtocols().split(",")));
}
return dto;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.
/**
* Rest api implementation to downloading the client sdk for given api in given sdk language.
*
* @param apiId : The id of the api.
* @param language : Preferred sdk language.
* @param messageContext : messageContext
* @return : The sdk as a zip archive.
*/
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
if (StringUtils.isEmpty(apiId) || StringUtils.isEmpty(language)) {
String message = "Error generating the SDK. API id or language should not be empty";
RestApiUtil.handleBadRequest(message, log);
}
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIDTO api = getAPIByAPIId(apiId, organization);
APIClientGenerationManager apiClientGenerationManager = new APIClientGenerationManager();
Map<String, String> sdkArtifacts;
String swaggerDefinition = api.getApiDefinition();
if (api != null) {
try {
sdkArtifacts = apiClientGenerationManager.generateSDK(language, api.getName(), api.getVersion(), swaggerDefinition);
// Create the sdk response.
File sdkFile = new File(sdkArtifacts.get("zipFilePath"));
return Response.ok(sdkFile, MediaType.APPLICATION_OCTET_STREAM_TYPE).header("Content-Disposition", "attachment; filename=\"" + sdkArtifacts.get("zipFileName") + "\"").build();
} catch (APIClientGenerationException e) {
String message = "Error generating client sdk for api: " + api.getName() + " for language: " + language;
RestApiUtil.handleInternalServerError(message, e, log);
}
}
String message = "Could not find an API for ID " + apiId;
RestApiUtil.handleResourceNotFoundError(message, log);
return null;
}
Aggregations