logger.warn("Attempt to unlock a page in a read-only content repository {}", site);
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
ResourceURI liveURI = new PageURIImpl(site, null, pageId, Resource.LIVE);
// Does the page exist?
Page livePage = null;
try {
if (!contentRepository.existsInAnyVersion(liveURI))
throw new WebApplicationException(Status.NOT_FOUND);
livePage = (Page) contentRepository.get(liveURI);
if (livePage == null)
throw new WebApplicationException(Status.PRECONDITION_FAILED);
liveURI.setPath(livePage.getURI().getPath());
} catch (ContentRepositoryException e) {
logger.warn("Error lookup up page {} from repository: {}", liveURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (IllegalStateException e) {
logger.warn("Error unpublishing page {}: {}", liveURI, e.getMessage());
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
// Check the value of the If-Match header against the etag
if (ifMatchHeader != null) {
String etag = ResourceUtils.getETagValue(livePage);
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 publishing rights
if (!SecurityUtils.userHasRole(user, SystemRole.PUBLISHER))
throw new WebApplicationException(Status.UNAUTHORIZED);
boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);
// If the page is locked by a different user, refuse
if (livePage.isLocked() && (!livePage.getLockOwner().equals(user) && !isAdmin)) {
return Response.status(Status.FORBIDDEN).build();
}
// Finally, perform the unpublish operation, including saving the current
// live version of the page as the new work version.
try {
contentRepository.delete(liveURI);
ResourceURI workURI = new ResourceURIImpl(liveURI, Resource.WORK);
if (!contentRepository.exists(workURI)) {
logger.debug("Creating work version of {}", workURI);
PageReader reader = new PageReader();
Page workPage = reader.read(IOUtils.toInputStream(livePage.toXml(), "utf-8"), site);
workPage.setVersion(Resource.WORK);