Search in sources :

Example 21 with APIInfoDTO

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

the class APIKeyValidator method findMatchingVerb.

public List<VerbInfoDTO> findMatchingVerb(MessageContext synCtx) throws ResourceNotFoundException, APISecurityException {
    List<VerbInfoDTO> verbInfoList = new ArrayList<>();
    String resourceCacheKey;
    String httpMethod = (String) ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(Constants.Configuration.HTTP_METHOD);
    String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT);
    String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    String fullRequestPath = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);
    String electedResource = (String) synCtx.getProperty(APIConstants.API_ELECTED_RESOURCE);
    ArrayList<String> resourceArray = null;
    if (electedResource != null) {
        if (APIConstants.GRAPHQL_API.equalsIgnoreCase((String) synCtx.getProperty(APIConstants.API_TYPE))) {
            resourceArray = new ArrayList<>(Arrays.asList(electedResource.split(",")));
        } else {
            resourceArray = new ArrayList<>(Arrays.asList(electedResource));
        }
    }
    String requestPath = getRequestPath(synCtx, apiContext, apiVersion, fullRequestPath);
    if ("".equals(requestPath)) {
        requestPath = "/";
    }
    if (log.isDebugEnabled()) {
        log.debug("Setting REST_SUB_REQUEST_PATH in msg context: " + requestPath);
    }
    synCtx.setProperty(RESTConstants.REST_SUB_REQUEST_PATH, requestPath);
    // verb has been put into the cache.
    if (resourceArray != null) {
        for (String resourceString : resourceArray) {
            VerbInfoDTO verbInfo;
            if (isGatewayAPIResourceValidationEnabled) {
                String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion);
                if (!getResourceCache().containsKey(apiCacheKey)) {
                    break;
                }
                resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod);
                verbInfo = (VerbInfoDTO) getResourceCache().get(resourceCacheKey);
                // Cache hit
                if (verbInfo != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Found resource in Cache for key: " + resourceCacheKey);
                    }
                    verbInfoList.add(verbInfo);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Resource not found in cache for key: " + resourceCacheKey);
                    }
                }
            }
        }
        if (resourceArray.size() == verbInfoList.size()) {
            return verbInfoList;
        }
    } else {
        API selectedApi = Utils.getSelectedAPI(synCtx);
        Resource selectedResource = null;
        String resourceString;
        if (selectedApi != null) {
            Resource[] selectedAPIResources = selectedApi.getResources();
            Set<Resource> acceptableResources = new LinkedHashSet<Resource>();
            for (Resource resource : selectedAPIResources) {
                // If the requesting method is OPTIONS or if the Resource contains the requesting method
                if (RESTConstants.METHOD_OPTIONS.equals(httpMethod) || (resource.getMethods() != null && Arrays.asList(resource.getMethods()).contains(httpMethod))) {
                    acceptableResources.add(resource);
                }
            }
            if (acceptableResources.size() > 0) {
                for (RESTDispatcher dispatcher : RESTUtils.getDispatchers()) {
                    Resource resource = dispatcher.findResource(synCtx, acceptableResources);
                    if (resource != null && Arrays.asList(resource.getMethods()).contains(httpMethod)) {
                        selectedResource = resource;
                        break;
                    }
                }
            }
        }
        if (selectedResource == null) {
            // No matching resource found.
            String msg = "Could not find matching resource for " + requestPath;
            log.error(msg);
            throw new ResourceNotFoundException(msg);
        }
        resourceString = selectedResource.getDispatcherHelper().getString();
        resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod);
        if (log.isDebugEnabled()) {
            log.debug("Selected Resource: " + resourceString);
        }
        // Set the elected resource
        synCtx.setProperty(APIConstants.API_ELECTED_RESOURCE, resourceString);
        if (isGatewayAPIResourceValidationEnabled) {
            VerbInfoDTO verbInfo;
            verbInfo = (VerbInfoDTO) getResourceCache().get(resourceCacheKey);
            // Cache hit
            if (verbInfo != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Got Resource from cache for key: " + resourceCacheKey);
                }
                verbInfoList.add(verbInfo);
                return verbInfoList;
            } else if (log.isDebugEnabled()) {
                log.debug("Cache miss for Resource for key: " + resourceCacheKey);
            }
        }
    }
    String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion);
    APIInfoDTO apiInfoDTO = null;
    if (isGatewayAPIResourceValidationEnabled) {
        apiInfoDTO = (APIInfoDTO) getResourceCache().get(apiCacheKey);
    }
    // Cache miss
    if (apiInfoDTO == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find API object in cache for key: " + apiCacheKey);
        }
        String apiType = (String) synCtx.getProperty(APIMgtGatewayConstants.API_TYPE);
        if (APIConstants.ApiTypes.PRODUCT_API.name().equalsIgnoreCase(apiType)) {
            apiInfoDTO = doGetAPIProductInfo(synCtx, apiContext, apiVersion);
        } else {
            apiInfoDTO = doGetAPIInfo(synCtx, apiContext, apiVersion);
        }
        if (isGatewayAPIResourceValidationEnabled) {
            getResourceCache().put(apiCacheKey, apiInfoDTO);
        }
    }
    if (apiInfoDTO.getResources() != null) {
        for (ResourceInfoDTO resourceInfoDTO : apiInfoDTO.getResources()) {
            Set<VerbInfoDTO> verbDTOList = resourceInfoDTO.getHttpVerbs();
            for (VerbInfoDTO verb : verbDTOList) {
                if (verb.getHttpVerb().equals(httpMethod)) {
                    for (String resourceString : resourceArray) {
                        if (isResourcePathMatching(resourceString, resourceInfoDTO)) {
                            resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion, resourceString, httpMethod);
                            verb.setRequestKey(resourceCacheKey);
                            verbInfoList.add(verb);
                            if (isGatewayAPIResourceValidationEnabled) {
                                // Set cache key in the message c\ontext so that it can be used by the subsequent handlers.
                                if (log.isDebugEnabled()) {
                                    log.debug("Putting resource object in cache with key: " + resourceCacheKey);
                                }
                                getResourceCache().put(resourceCacheKey, verb);
                                synCtx.setProperty(APIConstants.API_RESOURCE_CACHE_KEY, resourceCacheKey);
                            }
                        }
                    }
                }
            }
        }
    }
    if (verbInfoList.size() == 0) {
        verbInfoList = null;
    }
    return verbInfoList;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Resource(org.apache.synapse.api.Resource) RESTDispatcher(org.apache.synapse.api.dispatch.RESTDispatcher) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) API(org.apache.synapse.api.API) APIInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIInfoDTO) ResourceInfoDTO(org.wso2.carbon.apimgt.impl.dto.ResourceInfoDTO) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 22 with APIInfoDTO

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

