Search in sources :

Example 1 with DocumentListDTO

use of org.wso2.carbon.apimgt.rest.api.store.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.store.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.store.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)

Example 4 with DocumentListDTO

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

the class TestMappingUtilTestCase method testDocumentInfoListToDocumentDTOMapping.

@Test(description = "Document Info list to Document DTO list mapping")
void testDocumentInfoListToDocumentDTOMapping() {
    DocumentInfo documentInfo1 = SampleTestObjectCreator.createDefaultDocumentationInfo().id("newId1").name("newName1").fileName("newFile1").summary("newSum1").build();
    DocumentInfo documentInfo2 = SampleTestObjectCreator.createDefaultDocumentationInfo().id("newId2").name("newName2").fileName("newFile2").summary("newSum2").build();
    List<DocumentInfo> documentInfos = new ArrayList<>();
    documentInfos.add(documentInfo1);
    documentInfos.add(documentInfo2);
    DocumentListDTO documentListDTO = MappingUtil.toDocumentListDTO(documentInfos);
    assertEquals((Integer) documentInfos.size(), documentListDTO.getCount());
    assertEquals(documentInfo1.getName(), documentListDTO.getList().get(0).getName());
    assertEquals(documentInfo1.getId(), documentListDTO.getList().get(0).getDocumentId());
    assertEquals(documentInfo1.getOtherType(), documentListDTO.getList().get(0).getOtherTypeName());
    assertEquals(documentInfo1.getSourceType().getType(), documentListDTO.getList().get(0).getSourceType().name());
    assertEquals(documentInfo1.getSourceURL(), documentListDTO.getList().get(0).getSourceUrl());
    assertEquals(documentInfo1.getFileName(), documentListDTO.getList().get(0).getFileName());
    assertEquals(documentInfo1.getSummary(), documentListDTO.getList().get(0).getSummary());
    assertEquals(documentInfo1.getVisibility().toString(), documentListDTO.getList().get(0).getVisibility().name());
    assertEquals(documentInfo1.getType().toString(), documentListDTO.getList().get(0).getType().name());
    assertEquals(documentInfo2.getName(), documentListDTO.getList().get(1).getName());
    assertEquals(documentInfo2.getId(), documentListDTO.getList().get(1).getDocumentId());
    assertEquals(documentInfo2.getOtherType(), documentListDTO.getList().get(1).getOtherTypeName());
    assertEquals(documentInfo2.getSourceType().getType(), documentListDTO.getList().get(1).getSourceType().name());
    assertEquals(documentInfo2.getSourceURL(), documentListDTO.getList().get(1).getSourceUrl());
    assertEquals(documentInfo2.getFileName(), documentListDTO.getList().get(1).getFileName());
    assertEquals(documentInfo2.getSummary(), documentListDTO.getList().get(1).getSummary());
    assertEquals(documentInfo2.getVisibility().toString(), documentListDTO.getList().get(1).getVisibility().name());
    assertEquals(documentInfo2.getType().toString(), documentListDTO.getList().get(1).getType().name());
}
Also used : ArrayList(java.util.ArrayList) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO) Test(org.testng.annotations.Test)

Example 5 with DocumentListDTO

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

the class MappingUtil method toDocumentListDTO.

/**
 * This method converts documentInfoResults to documentListDTO
 *
 * @param documentInfoResults list of document which return as results
 * @return DTO cotaning document list
 */
public static DocumentListDTO toDocumentListDTO(List<DocumentInfo> documentInfoResults) {
    DocumentListDTO documentListDTO = new DocumentListDTO();
    for (DocumentInfo documentInfo : documentInfoResults) {
        documentListDTO.addListItem(toDocumentDTO(documentInfo));
    }
    documentListDTO.setCount(documentInfoResults.size());
    return documentListDTO;
}
Also used : DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)4 DocumentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO)3 HashMap (java.util.HashMap)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 DocumentListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DocumentListDTO)2 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)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.store.dto.DocumentDTO)1