Search in sources :

Example 1 with TierDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesTierLevelTierNameGet.

/**
 * Retrieves a tier by tier name and level
 *
 * @param tierName        Name of the tier
 * @param tierLevel       Level of the tier
 * @param ifNoneMatch     If-Non-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return The requested tier as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response policiesTierLevelTierNameGet(String tierName, String tierLevel, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    TierDTO tierDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = policiesTierLevelTierNameGetFingerprint(tierName, tierLevel, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        Policy tier = apiStore.getPolicy(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel), tierName);
        tierDTO = TierMappingUtil.fromTierToDTO(tier, tierLevel);
        return Response.ok().entity(tierDTO).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tier";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 2 with TierDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO in project carbon-apimgt by wso2.

the class TierMappingUtil method fromTierListToDTO.

/**
 * Converts a List object of Tiers into a DTO
 *
 * @param tiers  a list of Tier objects
 * @param limit  max number of objects returned
 * @param offset starting index
 * @return TierListDTO object containing TierDTOs
 */
public static TierListDTO fromTierListToDTO(List<Policy> tiers, String tierLevel, int limit, int offset) {
    TierListDTO tierListDTO = new TierListDTO();
    List<TierDTO> tierDTOs = tierListDTO.getList();
    if (tierDTOs == null) {
        tierDTOs = new ArrayList<>();
        tierListDTO.setList(tierDTOs);
    }
    // identifying the proper start and end indexes
    int size = tiers.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Policy tier = tiers.get(i);
        tierDTOs.add(fromTierToDTO(tier, tierLevel));
    }
    tierListDTO.setCount(tierDTOs.size());
    return tierListDTO;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO)

Example 3 with TierDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO in project carbon-apimgt by wso2.

the class TierMappingUtil method fromTierToDTO.

/**
 * Converts a Tier object into TierDTO
 *
 * @param tier      Tier object
 * @param tierLevel tier level (api/application or resource)
 * @return TierDTO corresponds to Tier object
 */
public static TierDTO fromTierToDTO(Policy tier, String tierLevel) {
    TierDTO dto = new TierDTO();
    dto.setName(tier.getPolicyName());
    dto.setDescription(tier.getDescription());
    dto.setTierLevel(TierDTO.TierLevelEnum.valueOf(StringUtils.upperCase(tierLevel)));
    dto.setUnitTime((long) tier.getDefaultQuotaPolicy().getLimit().getUnitTime());
    Limit limit = tier.getDefaultQuotaPolicy().getLimit();
    if (limit instanceof RequestCountLimit) {
        dto.setRequestCount((long) ((RequestCountLimit) limit).getRequestCount());
    } else if (limit instanceof BandwidthLimit) {
        dto.setRequestCount((long) ((BandwidthLimit) limit).getDataAmount());
    }
    // // TODO: 08/12/16 More fields to map
    return dto;
}
Also used : TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Aggregations

TierDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO)3 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)2 HashMap (java.util.HashMap)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)1 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)1 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 TierListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO)1