the class ApisApiServiceImpl method apisApiIdGet.

public Response apisApiIdGet(String apiId, String tenantDomain, MessageContext messageContext) {
    tenantDomain = GatewayUtils.validateTenantDomain(tenantDomain, messageContext);
    SubscriptionDataStore subscriptionDataStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (subscriptionDataStore == null) {
        log.warn("Subscription data store is not initialized for " + tenantDomain);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    API api;
    if (StringUtils.isNotEmpty(apiId)) {
        api = subscriptionDataStore.getAPIByUUID(apiId);
    } else {
        return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorDTO().moreInfo("required parameters " + "are missing")).build();
    }
    if (api == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    List<Subscription> subscriptionsByAPIId = subscriptionDataStore.getSubscriptionsByAPIId(api.getApiId());
    APIInfoDTO apiInfoDTO = GatewayUtils.generateAPIInfo(api, subscriptionsByAPIId, subscriptionDataStore);
    return Response.ok().entity(apiInfoDTO).build();
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.ErrorDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.APIInfoDTO)

Example 23 with APIInfoDTO

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

the class GatewayUtils method generateAPIInfo.

public static APIInfoDTO generateAPIInfo(API api, List<Subscription> subscriptionsByAPIId, SubscriptionDataStore subscriptionDataStore) {
    APIInfoDTO apiInfoDTO = new APIInfoDTO();
    apiInfoDTO.setApiId(api.getApiId());
    apiInfoDTO.setApiType(api.getApiType());
    apiInfoDTO.setName(api.getApiName());
    apiInfoDTO.setApiUUID(api.getUuid());
    apiInfoDTO.setContext(api.getContext());
    apiInfoDTO.setIsDefaultVersion(api.isDefaultVersion());
    apiInfoDTO.setPolicy(api.getApiTier());
    apiInfoDTO.setProvider(api.getApiProvider());
    apiInfoDTO.setUrlMappings(convertUriTemplate(api.getResources()));
    apiInfoDTO.setSubscripitons(convertSubscriptionsToSubscriptionInfo(subscriptionsByAPIId, subscriptionDataStore));
    return apiInfoDTO;
}
Also used : APIInfoDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.APIInfoDTO)

Example 24 with APIInfoDTO

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

the class APIMappingUtil method setThrottlePoliciesAndMonetization.

