WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
ResourceURI workURI = new PageURIImpl(site, null, pageId, Resource.WORK);
// Does the page exist?
Page currentPage = null;
try {
currentPage = (Page) contentRepository.get(workURI);
if (currentPage == null) {
logger.warn("Attempt to update a page without creating a work version first");
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
workURI.setPath(currentPage.getURI().getPath());
} catch (ContentRepositoryException e) {
logger.warn("Error lookup up page {} from repository: {}", workURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
// Check the value of the If-Match header against the etag
if (ifMatchHeader != null) {
String etag = ResourceUtils.getETagValue(currentPage);
if (!etag.equals(ifMatchHeader)) {
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
}
// Get the user
User user = securityService.getUser();
if (user == null)
throw new WebApplicationException(Status.UNAUTHORIZED);
// Make sure the user has editing rights
if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
throw new WebApplicationException(Status.UNAUTHORIZED);
boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);
// If the page is locked by a different user, refuse
if (currentPage.isLocked() && (!currentPage.getLockOwner().equals(user) && !isAdmin)) {
return Response.status(Status.FORBIDDEN).build();
}
// Parse the page and update it in the repository
Page page = null;
try {
PageReader pageReader = new PageReader();
page = pageReader.read(IOUtils.toInputStream(pageXml, "utf-8"), site);
if (StringUtils.isBlank(page.getURI().getPath()))
throw new WebApplicationException(Status.PRECONDITION_FAILED);
page.setModified(user, new Date());
page.setVersion(Resource.WORK);
// TODO: Preview generation disabled due to performance problems
contentRepository.putAsynchronously(page, false);
// Check if the page has been moved
String currentPath = currentPage.getURI().getPath();
String newPath = page.getURI().getPath();
if ((currentPath != null && !currentPath.equals(newPath) || (currentPath == null && newPath != null))) {
contentRepository.moveAsynchronously(currentPage.getURI(), newPath, true);
}
} catch (SecurityException e) {
logger.warn("Tried to update page {} of site '{}' without permission", workURI, site);