Search in sources :

Example 46 with PersistenceException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) SequenceInputStream(java.io.SequenceInputStream) FileOutputStream(java.io.FileOutputStream) PersistenceException(org.apache.sling.api.resource.PersistenceException) File(java.io.File)

Example 47 with PersistenceException

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;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException)

Example 48 with PersistenceException

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()));
        }
    }
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 49 with PersistenceException

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);
}
Also used : HashMap(java.util.HashMap) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource)

Example 50 with PersistenceException

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);
    }
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Aggregations

PersistenceException (org.apache.sling.api.resource.PersistenceException)143 Resource (org.apache.sling.api.resource.Resource)102 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)62 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)37 HashMap (java.util.HashMap)34 LoginException (org.apache.sling.api.resource.LoginException)32 RepositoryException (javax.jcr.RepositoryException)24 ValueMap (org.apache.sling.api.resource.ValueMap)23 Node (javax.jcr.Node)17 Calendar (java.util.Calendar)14 Map (java.util.Map)14 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 ConfigurationPersistenceException (org.apache.sling.caconfig.spi.ConfigurationPersistenceException)6 InstanceDescription (org.apache.sling.discovery.InstanceDescription)6 QueueInfo (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)6 Test (org.junit.Test)5 JobTopicTraverser (org.apache.sling.event.impl.jobs.JobTopicTraverser)4