Search in sources :

Example 41 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class SlingFileUploadHandler method setFile.

/**
     * Uses the file(s) in the request parameter for creation of new nodes.
     * if the parent node is a nt:folder a new nt:file is created. otherwise
     * just a nt:resource. if the <code>name</code> is '*', the filename of
     * the uploaded file is used.
     *
     * @param parent the parent node
     * @param prop the assembled property info
     * @throws PersistenceException if an error occurs
     */
private void setFile(final Resource parentResource, final RequestProperty prop, final RequestParameter value, final List<Modification> changes, String name, final String contentType) throws PersistenceException {
    // check type hint. if the type is ok and extends from nt:file,
    // create an nt:file with that type. if it's invalid, drop it and let
    // the parent node type decide.
    boolean createNtFile = parentResource.isResourceType(JcrConstants.NT_FOLDER) || this.jcrSupport.isNodeType(parentResource, JcrConstants.NT_FOLDER);
    String typeHint = prop.getTypeHint();
    if (typeHint != null) {
        Boolean isFileNodeType = this.jcrSupport.isFileNodeType(parentResource.getResourceResolver(), typeHint);
        if (isFileNodeType == null) {
            // assuming type not valid.
            createNtFile = false;
            typeHint = null;
        } else {
            createNtFile = isFileNodeType;
        }
    }
    // an nt:file, and an image name with an extension is probably "important"
    if (!createNtFile && name.indexOf('.') > 0) {
        createNtFile = true;
    }
    // set empty type
    if (typeHint == null) {
        typeHint = createNtFile ? JcrConstants.NT_FILE : JcrConstants.NT_RESOURCE;
    }
    // create nt:file resource if needed
    Resource resParent;
    if (createNtFile) {
        // create nt:file
        resParent = getOrCreateChildResource(parentResource, name, typeHint, changes);
        name = JcrConstants.JCR_CONTENT;
        typeHint = JcrConstants.NT_RESOURCE;
    } else {
        resParent = parentResource;
    }
    // create resource
    final Resource newResource = getOrCreateChildResource(resParent, name, typeHint, changes);
    final ModifiableValueMap mvm = newResource.adaptTo(ModifiableValueMap.class);
    // set properties
    mvm.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
    mvm.put(JcrConstants.JCR_MIMETYPE, contentType);
    changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_LASTMODIFIED));
    changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_MIMETYPE));
    try {
        // process chunk upload request separately
        if (prop.isChunkUpload()) {
            processChunk(resParent, newResource, prop, value, changes);
        } else {
            mvm.put(JcrConstants.JCR_DATA, value.getInputStream());
            changes.add(Modification.onModified(newResource.getPath() + "/" + JcrConstants.JCR_DATA));
        }
    } catch (IOException e) {
        throw new PersistenceException("Error while retrieving inputstream from parameter value.", e);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) IOException(java.io.IOException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 42 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class NoSqlResourceProvider method delete.

public void delete(ResourceResolver resolver, String path) throws PersistenceException {
    if (ROOT_PATH.equals(path) || !adapter.validPath(path)) {
        throw new PersistenceException("Unable to delete resource at {}" + path, null, path, null);
    }
    Pattern pathsToDeletePattern = PathUtil.getSameOrDescendantPathPattern(path);
    // remove all existing path and probably descendant paths from list of deleted paths
    Iterator<String> deletedResourcesIterator = deletedResources.iterator();
    while (deletedResourcesIterator.hasNext()) {
        String deletedPath = deletedResourcesIterator.next();
        if (pathsToDeletePattern.matcher(deletedPath).matches()) {
            deletedResourcesIterator.remove();
        }
    }
    // remove all changed descendant items from changeset
    Iterator<Map.Entry<String, NoSqlData>> changeResourcesIterator = changedResources.entrySet().iterator();
    while (changeResourcesIterator.hasNext()) {
        Map.Entry<String, NoSqlData> entry = changeResourcesIterator.next();
        if (pathsToDeletePattern.matcher(entry.getKey()).matches()) {
            changeResourcesIterator.remove();
        }
    }
    // add path to delete
    deletedResources.add(path);
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) Pattern(java.util.regex.Pattern) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 43 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class NoSqlResourceProvider method create.

// ### WRITE ACCESS ###
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
    if (ROOT_PATH.equals(path) || !adapter.validPath(path)) {
        throw new PersistenceException("Illegal path - unable to create resource at " + path, null, path, null);
    }
    // check if already exists
    boolean deleted = this.deletedResources.remove(path);
    boolean exists = changedResources.containsKey(path) || this.adapter.get(path) != null;
    if (!deleted && exists) {
        throw new PersistenceException("Resource already exists at " + path, null, path, null);
    }
    // create new resource in changeset
    Map<String, Object> writableMap = properties != null ? new HashMap<String, Object>(properties) : new HashMap<String, Object>();
    NoSqlData data = new NoSqlData(path, NoSqlValueMap.convertForWriteAll(writableMap));
    changedResources.put(path, data);
    return new NoSqlResource(data, resolver, this);
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 44 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class CouchbaseNoSqlResourceProviderIT method testRoot.

@Override
protected Resource testRoot() {
    if (this.testRoot == null) {
        try {
            Resource root = context.resourceResolver().getResource("/");
            Resource providerRoot = root.getChild("test");
            if (providerRoot == null) {
                providerRoot = context.resourceResolver().create(root, "test", ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
            }
            this.testRoot = context.resourceResolver().create(providerRoot, UUID.randomUUID().toString(), ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
        } catch (PersistenceException ex) {
            throw new RuntimeException(ex);
        }
    }
    return this.testRoot;
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 45 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class CouchbaseNoSqlResourceProviderTransactionalIT method testRoot.

@Override
protected Resource testRoot() {
    if (this.testRoot == null) {
        try {
            Resource root = context.resourceResolver().getResource("/");
            Resource providerRoot = root.getChild("test");
            if (providerRoot == null) {
                providerRoot = context.resourceResolver().create(root, "test", ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
            }
            this.testRoot = context.resourceResolver().create(providerRoot, UUID.randomUUID().toString(), ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
        } catch (PersistenceException ex) {
            throw new RuntimeException(ex);
        }
    }
    return this.testRoot;
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Aggregations

PersistenceException (org.apache.sling.api.resource.PersistenceException)146 Resource (org.apache.sling.api.resource.Resource)102 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)63 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)37 HashMap (java.util.HashMap)35 LoginException (org.apache.sling.api.resource.LoginException)32 RepositoryException (javax.jcr.RepositoryException)27 ValueMap (org.apache.sling.api.resource.ValueMap)23 Node (javax.jcr.Node)18 Map (java.util.Map)15 Calendar (java.util.Calendar)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)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 Session (javax.jcr.Session)4