Search in sources :

Example 11 with ScopeDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method getScopesFromSwagger.

/**
 * Extract scopes from the swagger.
 *
 * @param swagger swagger document
 * @return list of scopes
 * @throws APIManagementException throw if parsing exception occur
 */
private static List<ScopeDTO> getScopesFromSwagger(String swagger) throws APIManagementException {
    APIDefinition apiDefinition = OASParserUtil.getOASParser(swagger);
    Set<Scope> scopes = apiDefinition.getScopes(swagger);
    List<ScopeDTO> scopeDTOS = new ArrayList<>();
    for (Scope aScope : scopes) {
        ScopeDTO scopeDTO = new ScopeDTO();
        scopeDTO.setName(aScope.getKey());
        scopeDTO.setDisplayName(aScope.getName());
        scopeDTO.setDescription(aScope.getDescription());
        String roles = aScope.getRoles();
        if (roles == null || roles.isEmpty()) {
            scopeDTO.setBindings(Collections.emptyList());
        } else {
            scopeDTO.setBindings(Arrays.asList((roles).split(",")));
        }
        scopeDTOS.add(scopeDTO);
    }
    return scopeDTOS;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) ArrayList(java.util.ArrayList)

Example 12 with ScopeDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method fromAPIProducttoDTO.

public static APIProductDTO fromAPIProducttoDTO(APIProduct product) throws APIManagementException {
    APIProductDTO productDto = new APIProductDTO();
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    productDto.setName(product.getId().getName());
    productDto.setProvider(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    productDto.setId(product.getUuid());
    productDto.setContext(product.getContext());
    productDto.setDescription(product.getDescription());
    productDto.setApiType(APIProductDTO.ApiTypeEnum.fromValue(APIConstants.AuditLogConstants.API_PRODUCT));
    productDto.setAuthorizationHeader(product.getAuthorizationHeader());
    productDto.setGatewayVendor(product.getGatewayVendor());
    Set<String> apiTags = product.getTags();
    List<String> tagsToReturn = new ArrayList<>(apiTags);
    productDto.setTags(tagsToReturn);
    productDto.setEnableSchemaValidation(product.isEnabledSchemaValidation());
    productDto.setIsRevision(product.isRevision());
    productDto.setRevisionedApiProductId(product.getRevisionedApiProductId());
    productDto.setRevisionId(product.getRevisionId());
    if (APIConstants.ENABLED.equals(product.getResponseCache())) {
        productDto.setResponseCachingEnabled(Boolean.TRUE);
    } else {
        productDto.setResponseCachingEnabled(Boolean.FALSE);
    }
    productDto.setCacheTimeout(product.getCacheTimeout());
    APIProductBusinessInformationDTO businessInformation = new APIProductBusinessInformationDTO();
    businessInformation.setBusinessOwner(product.getBusinessOwner());
    businessInformation.setBusinessOwnerEmail(product.getBusinessOwnerEmail());
    businessInformation.setTechnicalOwner(product.getTechnicalOwner());
    businessInformation.setTechnicalOwnerEmail(product.getTechnicalOwnerEmail());
    productDto.setBusinessInformation(businessInformation);
    APICorsConfigurationDTO apiCorsConfigurationDTO = new APICorsConfigurationDTO();
    CORSConfiguration corsConfiguration = product.getCorsConfiguration();
    if (corsConfiguration == null) {
        corsConfiguration = APIUtil.getDefaultCorsConfiguration();
    }
    apiCorsConfigurationDTO.setAccessControlAllowOrigins(corsConfiguration.getAccessControlAllowOrigins());
    apiCorsConfigurationDTO.setAccessControlAllowHeaders(corsConfiguration.getAccessControlAllowHeaders());
    apiCorsConfigurationDTO.setAccessControlAllowMethods(corsConfiguration.getAccessControlAllowMethods());
    apiCorsConfigurationDTO.setCorsConfigurationEnabled(corsConfiguration.isCorsConfigurationEnabled());
    apiCorsConfigurationDTO.setAccessControlAllowCredentials(corsConfiguration.isAccessControlAllowCredentials());
    productDto.setCorsConfiguration(apiCorsConfigurationDTO);
    productDto.setState(StateEnum.valueOf(product.getState()));
    productDto.setWorkflowStatus(product.getWorkflowStatus());
    // Aggregate API resources to each relevant API.
    Map<String, ProductAPIDTO> aggregatedAPIs = new HashMap<String, ProductAPIDTO>();
    List<APIProductResource> resources = product.getProductResources();
    for (APIProductResource apiProductResource : resources) {
        String uuid = apiProductResource.getApiId();
        if (aggregatedAPIs.containsKey(uuid)) {
            ProductAPIDTO productAPI = aggregatedAPIs.get(uuid);
            URITemplate template = apiProductResource.getUriTemplate();
            List<APIOperationsDTO> operations = productAPI.getOperations();
            APIOperationsDTO operation = getOperationFromURITemplate(template);
            operations.add(operation);
        } else {
            ProductAPIDTO productAPI = new ProductAPIDTO();
            productAPI.setApiId(uuid);
            productAPI.setName(apiProductResource.getApiName());
            productAPI.setVersion(apiProductResource.getApiIdentifier().getVersion());
            List<APIOperationsDTO> operations = new ArrayList<APIOperationsDTO>();
            URITemplate template = apiProductResource.getUriTemplate();
            APIOperationsDTO operation = getOperationFromURITemplate(template);
            operations.add(operation);
            productAPI.setOperations(operations);
            aggregatedAPIs.put(uuid, productAPI);
        }
    }
    productDto.setApis(new ArrayList<>(aggregatedAPIs.values()));
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    String apiSwaggerDefinition = apiProvider.getOpenAPIDefinition(product.getId(), tenantDomain);
    List<ScopeDTO> scopeDTOS = getScopesFromSwagger(apiSwaggerDefinition);
    productDto.setScopes(getAPIScopesFromScopeDTOs(scopeDTOS));
    String subscriptionAvailability = product.getSubscriptionAvailability();
    if (subscriptionAvailability != null) {
        productDto.setSubscriptionAvailability(mapSubscriptionAvailabilityFromAPIProducttoDTO(subscriptionAvailability));
    }
    if (product.getSubscriptionAvailableTenants() != null) {
        productDto.setSubscriptionAvailableTenants(Arrays.asList(product.getSubscriptionAvailableTenants().split(",")));
    }
    Set<org.wso2.carbon.apimgt.api.model.Tier> apiTiers = product.getAvailableTiers();
    List<String> tiersToReturn = new ArrayList<>();
    for (org.wso2.carbon.apimgt.api.model.Tier tier : apiTiers) {
        tiersToReturn.add(tier.getName());
    }
    productDto.setPolicies(tiersToReturn);
    productDto.setApiThrottlingPolicy(product.getProductLevelPolicy());
    if (product.getVisibility() != null) {
        productDto.setVisibility(mapVisibilityFromAPIProducttoDTO(product.getVisibility()));
    }
    if (product.getVisibleRoles() != null) {
        productDto.setVisibleRoles(Arrays.asList(product.getVisibleRoles().split(",")));
    }
    if (product.getVisibleTenants() != null) {
        productDto.setVisibleTenants(Arrays.asList(product.getVisibleTenants().split(",")));
    }
    productDto.setAccessControl(APIConstants.API_RESTRICTED_VISIBILITY.equals(product.getAccessControl()) ? APIProductDTO.AccessControlEnum.RESTRICTED : APIProductDTO.AccessControlEnum.NONE);
    if (product.getAccessControlRoles() != null) {
        productDto.setAccessControlRoles(Arrays.asList(product.getAccessControlRoles().split(",")));
    }
    if (StringUtils.isEmpty(product.getTransports())) {
        List<String> transports = new ArrayList<>();
        transports.add(APIConstants.HTTPS_PROTOCOL);
        productDto.setTransport(transports);
    } else {
        productDto.setTransport(Arrays.asList(product.getTransports().split(",")));
    }
    if (product.getAdditionalProperties() != null) {
        JSONObject additionalProperties = product.getAdditionalProperties();
        List<APIInfoAdditionalPropertiesDTO> additionalPropertiesList = new ArrayList<>();
        Map<String, APIInfoAdditionalPropertiesMapDTO> additionalPropertiesMap = new HashMap<>();
        for (Object propertyKey : additionalProperties.keySet()) {
            APIInfoAdditionalPropertiesDTO additionalPropertiesDTO = new APIInfoAdditionalPropertiesDTO();
            APIInfoAdditionalPropertiesMapDTO apiInfoAdditionalPropertiesMapDTO = new APIInfoAdditionalPropertiesMapDTO();
            String key = (String) propertyKey;
            int index = key.lastIndexOf(APIConstants.API_RELATED_CUSTOM_PROPERTIES_SURFIX);
            additionalPropertiesDTO.setValue((String) additionalProperties.get(key));
            apiInfoAdditionalPropertiesMapDTO.setValue((String) additionalProperties.get(key));
            if (index > 0) {
                additionalPropertiesDTO.setName(key.substring(0, index));
                apiInfoAdditionalPropertiesMapDTO.setName(key.substring(0, index));
                additionalPropertiesDTO.setDisplay(true);
            } else {
                additionalPropertiesDTO.setName(key);
                apiInfoAdditionalPropertiesMapDTO.setName(key);
                additionalPropertiesDTO.setDisplay(false);
            }
            apiInfoAdditionalPropertiesMapDTO.setDisplay(false);
            additionalPropertiesMap.put(key, apiInfoAdditionalPropertiesMapDTO);
            additionalPropertiesList.add(additionalPropertiesDTO);
        }
        productDto.setAdditionalPropertiesMap(additionalPropertiesMap);
        productDto.setAdditionalProperties(additionalPropertiesList);
    }
    if (product.getApiSecurity() != null) {
        productDto.setSecurityScheme(Arrays.asList(product.getApiSecurity().split(",")));
    }
    List<APICategory> apiCategories = product.getApiCategories();
    List<String> categoryNameList = new ArrayList<>();
    if (apiCategories != null && !apiCategories.isEmpty()) {
        for (APICategory category : apiCategories) {
            categoryNameList.add(category.getName());
        }
    }
    productDto.setCategories(categoryNameList);
    if (null != product.getLastUpdated()) {
        Date lastUpdateDate = product.getLastUpdated();
        Timestamp timeStamp = new Timestamp(lastUpdateDate.getTime());
        productDto.setLastUpdatedTime(String.valueOf(timeStamp));
    }
    if (null != product.getCreatedTime()) {
        Date createdTime = product.getCreatedTime();
        Timestamp timeStamp = new Timestamp(createdTime.getTime());
        productDto.setCreatedTime(String.valueOf(timeStamp));
    }
    return productDto;
}
Also used : APIInfoAdditionalPropertiesMapDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesMapDTO) HashMap(java.util.HashMap) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) ArrayList(java.util.ArrayList) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Timestamp(java.sql.Timestamp) APIProductBusinessInformationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductBusinessInformationDTO) ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) APIInfoAdditionalPropertiesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) Tier(org.wso2.carbon.apimgt.api.model.Tier) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APICorsConfigurationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO) Date(java.util.Date) CORSConfiguration(org.wso2.carbon.apimgt.api.model.CORSConfiguration) JSONObject(org.json.simple.JSONObject) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) JSONObject(org.json.simple.JSONObject) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 13 with ScopeDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO in project carbon-apimgt by wso2.

