Search in sources :

Example 26 with DocumentDTO

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

the class ApiProductsApiServiceImpl method addAPIProductDocumentContent.

@Override
public Response addAPIProductDocumentContent(String apiProductId, String documentId, String ifMatch, InputStream fileInputStream, Attachment fileDetail, String inlineContent, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        APIProduct product = apiProvider.getAPIProductbyUUID(apiProductId, organization);
        APIProductIdentifier productIdentifier = product.getId();
        if (fileInputStream != null && inlineContent != null) {
            RestApiUtil.handleBadRequest("Only one of 'file' and 'inlineContent' should be specified", log);
        }
        // retrieves the document and send 404 if not found
        Documentation documentation = apiProvider.getDocumentation(apiProductId, documentId, organization);
        if (documentation == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
            return null;
        }
        // add content depending on the availability of either input stream or inline content
        if (fileInputStream != null) {
            if (!documentation.getSourceType().equals(Documentation.DocumentSourceType.FILE)) {
                RestApiUtil.handleBadRequest("Source type of product document " + documentId + " is not FILE", log);
            }
            RestApiPublisherUtils.attachFileToProductDocument(apiProductId, documentation, fileInputStream, fileDetail, organization);
        } else if (inlineContent != null) {
            if (!documentation.getSourceType().equals(Documentation.DocumentSourceType.INLINE) && !documentation.getSourceType().equals(Documentation.DocumentSourceType.MARKDOWN)) {
                RestApiUtil.handleBadRequest("Source type of product document " + documentId + " is not INLINE " + "or MARKDOWN", log);
            }
            PublisherCommonUtils.addDocumentationContent(documentation, apiProvider, apiProductId, documentId, organization, inlineContent);
        } else {
            RestApiUtil.handleBadRequest("Either 'file' or 'inlineContent' should be specified", log);
        }
        // retrieving the updated doc and the URI
        Documentation updatedDoc = apiProvider.getDocumentation(apiProductId, documentId, organization);
        DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(updatedDoc);
        String uriString = RestApiConstants.RESOURCE_PATH_PRODUCT_DOCUMENT_CONTENT.replace(RestApiConstants.APIPRODUCTID_PARAM, apiProductId).replace(RestApiConstants.DOCUMENTID_PARAM, documentId);
        URI uri = new URI(uriString);
        return Response.created(uri).entity(documentDTO).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_PRODUCT, apiProductId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while adding content to the document: " + documentId + " of API Product " + apiProductId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Failed to add content to the document " + documentId, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving document content location : " + documentId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
    return null;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 27 with DocumentDTO

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

the class DocumentationMappingUtil method fromDocumentationToDTO.

/**
 * Converts a APIM core Document object into corresponding REST API Document DTO object
 *
 * @param documentation Documentation object
 * @return a new DocumentDTO object corresponding to given Documentation object
 */
public static DocumentDTO fromDocumentationToDTO(Documentation documentation) {
    DocumentDTO documentDTO = new DocumentDTO();
    documentDTO.setDocumentId(documentation.getId());
    documentDTO.setName(documentation.getName());
    documentDTO.setSummary(documentation.getSummary());
    documentDTO.setType(DocumentDTO.TypeEnum.valueOf(documentation.getType().toString()));
    documentDTO.setOtherTypeName(documentation.getOtherTypeName());
    if (documentation.getSourceType() != null) {
        documentDTO.setSourceType(DocumentDTO.SourceTypeEnum.valueOf(documentation.getSourceType().toString()));
    }
    documentDTO.setSourceUrl(documentation.getSourceUrl());
    return documentDTO;
}
Also used : DocumentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO)

Example 28 with DocumentDTO

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

Example 29 with DocumentDTO

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

the class ApisApiServiceImpl method addAPIDocument.

/**
 * Add a documentation to an API
 *
 * @param apiId api identifier
 * @param body  Documentation DTO as request body
 * @return created document DTO as response
 */
@Override
public Response addAPIDocument(String apiId, DocumentDTO body, String ifMatch, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // validate if api exists
        APIInfo apiInfo = validateAPIExistence(apiId);
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(apiInfo.getStatus().toString());
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        Documentation documentation = PublisherCommonUtils.addDocumentationToAPI(body, apiId, organization);
        DocumentDTO newDocumentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
        String uriString = RestApiConstants.RESOURCE_PATH_DOCUMENTS_DOCUMENT_ID.replace(RestApiConstants.APIID_PARAM, apiId).replace(RestApiConstants.DOCUMENTID_PARAM, documentation.getId());
        URI uri = new URI(uriString);
        return Response.created(uri).entity(newDocumentDTO).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 adding documents of API : " + apiId, e, log);
        } else {
            String errorMessage = "Error while adding the document for API : " + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving location for document " + body.getName() + " of API " + 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) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 30 with DocumentDTO

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

the class ApisApiServiceImpl method getAPIDocumentByDocumentId.

@Override
public Response getAPIDocumentByDocumentId(String apiId, String documentId, String ifNoneMatch, MessageContext messageContext) {
    Documentation documentation;
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        documentation = apiProvider.getDocumentation(apiId, documentId, organization);
        if (documentation == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
        }
        DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
        return Response.ok().entity(documentDTO).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 document : " + documentId + " of API " + apiId, e, log);
        } else {
            String errorMessage = "Error while retrieving document : " + documentId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)13 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)12 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)11 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)9 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)9 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)9 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)4 HashMap (java.util.HashMap)3 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 DocumentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DocumentDTO)3