Search in sources :

Example 11 with DocumentListDTO

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

the class ApisApiServiceImpl method getAPIDocuments.

/**
 * Returns all the documents of the given API identifier that matches to the search condition
 *
 * @param apiId       API identifier
 * @param limit       max number of records returned
 * @param offset      starting index
 * @param ifNoneMatch If-None-Match header value
 * @return matched documents as a list if DocumentDTOs
 */
@Override
public Response getAPIDocuments(String apiId, Integer limit, Integer offset, String ifNoneMatch, MessageContext messageContext) {
    // do some magic!
    // 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 {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // this will fail if user does not have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        // List<Documentation> allDocumentation = apiProvider.getAllDocumentation(apiIdentifier);
        List<Documentation> allDocumentation = apiProvider.getAllDocumentation(apiId, organization);
        DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(allDocumentation, offset, limit);
        DocumentationMappingUtil.setPaginationParams(documentListDTO, apiId, offset, limit, allDocumentation.size());
        return Response.ok().entity(documentListDTO).build();
    } catch (APIManagementException e) {
        // Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving documents of API : " + apiId, e, log);
        } else {
            String msg = "Error while retrieving documents of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO)

Example 12 with DocumentListDTO

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

the class DocumentationMappingUtil method setPaginationParams.

/**
 * Sets pagination urls for a DocumentListDTO object given pagination parameters and url parameters.
 *
 * @param documentListDTO a DocumentListDTO object
 * @param limit           max number of objects returned
 * @param offset          starting index
 * @param size            max offset
 */
public static void setPaginationParams(DocumentListDTO documentListDTO, 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.getDocumentationPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), apiId);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getDocumentationPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), apiId);
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    documentListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)

Example 13 with DocumentListDTO

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

the class DocumentationMappingUtil method setProductDocListPaginationParams.

/**
 * Sets pagination urls for a DocumentListDTO object (associated to an api product) given pagination parameters
 * and url parameters.
 *
 * @param documentListDTO a DocumentListDTO object
 * @param limit           max number of objects returned
 * @param offset          starting index
 * @param size            max offset
 */
public static void setProductDocListPaginationParams(DocumentListDTO documentListDTO, String productId, 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.getProductDocumentPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), productId);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getProductDocumentPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), productId);
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    documentListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)3 DocumentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO)3 DocumentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO)3 HashMap (java.util.HashMap)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)2 DocumentListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DocumentListDTO)2 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)1 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO)1 DocumentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DocumentDTO)1 DocumentDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO)1