Search in sources :

Example 86 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class MediationMappingUtil method fromMediationListToDTO.

/**
 * Converts a List mediation objects into a DTO
 *
 * @param mediation a list of mediation objects
 * @param limit     max number of objects returned
 * @param offset    starting index
 * @return MediationListDTO object containing MediationInfoDTO
 */
public static MediationListDTO fromMediationListToDTO(List<Mediation> mediation, int offset, int limit) {
    MediationListDTO mediationListDTO = new MediationListDTO();
    List<MediationInfoDTO> mediationDTOs = mediationListDTO.getList();
    if (mediationDTOs == null) {
        mediationDTOs = new ArrayList<>();
        mediationListDTO.setList(mediationDTOs);
    }
    // identifying the proper start and end indexes
    int size = mediation.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++) {
        mediationDTOs.add(fromMediationInfoToDTO(mediation.get(i)));
    }
    mediationListDTO.setCount(mediationDTOs.size());
    return mediationListDTO;
}
Also used : MediationInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationInfoDTO) MediationListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationListDTO)

Example 87 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method getSubscriptions.

/**
 * Retrieves all subscriptions or retrieves subscriptions for a given API Id
 *
 * @param apiId API identifier
 * @param limit max number of objects returns
 * @param offset starting index
 * @param ifNoneMatch If-None-Match header value
 * @return Response object containing resulted subscriptions
 */
public Response getSubscriptions(String apiId, Integer limit, Integer offset, String ifNoneMatch, String query, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset if they are null
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        SubscriptionListDTO subscriptionListDTO;
        List<SubscribedAPI> apiUsages;
        if (apiId != null) {
            String apiUuid = apiId;
            APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                apiUuid = apiRevision.getApiUUID();
            }
            apiUsages = apiProvider.getAPIUsageByAPIId(apiUuid, organization);
        } else {
            UserApplicationAPIUsage[] allApiUsage = apiProvider.getAllAPIUsageByProvider(username);
            apiUsages = SubscriptionMappingUtil.fromUserApplicationAPIUsageArrayToSubscribedAPIList(allApiUsage);
        }
        if (query != null && !query.isEmpty()) {
            SubscriptionListDTO filteredSubscriptionList = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, query);
            subscriptionListDTO = SubscriptionMappingUtil.getPaginatedSubscriptions(filteredSubscriptionList, limit, offset);
            SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, filteredSubscriptionList.getCount());
        } else {
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, limit, offset);
            SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, apiUsages.size());
        }
        return Response.ok().entity(subscriptionListDTO).build();
    } catch (APIManagementException e) {
        // existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            String msg = "Error while retrieving subscriptions of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)

Example 88 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ThrottlingPoliciesApiServiceImpl method getAllThrottlingPolicies.

/**
 * Retrieves all the Tiers
 *
 * @param policyLevel tier level (api/application or resource)
 * @param limit       max number of objects returns
 * @param offset      starting index
 * @param ifNoneMatch If-None-Match header value
 * @return Response object containing resulted tiers
 */
@Override
public Response getAllThrottlingPolicies(String policyLevel, Integer limit, Integer offset, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset if they are null
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    List<Tier> tierList = getThrottlingPolicyList(policyLevel, false);
    ThrottlingPolicyListDTO policyListDTO = ThrottlingPolicyMappingUtil.fromTierListToDTO(tierList, policyLevel, limit, offset);
    // todo: set total counts properly
    ThrottlingPolicyMappingUtil.setPaginationParams(policyListDTO, policyLevel, limit, offset, tierList.size());
    return Response.ok().entity(policyListDTO).build();
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) ThrottlingPolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyListDTO)

Example 89 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class RestApiUtilTest method testRootCauseMessageMatchesNegative.

@Test
public void testRootCauseMessageMatchesNegative() throws Exception {
    String rootCauseMessage = "Entered start index seems to be greater than the limit count. Please verify your " + "parameters";
    ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException("Resource Not Found Exception");
    Throwable testThrowable = Mockito.mock(Throwable.class);
    PowerMockito.spy(RestApiUtil.class);
    PowerMockito.doReturn(testThrowable).when(RestApiUtil.class, "getPossibleErrorCause", resourceNotFoundException);
    when(testThrowable.getMessage()).thenReturn(rootCauseMessage);
    Assert.assertFalse(RestApiUtil.rootCauseMessageMatches(resourceNotFoundException, "Caused by exceeded limit count"));
}
Also used : ResourceNotFoundException(org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 90 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class RestApiUtilTest method testRootCauseMessageMatches.

@Test
public void testRootCauseMessageMatches() throws Exception {
    String rootCauseMessage = "Entered start index seems to be greater than the limit count. Please verify your " + "parameters";
    ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException("Resource Not Found Exception");
    Throwable testThrowable = Mockito.mock(Throwable.class);
    PowerMockito.spy(RestApiUtil.class);
    PowerMockito.doReturn(testThrowable).when(RestApiUtil.class, "getPossibleErrorCause", resourceNotFoundException);
    when(testThrowable.getMessage()).thenReturn(rootCauseMessage);
    Assert.assertTrue(RestApiUtil.rootCauseMessageMatches(resourceNotFoundException, "index seems to be greater than the limit count"));
}
Also used : ResourceNotFoundException(org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)49 ArrayList (java.util.ArrayList)46 HashMap (java.util.HashMap)41 Test (org.testng.annotations.Test)29 PreparedStatement (java.sql.PreparedStatement)22 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)21 API (org.wso2.carbon.apimgt.api.model.API)20 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 Map (java.util.Map)18 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 SQLException (java.sql.SQLException)16 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)16 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)15 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)14 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)13 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)13 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)13 GovernanceArtifact (org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact)12