use of io.gravitee.repository.management.model.Audit.AuditProperties.PAGE in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method update.
@Override
public PageEntity update(String pageId, UpdatePageEntity updatePageEntity, boolean partial) {
try {
logger.debug("Update Page {}", pageId);
Optional<Page> optPageToUpdate = pageRepository.findById(pageId);
if (!optPageToUpdate.isPresent()) {
throw new PageNotFoundException(pageId);
}
Page pageToUpdate = optPageToUpdate.get();
Page page = null;
String pageType = pageToUpdate.getType();
// create page revision if content has changed only for :
// - SWAGGER
// - Markdown
// - Translation
boolean createRevision = false;
if (PageType.LINK.name().equalsIgnoreCase(pageType)) {
String newResourceRef = updatePageEntity.getContent();
String actualResourceRef = pageToUpdate.getContent();
if (newResourceRef != null && !newResourceRef.equals(actualResourceRef)) {
String resourceType = (updatePageEntity.getConfiguration() != null ? updatePageEntity.getConfiguration().get(PageConfigurationKeys.LINK_RESOURCE_TYPE) : pageToUpdate.getConfiguration().get(PageConfigurationKeys.LINK_RESOURCE_TYPE));
if (PageConfigurationKeys.LINK_RESOURCE_TYPE_EXTERNAL.equals(resourceType) && (updatePageEntity.getContent() != null && updatePageEntity.getContent().isEmpty())) {
throw new PageActionException(PageType.LINK, "be created. An external Link must have a URL");
}
if ("root".equals(newResourceRef) || PageConfigurationKeys.LINK_RESOURCE_TYPE_EXTERNAL.equals(resourceType) || PageConfigurationKeys.LINK_RESOURCE_TYPE_CATEGORY.equals(resourceType)) {
updatePageEntity.setPublished(true);
} else {
Optional<Page> optionalRelatedPage = pageRepository.findById(newResourceRef);
if (optionalRelatedPage.isPresent()) {
Page relatedPage = optionalRelatedPage.get();
checkLinkRelatedPageType(relatedPage);
updatePageEntity.setPublished(relatedPage.isPublished());
}
}
} else if (newResourceRef != null && newResourceRef.equals(actualResourceRef)) {
// can not publish or unpublish a Link. LINK publication state is changed when the related page is updated.
updatePageEntity.setPublished(pageToUpdate.isPublished());
}
}
if (PageType.TRANSLATION.name().equalsIgnoreCase(pageType)) {
String parentId = (updatePageEntity.getParentId() != null && !updatePageEntity.getParentId().isEmpty()) ? updatePageEntity.getParentId() : pageToUpdate.getParentId();
Map<String, String> configuration = updatePageEntity.getConfiguration() != null ? updatePageEntity.getConfiguration() : pageToUpdate.getConfiguration();
checkTranslationConsistency(parentId, configuration, false);
boolean hasChanged = pageHasChanged(updatePageEntity, pageToUpdate);
Optional<Page> optParentPage = this.pageRepository.findById(parentId);
if (optParentPage.isPresent()) {
createRevision = isSwaggerOrMarkdown(optParentPage.get().getType()) && hasChanged;
}
}
if (updatePageEntity.getParentId() != null && !updatePageEntity.getParentId().equals(pageToUpdate.getParentId())) {
checkUpdatedPageSituation(updatePageEntity, pageType, pageId);
if (PageType.TRANSLATION.name().equalsIgnoreCase(pageType)) {
Optional<Page> optionalTranslatedPage = pageRepository.findById(updatePageEntity.getParentId());
if (optionalTranslatedPage.isPresent()) {
updatePageEntity.setPublished(optionalTranslatedPage.get().isPublished());
}
}
}
if (updatePageEntity.getExcludedGroups() != null && !updatePageEntity.getExcludedGroups().isEmpty()) {
updatePageEntity.setVisibility(Visibility.PRIVATE);
updatePageEntity.setExcludedAccessControls(true);
Set<AccessControlEntity> accessControlEntities = updatePageEntity.getAccessControls() != null ? updatePageEntity.getAccessControls() : new HashSet<>();
accessControlEntities.addAll(updatePageEntity.getExcludedGroups().stream().map((groupId -> {
AccessControlEntity accessControl = new AccessControlEntity();
accessControl.setReferenceType("GROUP");
accessControl.setReferenceId(groupId);
return accessControl;
})).collect(Collectors.toSet()));
updatePageEntity.setAccessControls(accessControlEntities);
updatePageEntity.setExcludedGroups(updatePageEntity.getExcludedGroups());
}
if (partial) {
page = merge(updatePageEntity, pageToUpdate);
} else {
page = convert(updatePageEntity);
}
if (page.getSource() != null) {
try {
if (pageToUpdate.getSource() != null && pageToUpdate.getSource().getConfiguration() != null) {
mergeSensitiveData(this.getFetcher(pageToUpdate.getSource()).getConfiguration(), page);
}
fetchPage(page);
} catch (FetcherException e) {
throw onUpdateFail(pageId, e);
}
} else {
// set null to remove the value not set to false
page.setUseAutoFetch(null);
}
if (isSwaggerOrMarkdown(pageType)) {
createRevision = pageHasChanged(pageToUpdate, page);
}
page.setId(pageId);
page.setUpdatedAt(new Date());
// Copy fields from existing values
page.setCreatedAt(pageToUpdate.getCreatedAt());
page.setType(pageType);
page.setReferenceId(pageToUpdate.getReferenceId());
page.setReferenceType(pageToUpdate.getReferenceType());
if (page.getVisibility() == null) {
page.setVisibility(pageToUpdate.getVisibility());
}
onlyOneHomepage(page);
// we can't unpublish it until the plan is closed
if (PageReferenceType.API.equals(pageToUpdate.getReferenceType())) {
if (updatePageEntity.isPublished() != null && !updatePageEntity.isPublished()) {
Optional<PlanEntity> activePlan = planService.findByApi(pageToUpdate.getReferenceId()).stream().filter(plan -> plan.getGeneralConditions() != null).filter(plan -> pageToUpdate.getId().equals(plan.getGeneralConditions())).filter(plan -> !(PlanStatus.CLOSED.equals(plan.getStatus()) || PlanStatus.STAGING.equals(plan.getStatus()))).findFirst();
if (activePlan.isPresent()) {
throw new PageUsedAsGeneralConditionsException(pageId, page.getName(), "unpublish", activePlan.get().getName());
}
}
}
// we can't unpublish it
if (PageReferenceType.ENVIRONMENT.equals(pageToUpdate.getReferenceType())) {
if (updatePageEntity.isPublished() != null && !updatePageEntity.isPublished()) {
List<CategoryEntity> categoriesUsingPage = categoryService.findByPage(pageId);
if (!categoriesUsingPage.isEmpty()) {
String categoriesName = categoriesUsingPage.stream().map(CategoryEntity::getName).collect(Collectors.joining(", ", "{ ", " }"));
throw new PageUsedByCategoryException(pageId, page.getName(), "unpublish", categoriesName);
}
}
}
// if order change, reorder all pages
if (page.getOrder() != pageToUpdate.getOrder()) {
reorderAndSavePages(page);
if (createRevision) {
createPageRevision(page);
}
return null;
} else {
List<String> messages = validateSafeContent(page);
Page updatedPage = pageRepository.update(page);
if ((pageToUpdate.isPublished() != page.isPublished() || !Objects.equals(pageToUpdate.getVisibility(), page.getVisibility())) && !PageType.LINK.name().equalsIgnoreCase(pageType) && !PageType.TRANSLATION.name().equalsIgnoreCase(pageType)) {
// if just publishing the page, updatePageEntity.getVisibility() will return null. In that case, we must keep the existing visibility status.
String newVisibility = updatePageEntity.getVisibility() == null ? pageToUpdate.getVisibility() : updatePageEntity.getVisibility().name();
// update all the related links and translations publication status.
this.changeRelatedPagesPublicationStatusAndVisibility(pageId, updatePageEntity.isPublished(), newVisibility);
}
createAuditLog(PageReferenceType.API.equals(page.getReferenceType()) ? page.getReferenceId() : null, PAGE_UPDATED, page.getUpdatedAt(), pageToUpdate, page);
if (createRevision) {
createPageRevision(updatedPage);
}
PageEntity pageEntity = convert(updatedPage);
pageEntity.setMessages(messages);
// update document in search engine
if (pageToUpdate.isPublished() && !page.isPublished()) {
searchEngineService.delete(convert(pageToUpdate), false);
} else {
index(pageEntity);
}
return pageEntity;
}
} catch (TechnicalException ex) {
throw onUpdateFail(pageId, ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.PAGE in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method createPage.
private PageEntity createPage(String apiId, NewPageEntity newPageEntity, String environmentId, String pageId) {
try {
logger.debug("Create page {} for API {}", newPageEntity, apiId);
String id = pageId != null && UUID.fromString(pageId) != null ? pageId : UuidString.generateRandom();
PageType newPageType = newPageEntity.getType();
// handle default visibility
if (newPageEntity.getVisibility() == null) {
newPageEntity.setVisibility(Visibility.PUBLIC);
}
// create page revision only for :
// - SWAGGER
// - Markdown
// - Translation
boolean createRevision = false;
// Useful when import api with pages in old version
if (newPageEntity.getExcludedGroups() != null && !newPageEntity.getExcludedGroups().isEmpty()) {
newPageEntity.setVisibility(Visibility.PRIVATE);
newPageEntity.setExcludedAccessControls(true);
newPageEntity.setAccessControls(newPageEntity.getExcludedGroups().stream().map((groupId -> {
AccessControlEntity accessControl = new AccessControlEntity();
accessControl.setReferenceType("GROUP");
accessControl.setReferenceId(groupId);
return accessControl;
})).collect(Collectors.toSet()));
}
if (PageType.TRANSLATION.equals(newPageType)) {
checkTranslationConsistency(newPageEntity.getParentId(), newPageEntity.getConfiguration(), true);
Optional<Page> optTranslatedPage = this.pageRepository.findById(newPageEntity.getParentId());
if (optTranslatedPage.isPresent()) {
newPageEntity.setPublished(optTranslatedPage.get().isPublished());
// create revision only for Swagger & Markdown page
createRevision = isSwaggerOrMarkdown(optTranslatedPage.get().getType());
}
}
if (PageType.FOLDER.equals(newPageType)) {
checkFolderConsistency(newPageEntity);
}
if (PageType.LINK.equals(newPageType)) {
String resourceType = newPageEntity.getConfiguration().get(PageConfigurationKeys.LINK_RESOURCE_TYPE);
String content = newPageEntity.getContent();
if (content == null || content.isEmpty()) {
throw new PageActionException(PageType.LINK, "be created. It must have a URL, a page Id or a category Id");
}
if ("root".equals(content) || PageConfigurationKeys.LINK_RESOURCE_TYPE_EXTERNAL.equals(resourceType) || PageConfigurationKeys.LINK_RESOURCE_TYPE_CATEGORY.equals(resourceType)) {
newPageEntity.setPublished(true);
} else {
Optional<Page> optionalRelatedPage = pageRepository.findById(content);
if (optionalRelatedPage.isPresent()) {
Page relatedPage = optionalRelatedPage.get();
checkLinkRelatedPageType(relatedPage);
newPageEntity.setPublished(relatedPage.isPublished());
newPageEntity.setVisibility(Visibility.valueOf(relatedPage.getVisibility()));
}
}
}
if (PageType.SWAGGER == newPageType || PageType.MARKDOWN == newPageType) {
checkMarkdownOrSwaggerConsistency(newPageEntity, newPageType);
createRevision = true;
}
Page page = convert(newPageEntity);
if (page.getSource() != null) {
fetchPage(page);
}
page.setId(id);
if (StringUtils.isEmpty(apiId)) {
page.setReferenceId(environmentId);
page.setReferenceType(PageReferenceType.ENVIRONMENT);
} else {
page.setReferenceId(apiId);
page.setReferenceType(PageReferenceType.API);
}
// Set date fields
page.setCreatedAt(new Date());
page.setUpdatedAt(page.getCreatedAt());
List<String> messages = validateSafeContent(page);
Page createdPage = this.pageRepository.create(page);
if (createRevision) {
createPageRevision(createdPage);
}
// only one homepage is allowed
onlyOneHomepage(page);
createAuditLog(PageReferenceType.API.equals(page.getReferenceType()) ? page.getReferenceId() : null, PAGE_CREATED, page.getCreatedAt(), null, page);
PageEntity pageEntity = convert(createdPage);
if (messages != null && messages.size() > 0) {
pageEntity.setMessages(messages);
}
// add document in search engine
index(pageEntity);
return pageEntity;
} catch (TechnicalException | FetcherException ex) {
logger.error("An error occurs while trying to create {}", newPageEntity, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newPageEntity, ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.PAGE in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method delete.
@Override
public void delete(String pageId) {
try {
logger.debug("Delete Page : {}", pageId);
Optional<Page> optPage = pageRepository.findById(pageId);
if (!optPage.isPresent()) {
throw new PageNotFoundException(pageId);
}
Page page = optPage.get();
// if the folder is not empty, throw exception
if (PageType.FOLDER.name().equalsIgnoreCase(page.getType())) {
List<Page> search = pageRepository.search(new PageCriteria.Builder().referenceId(page.getReferenceId()).referenceType(page.getReferenceType().name()).parent(page.getId()).build());
if (!search.isEmpty()) {
throw new TechnicalManagementException("Unable to remove the folder. It must be empty before being removed.");
}
}
// if the page is used as a category documentation, throw an exception
final List<CategoryEntity> categories = categoryService.findByPage(pageId);
if (categories != null && !categories.isEmpty()) {
String categoriesKeys = categories.stream().map(CategoryEntity::getKey).collect(Collectors.joining(","));
throw new PageActionException(PageType.valueOf(page.getType()), "be deleted since it is used in categories [" + categoriesKeys + "]");
}
// we can't remove it until the plan is closed
if (page.getReferenceType() != null && page.getReferenceType().equals(PageReferenceType.API)) {
Optional<PlanEntity> activePlan = planService.findByApi(page.getReferenceId()).stream().filter(plan -> plan.getGeneralConditions() != null).filter(// check the page and the parent for translations.
plan -> (PageType.TRANSLATION.name().equals(page.getType()) && plan.getGeneralConditions().equals(page.getParentId())) || plan.getGeneralConditions().equals(page.getId())).filter(plan -> !PlanStatus.CLOSED.equals(plan.getStatus())).findFirst();
if (activePlan.isPresent()) {
throw new PageUsedAsGeneralConditionsException(pageId, page.getName(), "remove", activePlan.get().getName());
}
}
pageRepository.delete(pageId);
// delete links and translations related to the page
if (!PageType.LINK.name().equalsIgnoreCase(page.getType()) && !PageType.TRANSLATION.name().equalsIgnoreCase(page.getType())) {
this.deleteRelatedPages(page.getId());
}
createAuditLog(PageReferenceType.API.equals(page.getReferenceType()) ? page.getReferenceId() : null, PAGE_DELETED, new Date(), page, null);
// remove from search engine
searchEngineService.delete(convert(page), false);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to delete Page {}", pageId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete Page " + pageId, ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.PAGE in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method search.
private List<PageEntity> search(final PageQuery query, String acceptedLocale, boolean withTranslations, boolean withLinks, String environmentId) {
try {
Stream<Page> pagesStream = pageRepository.search(queryToCriteria(query, environmentId)).stream();
if (!withTranslations) {
pagesStream = pagesStream.filter(page -> !PageType.TRANSLATION.name().equals(page.getType()));
}
if (!withLinks) {
pagesStream = pagesStream.filter(page -> !PageType.LINK.name().equals(page.getType()));
}
List<PageEntity> pages = pagesStream.map(this::convert).collect(Collectors.toList());
if (acceptedLocale == null || acceptedLocale.isEmpty()) {
pages.forEach(p -> {
if (!PageType.TRANSLATION.name().equals(p.getType())) {
List<PageEntity> translations = convert(getTranslations(p.getId()));
if (translations != null && !translations.isEmpty()) {
p.setTranslations(translations);
}
}
});
} else {
pages.forEach(p -> {
if (!PageType.TRANSLATION.name().equals(p.getType())) {
Page translation = getTranslation(p, acceptedLocale);
if (translation != null) {
String translationName = translation.getName();
if (translationName != null && !translationName.isEmpty()) {
p.setName(translationName);
}
String inheritContent = translation.getConfiguration().get(PageConfigurationKeys.TRANSLATION_INHERIT_CONTENT);
if (inheritContent != null && "false".equals(inheritContent)) {
p.setContent(translation.getContent());
}
}
}
});
}
if (query != null && query.getPublished() != null && query.getPublished() || !isAuthenticated()) {
// remove child of unpublished folders
return pages.stream().filter(page -> {
if (page.getParentId() != null) {
PageEntity parent = this.findById(page.getParentId());
if (!isAuthenticated()) {
return parent.isPublished() && Visibility.PUBLIC.equals(parent.getVisibility());
} else {
return parent.isPublished();
}
}
return true;
}).collect(toList());
}
return pages;
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to search pages", ex);
throw new TechnicalManagementException("An error occurs while trying to search pages", ex);
}
}
use of io.gravitee.repository.management.model.Audit.AuditProperties.PAGE in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method convert.
private PageEntity convert(Page page) {
PageEntity pageEntity;
if (page.getReferenceId() != null && PageReferenceType.API.equals(page.getReferenceType())) {
pageEntity = new ApiPageEntity();
((ApiPageEntity) pageEntity).setApi(page.getReferenceId());
} else {
pageEntity = new PageEntity();
}
pageEntity.setId(page.getId());
pageEntity.setName(page.getName());
pageEntity.setHomepage(page.isHomepage());
pageEntity.setType(page.getType());
pageEntity.setContent(page.getContent());
pageEntity.setReferenceType(page.getReferenceType().name());
pageEntity.setReferenceId(page.getReferenceId());
if (isJson(page.getContent())) {
pageEntity.setContentType(MediaType.APPLICATION_JSON);
} else {
// Yaml or RAML format ?
pageEntity.setContentType("text/yaml");
}
pageEntity.setLastContributor(page.getLastContributor());
pageEntity.setLastModificationDate(page.getUpdatedAt());
pageEntity.setOrder(page.getOrder());
pageEntity.setPublished(page.isPublished());
pageEntity.setVisibility(Visibility.valueOf(page.getVisibility()));
if (page.getSource() != null) {
pageEntity.setSource(convert(page.getSource()));
}
if (page.getConfiguration() != null) {
pageEntity.setConfiguration(page.getConfiguration());
}
if (page.getAttachedMedia() != null) {
pageEntity.setAttachedMedia(convertMedia(page.getAttachedMedia()));
}
pageEntity.setExcludedAccessControls(page.isExcludedAccessControls());
pageEntity.setAccessControls(PageServiceImpl.convertToEntities(page.getAccessControls()));
if (page.isExcludedAccessControls() && Visibility.PRIVATE.name().equals(page.getVisibility())) {
List<String> excludedGroups = page.getAccessControls().stream().filter(accessControl -> AccessControlReferenceType.GROUP.name().equals(accessControl.getReferenceType())).map(accessControl -> accessControl.getReferenceId()).collect(toList());
pageEntity.setExcludedGroups(excludedGroups);
}
pageEntity.setParentId("".equals(page.getParentId()) ? null : page.getParentId());
pageEntity.setMetadata(page.getMetadata());
pageEntity.setParentPath(this.computeParentPath(page, ""));
return pageEntity;
}
Aggregations