use of org.wso2.carbon.apimgt.persistence.dto.DocumentContent in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentGet.
/**
* Retrieves the content of a particular document
*
* @param apiId UUID of API
* @param documentId UUID of the document
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Content of the document
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdDocumentsDocumentIdContentGet(String apiId, String documentId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
DocumentContent documentationContent = apiPublisher.getDocumentationContent(documentId);
DocumentInfo documentInfo = documentationContent.getDocumentInfo();
if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
String filename = documentInfo.getFileName();
return Response.ok(documentationContent.getFileContent()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"").header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
} else if (DocumentInfo.SourceType.INLINE.equals(documentInfo.getSourceType())) {
String content = documentationContent.getInlineContent();
return Response.ok(content).header(RestApiConstants.HEADER_CONTENT_TYPE, MediaType.TEXT_PLAIN).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
} else if (DocumentInfo.SourceType.URL.equals(documentInfo.getSourceType())) {
String sourceUrl = documentInfo.getSourceURL();
return Response.seeOther(new URI(sourceUrl)).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving document " + documentId + " of the API " + 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();
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving source URI location of " + documentId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.DocumentContent in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentGet.
@Test
public void testApisApiIdDocumentsDocumentIdContentGet() throws APIManagementException, NotFoundException {
printTestMethodName();
String apiId = UUID.randomUUID().toString();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
String documentIdFile = UUID.randomUUID().toString();
String documentIdInline = UUID.randomUUID().toString();
DocumentInfo documentInfoFile = TestUtil.createAPIDoc(documentIdFile, "documentInfoFile", "", "API1 documentation file", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.PRIVATE);
DocumentInfo documentInfoInline = TestUtil.createAPIDoc(documentIdInline, "documentInfoInline", "", "API1 documentation inline", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
DocumentContent documentContentFIle = TestUtil.createDocContent(documentInfoFile, "Sample inline content for API1 DOC 1", null);
DocumentContent documentContentInline = TestUtil.createDocContent(documentInfoInline, "Sample inline content for API1 DOC 2", null);
Mockito.when(apiStore.getDocumentationContent(documentIdFile)).thenReturn(documentContentFIle);
Mockito.when(apiStore.getDocumentationContent(documentIdInline)).thenReturn(documentContentInline);
Response responseFile = apisApiService.apisApiIdDocumentsDocumentIdContentGet(apiId, documentIdFile, null, null, request);
Response responseInline = apisApiService.apisApiIdDocumentsDocumentIdContentGet(apiId, documentIdInline, null, null, request);
Assert.assertEquals(200, responseFile.getStatus());
Assert.assertEquals(200, responseInline.getStatus());
}
use of org.wso2.carbon.apimgt.persistence.dto.DocumentContent in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getDocumentationContent.
@Override
public DocumentContent getDocumentationContent(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
DocumentContent documentContent = null;
boolean tenantFlowStarted = false;
try {
String requestedTenantDomain = org.getName();
RegistryHolder holder = getRegistry(requestedTenantDomain);
Registry registryType = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
if (artifact == null) {
return null;
}
if (artifact != null) {
Documentation documentation = RegistryPersistenceDocUtil.getDocumentation(artifact);
if (documentation.getSourceType().equals(Documentation.DocumentSourceType.FILE)) {
String resource = documentation.getFilePath();
if (resource == null) {
return null;
}
String[] resourceSplitPath = resource.split(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH);
if (resourceSplitPath.length == 2) {
resource = resourceSplitPath[1];
} else {
throw new DocumentationPersistenceException("Invalid resource Path " + resource);
}
if (registryType.resourceExists(resource)) {
documentContent = new DocumentContent();
Resource apiDocResource = registryType.get(resource);
String[] content = apiDocResource.getPath().split("/");
String name = content[content.length - 1];
documentContent.setSourceType(ContentSourceType.FILE);
ResourceFile resourceFile = new ResourceFile(apiDocResource.getContentStream(), apiDocResource.getMediaType());
resourceFile.setName(name);
documentContent.setResourceFile(resourceFile);
}
} else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.INLINE) || documentation.getSourceType().equals(Documentation.DocumentSourceType.MARKDOWN)) {
String contentPath = artifact.getPath().replace(RegistryConstants.PATH_SEPARATOR + documentation.getName(), "") + RegistryConstants.PATH_SEPARATOR + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentation.getName();
if (registryType.resourceExists(contentPath)) {
documentContent = new DocumentContent();
Resource docContent = registryType.get(contentPath);
Object content = docContent.getContent();
if (content != null) {
String contentStr = new String((byte[]) docContent.getContent(), Charset.defaultCharset());
documentContent.setTextContent(contentStr);
documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
}
}
} else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.URL)) {
documentContent = new DocumentContent();
String sourceUrl = documentation.getSourceUrl();
documentContent.setTextContent(sourceUrl);
documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
}
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Failed to get documentation details";
throw new DocumentationPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
return documentContent;
}
use of org.wso2.carbon.apimgt.persistence.dto.DocumentContent in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method addDocumentationContent.
@Override
public DocumentContent addDocumentationContent(Organization org, String apiId, String docId, DocumentContent content) throws DocumentationPersistenceException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
GenericArtifactManager docArtifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docId);
Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
if (DocumentContent.ContentSourceType.FILE.equals(content.getSourceType())) {
ResourceFile resource = content.getResourceFile();
String filePath = RegistryPersistenceDocUtil.getDocumentFilePath(apiProviderName, apiName, apiVersion, resource.getName());
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(RegistryPersistenceUtil.replaceEmailDomain(apiProviderName), visibility, visibleRoles, filePath, registry);
// documentation.setFilePath(addResourceFile(apiId, filePath, icon));
String savedFilePath = addResourceFile(filePath, resource, registry, tenantDomain);
// doc.setFilePath(savedFilePath);
docArtifact.setAttribute(APIConstants.DOC_FILE_PATH, savedFilePath);
docArtifactManager.updateGenericArtifact(docArtifact);
RegistryPersistenceUtil.setFilePermission(filePath);
} else {
String contentPath = RegistryPersistenceDocUtil.getDocumentContentPath(apiProviderName, apiName, apiVersion, doc.getName());
Resource docContent;
if (!registry.resourceExists(contentPath)) {
docContent = registry.newResource();
} else {
docContent = registry.get(contentPath);
}
String text = content.getTextContent();
if (!APIConstants.NO_CONTENT_UPDATE.equals(text)) {
docContent.setContent(text);
}
docContent.setMediaType(APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE);
registry.put(contentPath, docContent);
// Set resource permission
String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
String docVisibility = doc.getVisibility().name();
String[] authorizedRoles = RegistryPersistenceUtil.getAuthorizedRoles(apiPath, tenantDomain);
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
if (docVisibility != null) {
if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_SHARED_VISIBILITY;
} else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, contentPath, registry);
GenericArtifact updateDocArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact, apiProviderName, apiName, apiVersion, doc);
Boolean toggle = Boolean.parseBoolean(updateDocArtifact.getAttribute("toggle"));
updateDocArtifact.setAttribute("toggle", Boolean.toString(!toggle));
docArtifactManager.updateGenericArtifact(updateDocArtifact);
}
} catch (APIPersistenceException | RegistryException | APIManagementException | PersistenceException | UserStoreException e) {
throw new DocumentationPersistenceException("Error while adding document content", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.DocumentContent in project carbon-apimgt by wso2.
the class APIProviderImpl method addDocumentationContent.
@Override
public void addDocumentationContent(String uuid, String docId, String organization, DocumentationContent content) throws APIManagementException {
DocumentContent mappedContent = null;
try {
mappedContent = DocumentMapper.INSTANCE.toDocumentContent(content);
DocumentContent doc = apiPersistenceInstance.addDocumentationContent(new Organization(organization), uuid, docId, mappedContent);
} catch (DocumentationPersistenceException e) {
throw new APIManagementException("Error while adding content to doc " + docId);
}
}
Aggregations