// Make sure the user has editing rights
if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
throw new WebApplicationException(Status.UNAUTHORIZED);
WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
// Create the resource uri
URI uri = null;
InputStream is = null;
Resource<?> resource = null;
ResourceURI resourceURI = null;
logger.debug("Adding resource to {}", resourceURI);
ResourceSerializer<?, ?> serializer = serializerService.getSerializerByMimeType(mimeType);
if (serializer == null) {
logger.debug("No specialized resource serializer found, using regular file serializer");
serializer = serializerService.getSerializerByType(FileResource.TYPE);
}
// Create the resource
try {
is = new FileInputStream(uploadedFile);
resource = serializer.newResource(site, is, user, language);
resourceURI = resource.getURI();
} catch (FileNotFoundException e) {
logger.warn("Error creating resource at {} from image: {}", uri, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(is);
}
// If a path has been specified, set it
if (path != null && StringUtils.isNotBlank(path)) {
try {
if (!path.startsWith("/"))
path = "/" + path;
WebUrl url = new WebUrlImpl(site, path);
resourceURI.setPath(url.getPath());
// 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);
}
}
// Store the new resource
try {
uri = new URI(resourceURI.getIdentifier());
contentRepository.put(resource, true);
} 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());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (IllegalStateException e) {
logger.warn("Illegal state while adding new resource {}: {}", resourceURI, e.getMessage());
throw new WebApplicationException(Status.PRECONDITION_FAILED);
} catch (ContentRepositoryException e) {
logger.warn("Error adding new resource {}: {}", resourceURI, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
ResourceContent content = null;
ResourceContentReader<?> reader = null;
try {
reader = serializer.getContentReader();
is = new FileInputStream(uploadedFile);
content = reader.createFromContent(is, user, language, uploadedFile.length(), fileName, mimeType);
} catch (IOException e) {
logger.warn("Error reading resource content {} from request", uri);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (ParserConfigurationException e) {
logger.warn("Error configuring parser to read resource content {}: {}", uri, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (SAXException e) {
logger.warn("Error parsing udpated resource {}: {}", uri, e.getMessage());
throw new WebApplicationException(Status.BAD_REQUEST);
} catch (Throwable t) {
logger.warn("Unknown error while trying to read resource content {}: {}", uri, t.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(is);
if (content == null) {
try {
contentRepository.delete(resourceURI);
} catch (Throwable t) {
logger.error("Error deleting orphan resource {}", resourceURI, t);
}
}
}
try {
is = new FileInputStream(uploadedFile);
resource = contentRepository.putContent(resource.getURI(), content, is);
} catch (IOException e) {
logger.warn("Error writing content to resource {}: {}", uri, e.getMessage());
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} catch (IllegalStateException e) {
logger.warn("Illegal state while adding content to resource {}: {}", uri, e.getMessage());