use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class SlingFileUploadHandler method mergeChunks.
/**
* Merge all previous chunks with last chunk's stream into a temporary file
* and return it.
*/
private File mergeChunks(final Resource parentResource, final InputStream lastChunkStream) throws PersistenceException {
OutputStream out = null;
SequenceInputStream mergeStrm = null;
File file = null;
try {
file = File.createTempFile("tmp-", "-mergechunk");
out = new FileOutputStream(file);
String startPattern = SlingPostConstants.CHUNK_NODE_NAME + "_" + "0_";
Iterator<Resource> itr = new FilteringResourceIterator(parentResource.listChildren(), startPattern);
final Set<InputStream> inpStrmSet = new LinkedHashSet<>();
while (itr.hasNext()) {
final Resource rangeResource = itr.next();
if (itr.hasNext()) {
throw new PersistenceException("more than one resource found for pattern: " + startPattern + "*");
}
inpStrmSet.add(rangeResource.adaptTo(InputStream.class));
log.debug("added chunk {} to merge stream", rangeResource.getName());
String[] indexBounds = rangeResource.getName().substring((SlingPostConstants.CHUNK_NODE_NAME + "_").length()).split("_");
startPattern = SlingPostConstants.CHUNK_NODE_NAME + "_" + String.valueOf(Long.valueOf(indexBounds[1]) + 1) + "_";
itr = new FilteringResourceIterator(parentResource.listChildren(), startPattern);
}
inpStrmSet.add(lastChunkStream);
mergeStrm = new SequenceInputStream(Collections.enumeration(inpStrmSet));
IOUtils.copyLarge(mergeStrm, out);
} catch (final IOException e) {
throw new PersistenceException("Exception during chunk merge occured: " + e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(mergeStrm);
}
return file;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class StreamedChunk method initState.
/**
* Initialise the state of the jcr:content sub resource.
* @param fileResource the fileResource parent resource.
* @param changes changes that were made.
* @return the content resource.
* @throws PersistenceException
*/
private Resource initState(Resource fileResource, List<Modification> changes) throws PersistenceException {
Map<String, Object> resourceProps = new HashMap<>();
resourceProps.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_RESOURCE);
resourceProps.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
resourceProps.put(JcrConstants.JCR_MIMETYPE, getContentType(part));
if (chunked) {
resourceProps.put(SLING_CHUNKS_LENGTH, chunkLength);
resourceProps.put(SLING_FILE_LENGTH, fileLength);
resourceProps.put(JcrConstants.JCR_MIXINTYPES, SLING_CHUNK_MIXIN);
// add a zero size file to satisfy JCR constraints.
resourceProps.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0]));
} else {
try {
resourceProps.put(JcrConstants.JCR_DATA, part.getInputStream());
} catch (IOException e) {
throw new PersistenceException("Error while retrieving inputstream from request part.", e);
}
}
Resource result = fileResource.getResourceResolver().create(fileResource, JcrConstants.JCR_CONTENT, resourceProps);
for (String key : resourceProps.keySet()) {
changes.add(Modification.onModified(result.getPath() + '/' + key));
}
return result;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class ModifyOperation method processDeletes.
/**
* Removes all properties listed as {@link RequestProperty#isDelete()} from
* the resource.
*
* @param resolver The <code>ResourceResolver</code> used to access the
* resources to delete the properties.
* @param reqProperties The map of request properties to check for
* properties to be removed.
* @param response The <code>HtmlResponse</code> to be updated with
* information on deleted properties.
* @throws PersistenceException Is thrown if an error occurs checking or
* removing properties.
*/
private void processDeletes(final ResourceResolver resolver, final Map<String, RequestProperty> reqProperties, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
for (final RequestProperty property : reqProperties.values()) {
if (property.isDelete()) {
final Resource parent = resolver.getResource(property.getParentPath());
if (parent == null) {
continue;
}
this.jcrSsupport.checkoutIfNecessary(parent, changes, versioningConfiguration);
final ValueMap vm = parent.adaptTo(ModifiableValueMap.class);
if (vm == null) {
throw new PersistenceException("Resource '" + parent.getPath() + "' is not modifiable.");
}
if (vm.containsKey(property.getName())) {
if (JcrConstants.JCR_MIXINTYPES.equals(property.getName())) {
vm.put(JcrConstants.JCR_MIXINTYPES, new String[0]);
} else {
vm.remove(property.getName());
}
} else {
final Resource childRsrc = resolver.getResource(parent.getPath() + '/' + property.getName());
if (childRsrc != null) {
resolver.delete(childRsrc);
}
}
changes.add(Modification.onDeleted(property.getPath()));
}
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class TenantProviderImpl method createTenantResource.
@SuppressWarnings("serial")
private Resource createTenantResource(final ResourceResolver resolver, final String tenantId, final Map<String, Object> properties) throws PersistenceException {
// check for duplicate first
if (getTenantResource(resolver, tenantId) != null) {
throw new PersistenceException("Tenant '" + tenantId + "' already exists");
}
Resource tenantRoot = resolver.getResource(tenantRootPath);
if (tenantRoot == null) {
Resource current = resolver.getResource("/");
if (current == null) {
throw new PersistenceException("Cannot get root Resource");
}
String[] segments = this.tenantRootPath.split("/");
for (String segment : segments) {
Resource child = current.getChild(segment);
if (child == null) {
child = resolver.create(current, segment, new HashMap<String, Object>() {
{
put("jcr:primaryType", "sling:Folder");
}
});
}
current = child;
}
tenantRoot = current;
}
return resolver.create(tenantRoot, tenantId, properties);
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class ContentLoader method createResourceHierarchy.
private Resource createResourceHierarchy(String path) {
String parentPath = ResourceUtil.getParent(path);
if (parentPath == null) {
return null;
}
Resource parentResource = resourceResolver.getResource(parentPath);
if (parentResource == null) {
parentResource = createResourceHierarchy(parentPath);
}
Map<String, Object> props = new HashMap<String, Object>();
props.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
try {
return resourceResolver.create(parentResource, ResourceUtil.getName(path), props);
} catch (PersistenceException ex) {
throw new RuntimeException(ex);
}
}
Aggregations