public static void setThrottlePoliciesAndMonetization(API api, APIInfoDTO apiInfoDTO, Set<String> deniedTiers, Map<String, Tier> tierMap) {
    Set<Tier> throttlingPolicies = new HashSet<Tier>();
    List<String> throttlingPolicyNames = new ArrayList<>();
    Set<Tier> apiTiers = api.getAvailableTiers();
    for (Tier currentTier : apiTiers) {
        if (!deniedTiers.contains(currentTier.getName())) {
            throttlingPolicies.add(currentTier);
            throttlingPolicyNames.add(currentTier.getName());
        }
    }
    int free = 0, commercial = 0;
    for (Tier tier : throttlingPolicies) {
        tier = tierMap.get(tier.getName());
        if (RestApiConstants.FREE.equalsIgnoreCase(tier.getTierPlan())) {
            free = free + 1;
        } else if (RestApiConstants.COMMERCIAL.equalsIgnoreCase(tier.getTierPlan())) {
            commercial = commercial + 1;
        }
    }
    if (free > 0 && commercial == 0) {
        apiInfoDTO.setMonetizationLabel(RestApiConstants.FREE);
    } else if (free == 0 && commercial > 0) {
        apiInfoDTO.setMonetizationLabel(RestApiConstants.PAID);
    } else if (free > 0 && commercial > 0) {
        apiInfoDTO.setMonetizationLabel(RestApiConstants.FREEMIUM);
    }
    apiInfoDTO.setThrottlingPolicies(throttlingPolicyNames);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 25 with APIInfoDTO

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

the class APIMappingUtil method fromAPIToInfoDTO.

/**
 * Creates a minimal DTO representation of an API object
 *
 * @param api API object
 * @return a minimal representation DTO
 */
static APIInfoDTO fromAPIToInfoDTO(API api) throws APIManagementException {
    APIInfoDTO apiInfoDTO = new APIInfoDTO();
    apiInfoDTO.setDescription(api.getDescription());
    String context = api.getContextTemplate();
    if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) {
        context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, "");
    }
    apiInfoDTO.setContext(context);
    apiInfoDTO.setId(api.getUUID());
    APIIdentifier apiId = api.getId();
    apiInfoDTO.setName(apiId.getApiName());
    apiInfoDTO.setVersion(apiId.getVersion());
    apiInfoDTO.setProvider(apiId.getProviderName());
    apiInfoDTO.setLifeCycleStatus(api.getStatus());
    apiInfoDTO.setType(api.getType());
    apiInfoDTO.setAvgRating(String.valueOf(api.getRating()));
    String providerName = api.getId().getProviderName();
    apiInfoDTO.setProvider(APIUtil.replaceEmailDomainBack(providerName));
    APIBusinessInformationDTO apiBusinessInformationDTO = new APIBusinessInformationDTO();
    apiBusinessInformationDTO.setBusinessOwner(api.getBusinessOwner());
    apiBusinessInformationDTO.setBusinessOwnerEmail(api.getBusinessOwnerEmail());
    apiBusinessInformationDTO.setTechnicalOwner(api.getTechnicalOwner());
    apiBusinessInformationDTO.setTechnicalOwnerEmail(api.getTechnicalOwnerEmail());
    apiInfoDTO.setBusinessInformation(apiBusinessInformationDTO);
    apiInfoDTO.setCreatedTime(api.getCreatedTime());
    // }
    if (!StringUtils.isBlank(api.getThumbnailUrl())) {
        apiInfoDTO.setThumbnailUri(api.getThumbnailUrl());
    }
    apiInfoDTO.setAdvertiseInfo(extractAdvertiseInfo(api));
    String apiTenant = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(api.getId().getProviderName()));
    String subscriptionAvailability = api.getSubscriptionAvailability();
    String subscriptionAllowedTenants = api.getSubscriptionAvailableTenants();
    apiInfoDTO.setIsSubscriptionAvailable(isSubscriptionAvailable(apiTenant, subscriptionAvailability, subscriptionAllowedTenants));
    apiInfoDTO.setGatewayVendor(api.getGatewayVendor());
    return apiInfoDTO;
}
Also used : APIBusinessInformationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIBusinessInformationDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO)

Aggregations

ArrayList (java.util.ArrayList)11 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)7 Tier (org.wso2.carbon.apimgt.api.model.Tier)7 APIInfoDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO)7 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)5 API (org.wso2.carbon.apimgt.api.model.API)4 APIInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIInfoDTO)4 JSONObject (org.json.simple.JSONObject)3 API (org.wso2.carbon.apimgt.core.models.API)3 ResourceInfoDTO (org.wso2.carbon.apimgt.impl.dto.ResourceInfoDTO)3 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)3 APIBusinessInformationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIBusinessInformationDTO)3 JsonObject (com.google.gson.JsonObject)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 Application (org.wso2.carbon.apimgt.api.model.Application)2