the class SharedScopeMappingUtil method fromScopeToDTO.

/**
 * Converts Scope object into ScopeDTO object.
 *
 * @param scope Scope object
 * @return ScopeDTO object
 */
public static ScopeDTO fromScopeToDTO(Scope scope) {
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setName(scope.getKey());
    scopeDTO.setDisplayName(scope.getName());
    scopeDTO.setUsageCount(scope.getUsageCount());
    scopeDTO.setDescription(scope.getDescription());
    scopeDTO.setId(scope.getId());
    String roles = scope.getRoles();
    if (StringUtils.isEmpty(roles)) {
        scopeDTO.setBindings(Collections.emptyList());
    } else {
        scopeDTO.setBindings(Arrays.asList((roles).split(",")));
    }
    return scopeDTO;
}
Also used : ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)

Example 14 with ScopeDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method extractAdvertiseInfo.

// /**
// * Creates a minimal scope DTO which will be a part of API Object
// *
// * @param scopes set
// * @return Scope DTO
// */
// public static List<ScopeInfoDTO> getScopeInfoDTO(Set<Scope> scopes) {
// 
// List<ScopeInfoDTO> scopeDto = new ArrayList<ScopeInfoDTO>();
// for (Scope scope : scopes) {
// ScopeInfoDTO scopeInfoDTO = new ScopeInfoDTO();
// scopeInfoDTO.setKey(scope.getKey());
// scopeInfoDTO.setName(scope.getName());
// if (scope.getRoles() != null) {
// scopeInfoDTO.setRoles(Arrays.asList(scope.getRoles().split(",")));
// }
// scopeDto.add(scopeInfoDTO);
// }
// return scopeDto;
// }
/**
 * Maps external store advertise API properties to AdvertiseInfoDTO object.
 *
 * @param api API object
 * @return AdvertiseInfoDTO
 */
