Search in sources :

Example 96 with Limit

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

the class APIMappingUtil method setRatingPaginationParams.

/**
 * Sets pagination urls for a RatingListDTO object given pagination parameters and url parameters
 *
 * @param ratingListDTO a RatingListDTO object
 * @param limit         max number of objects returned
 * @param offset        starting index
 * @param size          max offset
 */
public static void setRatingPaginationParams(RatingListDTO ratingListDTO, String apiId, int offset, int limit, int size) {
    // acquiring pagination parameters and setting pagination urls
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
    String paginatedPrevious = "";
    String paginatedNext = "";
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = RestApiCommonUtil.getRatingPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), apiId);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getRatingPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), apiId);
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    ratingListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)

Example 97 with Limit

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

the class ApisApiServiceImpl method apisGet.

@Override
public Response apisGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "" : query;
    APIListDTO apiListDTO = new APIListDTO();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // revert content search back to normal search by name to avoid doc result complexity and to comply with REST api practices
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":")) {
            query = query.replace(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":", APIConstants.NAME_TYPE_PREFIX + ":");
        }
        Map allMatchedApisMap = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        // This is a SortedSet
        Set<Object> sortedSet = (Set<Object>) allMatchedApisMap.get("apis");
        ArrayList<Object> allMatchedApis = new ArrayList<>(sortedSet);
        apiListDTO = APIMappingUtil.fromAPIListToDTO(allMatchedApis, organization);
        // Add pagination section in the response
        Object totalLength = allMatchedApisMap.get("length");
        Integer totalAvailableAPis = 0;
        if (totalLength != null) {
            totalAvailableAPis = (Integer) totalLength;
        }
        APIMappingUtil.setPaginationParams(apiListDTO, query, offset, limit, totalAvailableAPis);
        return Response.ok().entity(apiListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.rootCauseMessageMatches(e, "start index seems to be greater than the limit count")) {
            // this is not an error of the user as he does not know the total number of apis available. Thus sends
            // an empty response
            apiListDTO.setCount(0);
            apiListDTO.setPagination(new PaginationDTO());
            return Response.ok().entity(apiListDTO).build();
        } else {
            String errorMessage = "Error while retrieving APIs";
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : Set(java.util.Set) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Map(java.util.Map) HashMap(java.util.HashMap)

Example 98 with Limit

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

the class ApisApiServiceImpl method apisApiIdRatingsGet.

@Override
public Response apisApiIdRatingsGet(String id, Integer limit, Integer offset, String xWSO2Tenant, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset values if they are not set
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        apiConsumer.checkAPIVisibility(id, organization);
        float avgRating = apiConsumer.getAverageAPIRating(id);
        int userRating = 0;
        if (!APIConstants.WSO2_ANONYMOUS_USER.equals(username)) {
            userRating = apiConsumer.getUserRating(id, username);
        }
        List<RatingDTO> ratingDTOList = new ArrayList<>();
        JSONArray array = apiConsumer.getAPIRatings(id);
        for (int i = 0; i < array.size(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            RatingDTO ratingDTO = APIMappingUtil.fromJsonToRatingDTO(obj);
            ratingDTO.setApiId(id);
            ratingDTOList.add(ratingDTO);
        }
        RatingListDTO ratingListDTO = APIMappingUtil.fromRatingListToDTO(ratingDTOList, offset, limit);
        ratingListDTO.setUserRating(userRating);
        ratingListDTO.setAvgRating(String.valueOf(avgRating));
        APIMappingUtil.setRatingPaginationParams(ratingListDTO, id, offset, limit, ratingDTOList.size());
        return Response.ok().entity(ratingListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, id, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while retrieving ratings for API " + id, e, log);
        }
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 99 with Limit

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

the class ApisApiServiceImpl method getRepliesOfComment.

@Override
public Response getRepliesOfComment(String commentId, String apiId, String xWSO2Tenant, Integer limit, Integer offset, String ifNoneMatch, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
        CommentList comments = apiConsumer.getComments(apiTypeWrapper, commentId, limit, offset);
        CommentListDTO commentDTO = CommentMappingUtil.fromCommentListToDTO(comments, includeCommenterInfo);
        String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS;
        URI uri = new URI(uriString);
        return Response.ok(uri).entity(commentDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Failed to get comments of API " + apiId, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comments content location for API " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 100 with Limit

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

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset values if they are not set
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // this will fail if user doesn't have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        // List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiIdentifier, username);
        List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiId, organization);
        DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentationList, offset, limit);
        // todo : set total count properly
        DocumentationMappingUtil.setPaginationParams(documentListDTO, apiId, offset, limit, documentationList.size());
        return Response.ok().entity(documentListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
        }
    }
    /*catch (UnsupportedEncodingException e) {
            String errorMessage = "Error while Decoding apiId" + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }*/
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

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