Search in sources :

Example 1 with DocumentListDTO

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

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

/**
 * Retrieves a list of documents of an API
 *
 * @param apiId       UUID of API
 * @param limit       maximum documents to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     minor version header
 * @return a list of document DTOs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    DocumentListDTO documentListDTO = null;
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<DocumentInfo> documentInfoResults = apiStore.getAllDocumentation(apiId, offset, limit);
        documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentInfoResults, offset, limit);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving documentation for given apiId " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(documentListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DocumentListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 2 with DocumentListDTO

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

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

/**
 * Retrieves a list of documents of an API
 *
 * @param apiId       UUID of API
 * @param limit       maximum documents to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return a list of document DTOs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        List<DocumentInfo> documentInfos = apiPublisher.getAllDocumentation(apiId, offset, limit);
        DocumentListDTO documentListDTO = MappingUtil.toDocumentListDTO(documentInfos);
        return Response.status(Response.Status.OK).entity(documentListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while getting list of documents" + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO)

Example 3 with DocumentListDTO

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

the class DocumentationMappingUtil method fromDocumentationListToDTO.

/**
 * Converts a List object of Documents into a DTO.
 *
 * @param documentations List of Documentations
 * @param limit          maximum number of APIs returns
 * @param offset         starting index
 * @return DocumentListDTO object containing Document DTOs
 */
public static DocumentListDTO fromDocumentationListToDTO(List<Documentation> documentations, int offset, int limit) {
    DocumentListDTO documentListDTO = new DocumentListDTO();
    List<DocumentDTO> documentDTOs = documentListDTO.getList();
    if (documentDTOs == null) {
        documentDTOs = new ArrayList<>();
        documentListDTO.setList(documentDTOs);
    }
    // add the required range of objects to be returned
    int start = offset < documentations.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= documentations.size() - 1 ? offset + limit - 1 : documentations.size() - 1;
    for (int i = start; i <= end; i++) {
        documentDTOs.add(fromDocumentationToDTO(documentations.get(i)));
    }
    documentListDTO.setCount(documentDTOs.size());
    return documentListDTO;
}
Also used : DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO)

Example 4 with DocumentListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentListDTO 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)

Example 5 with DocumentListDTO

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

the class DocumentationMappingUtil method fromDocumentationListToDTO.

/**
 * Converts a List object of ArtifactResourceMetaData into a DTO
 *
 * @param documentInfoList List of ArtifactResourceMetaData
 * @param limit          maximum number of APIs returns
 * @param offset         starting index
 * @return DocumentListDTO object containing Document DTOs
 */
public static DocumentListDTO fromDocumentationListToDTO(List<DocumentInfo> documentInfoList, int offset, int limit) {
    DocumentListDTO documentListDTO = new DocumentListDTO();
    List<DocumentDTO> documentDTOs = documentListDTO.getList();
    if (documentDTOs == null) {
        documentDTOs = new ArrayList<>();
        documentListDTO.setList(documentDTOs);
    }
    // add the required range of objects to be returned
    int start = offset < documentInfoList.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= documentInfoList.size() - 1 ? offset + limit - 1 : documentInfoList.size() - 1;
    for (int i = start; i <= end; i++) {
        documentDTOs.add(fromDocumentationToDTO(documentInfoList.get(i)));
    }
    documentListDTO.setCount(documentDTOs.size());
    return documentListDTO;
}
Also used : DocumentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DocumentDTO) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DocumentListDTO)

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