use of org.wso2.carbon.apimgt.api.model.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;
}
use of org.wso2.carbon.apimgt.api.model.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;
}
use of org.wso2.carbon.apimgt.api.model.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();
}
use of org.wso2.carbon.apimgt.api.model.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"));
}
use of org.wso2.carbon.apimgt.api.model.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"));
}
Aggregations