Search in sources :

Example 11 with DocumentationContent

use of org.wso2.carbon.apimgt.api.model.DocumentationContent in project carbon-apimgt by wso2.

the class AbstractAPIManager method getDocumentationContent.

@Override
public DocumentationContent getDocumentationContent(String apiId, String docId, String organization) throws APIManagementException {
    try {
        DocumentContent content = apiPersistenceInstance.getDocumentationContent(new Organization(organization), apiId, docId);
        DocumentationContent docContent = null;
        if (content != null) {
            docContent = DocumentMapper.INSTANCE.toDocumentationContent(content);
        } else {
            String msg = "Failed to get the document content. Artifact corresponding to document id " + docId + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
        return docContent;
    } catch (DocumentationPersistenceException e) {
        throw new APIManagementException("Error while retrieving document content ", e);
    }
}
Also used : DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.persistence.dto.DocumentContent) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 12 with DocumentationContent

use of org.wso2.carbon.apimgt.api.model.DocumentationContent in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentGet.

@Override
public Response apisApiIdDocumentsDocumentIdContentGet(String apiId, String documentId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        DocumentationContent docContent = apiConsumer.getDocumentationContent(apiId, documentId, organization);
        if (docContent == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
            return null;
        }
        // gets the content depending on the type of the document
        if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.FILE)) {
            String contentType = docContent.getResourceFile().getContentType();
            contentType = contentType == null ? RestApiConstants.APPLICATION_OCTET_STREAM : contentType;
            String name = docContent.getResourceFile().getName();
            return Response.ok(docContent.getResourceFile().getContent()).header(RestApiConstants.HEADER_CONTENT_TYPE, contentType).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + name + "\"").build();
        } else if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.INLINE) || docContent.getSourceType().equals(DocumentationContent.ContentSourceType.MARKDOWN)) {
            String content = docContent.getTextContent();
            return Response.ok(content).header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE).build();
        } else if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.URL)) {
            String sourceUrl = docContent.getTextContent();
            return Response.seeOther(new URI(sourceUrl)).build();
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            String errorMessage = "Error while retrieving document " + documentId + " of the API " + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving source URI location of " + documentId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

DocumentationContent (org.wso2.carbon.apimgt.api.model.DocumentationContent)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)4 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)4 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)2 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)2 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)2 DocumentContent (org.wso2.carbon.apimgt.persistence.dto.DocumentContent)2 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)2