public static AdvertiseInfoDTO extractAdvertiseInfo(API api) {
    AdvertiseInfoDTO advertiseInfoDTO = new AdvertiseInfoDTO();
    advertiseInfoDTO.setAdvertised(api.isAdvertiseOnly());
    advertiseInfoDTO.setOriginalDevPortalUrl(api.getRedirectURL());
    advertiseInfoDTO.setApiExternalProductionEndpoint(api.getApiExternalProductionEndpoint());
    advertiseInfoDTO.setApiExternalSandboxEndpoint(api.getApiExternalSandboxEndpoint());
    advertiseInfoDTO.setApiOwner(api.getApiOwner());
    if (api.getAdvertiseOnlyAPIVendor() != null) {
        advertiseInfoDTO.setVendor(AdvertiseInfoDTO.VendorEnum.valueOf(api.getAdvertiseOnlyAPIVendor()));
    }
    return advertiseInfoDTO;
}
Also used : AdvertiseInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdvertiseInfoDTO)

Example 15 with ScopeDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdScopesPost.

@Override
public Response apisApiIdScopesPost(String apiId, ScopeDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
        if (body.getBindings() != null && StringUtils.isNotEmpty(body.getBindings().getType())) {
            if (!keyManagerConfiguration.getScopeBindingType().equalsIgnoreCase(body.getBindings().getType())) {
                String message = "binding type not valid";
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
                return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
            }
        }
        Scope scope = MappingUtil.toScope(body);
        apiPublisher.addScopeToTheApi(apiId, scope);
        URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/scopes/" + scope.getName());
        return Response.created(location).entity(body).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while creating scope" + body.getName();
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.SCOPE_NAME, body.getName());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving source URI location of " + body.getName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

Scope (org.wso2.carbon.apimgt.api.model.Scope)11 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)11 ArrayList (java.util.ArrayList)10 APIScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)8 HashMap (java.util.HashMap)7 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 Scope (org.wso2.carbon.apimgt.core.models.Scope)4 JSONObject (org.json.simple.JSONObject)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)2 CORSConfiguration (org.wso2.carbon.apimgt.api.model.CORSConfiguration)2