// Make sure the user has editing rights
if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
throw new WebApplicationException(Status.UNAUTHORIZED);
// Create the resource uri
ResourceURIImpl resourceURI = null;
String uuid = UUID.randomUUID().toString();
if (!StringUtils.isBlank(path)) {
try {
if (!path.startsWith("/"))
path = "/" + path;
WebUrl url = new WebUrlImpl(site, path);
resourceURI = new GeneralResourceURIImpl(site, url.getPath(), uuid);
// Make sure the resource doesn't exist
if (contentRepository.exists(new GeneralResourceURIImpl(site, url.getPath()))) {
logger.warn("Tried to create already existing resource {} in site '{}'", resourceURI, site);
throw new WebApplicationException(Status.CONFLICT);
}
} catch (IllegalArgumentException e) {
logger.warn("Tried to create a resource with an invalid path '{}': {}", path, e.getMessage());
throw new WebApplicationException(Status.BAD_REQUEST);
} catch (ContentRepositoryException e) {
logger.warn("Resource lookup {} failed for site '{}'", resourceURI, site);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
} else {
resourceURI = new GeneralResourceURIImpl(site, "/" + uuid.replaceAll("-", ""), uuid);
}
URI uri = null;
Resource<?> resource = null;
try {
// Parse the resource and store it
logger.debug("Creating new resource at {}", resourceURI);
resource = new FileResourceImpl(resourceURI);
resource.setCreated(user, new Date());
contentRepository.put(resource, true);
uri = new URI(UrlUtils.concat(request.getRequestURL().toString(), resourceURI.getIdentifier()));
} catch (URISyntaxException e) {
logger.warn("Error creating a uri for resource {}: {}", resourceURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
logger.warn("Error writing new resource {}: {}", resourceURI, e.getMessage());