if (site.getContentRepository().isReadOnly()) {
logger.warn("Attempt to publish a page in a read-only content repository {}", site);
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
ResourceURI workURI = new PageURIImpl(site, null, pageId, Resource.WORK);
// Does the work page exist?
Page workPage = null;
try {
if (!contentRepository.existsInAnyVersion(workURI))
throw new WebApplicationException(Status.NOT_FOUND);
workPage = (Page) contentRepository.get(workURI);
if (workPage == null)
throw new WebApplicationException(Status.PRECONDITION_FAILED);
workURI.setPath(workPage.getURI().getPath());
} catch (ContentRepositoryException e) {
logger.warn("Error looking 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(workPage);
if (!etag.equals(ifMatchHeader)) {
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
}
// Make sure the page does not contain references to resources that don't
// exist anymore.
logger.debug("Checking referenced resources on {}", workPage);
try {
for (Pagelet pagelet : workPage.getPagelets()) {
String resourceId = pagelet.getProperty("resourceid");
if (StringUtils.isEmpty(resourceId))
continue;
ResourceURI resourceURI = contentRepository.getResourceURI(resourceId);
if (resourceURI == null) {
logger.warn("Page {} references non existing resource '{}'", workPage, resourceId);
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
resourceURI.setVersion(Resource.LIVE);
if (!contentRepository.exists(resourceURI)) {
logger.warn("Page {} references unpublished resource '{}'", workPage, resourceURI);
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
}
} catch (ContentRepositoryException e) {
logger.warn("Error looking up referenced resources", e);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
// 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 (workPage.isLocked() && (!workPage.getLockOwner().equals(user) && !isAdmin)) {
return Response.status(Status.FORBIDDEN).build();
}
// Fix the dates
Date startDate = null;
Date endDate = null;
DateFormat df = new SimpleDateFormat();
// Parse the start date
if (StringUtils.isNotBlank(startDateText)) {
try {
startDate = df.parse(startDateText);
} catch (ParseException e) {
try {
startDate = WebloungeDateFormat.parseStatic(startDateText);
} catch (ParseException e2) {
throw new WebApplicationException(Status.BAD_REQUEST);
}
}
} else {
startDate = new Date();
}
// Parse the end date
if (StringUtils.isNotBlank(endDateText)) {
try {
endDate = df.parse(endDateText);
} catch (ParseException e) {
try {
endDate = WebloungeDateFormat.parseStatic(endDateText);
} catch (ParseException e2) {
throw new WebApplicationException(Status.BAD_REQUEST);
}
}
}
// Finally, perform the publish operation
try {
PageReader reader = new PageReader();
Page livePage = reader.read(IOUtils.toInputStream(workPage.toXml(), "utf-8"), site);
livePage.setVersion(Resource.LIVE);
if (setModified)
livePage.setModified(user, new Date());
if (!livePage.isPublished())
livePage.setPublished(user, startDate, endDate);
contentRepository.putAsynchronously(livePage);
contentRepository.deleteAsynchronously(workURI);
logger.info("Page {} has been published by {}", workURI, user);
} catch (SecurityException e) {
logger.warn("Tried to publish page {} of site '{}' without permission", workURI, site);
throw new WebApplicationException(Status.FORBIDDEN);
} catch (IOException e) {