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);
}
}
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;
}
Aggregations