Site site = getSite(request);
WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
// Create the page uri
ResourceURIImpl pageURI = null;
String uuid = UUID.randomUUID().toString();
if (!StringUtils.isBlank(path)) {
try {
if (!path.startsWith("/"))
path = "/" + path;
WebUrl url = new WebUrlImpl(site, path);
path = url.getPath();
} catch (IllegalArgumentException e) {
logger.warn("Tried to create a page with an invalid path '{}': {}", path, e.getMessage());
throw new WebApplicationException(Status.BAD_REQUEST);
}
} else {
path = "/" + uuid.replaceAll("-", "");
}
pageURI = new PageURIImpl(site, path, uuid, Resource.WORK);
// Make sure the page doesn't exist
try {
if (contentRepository.existsInAnyVersion(new GeneralResourceURIImpl(site, pageURI.getPath()))) {
logger.warn("Tried to create already existing page {} in site '{}'", pageURI, site);
throw new WebApplicationException(Status.CONFLICT);
}
} catch (IllegalArgumentException e) {
logger.warn("Tried to create a page with an invalid path '{}': {}", path, e.getMessage());
throw new WebApplicationException(Status.BAD_REQUEST);
} catch (ContentRepositoryException e) {
logger.warn("Page lookup {} failed for site '{}'", pageURI, site);
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 editing rights
if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
throw new WebApplicationException(Status.UNAUTHORIZED);
// Parse the page and store it
PageImpl page = null;
URI uri = null;
if (!StringUtils.isBlank(pageXml)) {
logger.debug("Adding page to {}", pageURI);
try {
PageReader pageReader = new PageReader();
page = pageReader.read(IOUtils.toInputStream(pageXml, "utf-8"), site);
page.setIdentifier(pageURI.getIdentifier());
page.setPath(pageURI.getPath());
page.setVersion(pageURI.getVersion());
} catch (IOException e) {
logger.warn("Error reading page {} from request", pageURI);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (ParserConfigurationException e) {
logger.warn("Error configuring parser to read updated page {}: {}", pageURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (SAXException e) {
logger.warn("Error parsing updated page {}: {}", pageURI, e.getMessage());
throw new WebApplicationException(Status.BAD_REQUEST);
}
} else {
logger.debug("Creating new page at {}", pageURI);
page = new PageImpl(pageURI);
page.setTemplate(site.getDefaultTemplate().getIdentifier());
page.setCreated(user, new Date());
}
// Store the new page
try {
contentRepository.put(page, true);
uri = new URI(UrlUtils.concat(request.getRequestURL().toString(), pageURI.getIdentifier()));
} catch (URISyntaxException e) {
logger.warn("Error creating a uri for page {}: {}", pageURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (SecurityException e) {
logger.warn("Tried to update page {} of site '{}' without permission", pageURI, site);