Search in sources :

Example 51 with PersistenceException

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

the class AbstractCreateOperation method deepGetOrCreateResource.

/**
     * Deep gets or creates a resource, parent-padding with default resources. If
     * the path is empty, the given parent resource is returned.
     *
     * @param path path to resources that needs to be deep-created
     * @return Resource at path
     * @throws PersistenceException if an error occurs
     * @throws IllegalArgumentException if the path is relative and parent is
     *             <code>null</code>
     */
protected Resource deepGetOrCreateResource(final ResourceResolver resolver, final String path, final Map<String, RequestProperty> reqProperties, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
    if (log.isDebugEnabled()) {
        log.debug("Deep-creating resource '{}'", path);
    }
    if (path == null || !path.startsWith("/")) {
        throw new IllegalArgumentException("path must be an absolute path.");
    }
    // get the starting resource
    String startingResourcePath = path;
    Resource startingResource = null;
    while (startingResource == null) {
        if (startingResourcePath.equals("/")) {
            startingResource = resolver.getResource("/");
            if (startingResource == null) {
                throw new PersistenceException("Access denied for root resource, resource can't be created: " + path);
            }
        } else {
            final Resource r = resolver.getResource(startingResourcePath);
            if (r != null && !ResourceUtil.isSyntheticResource(r)) {
                startingResource = resolver.getResource(startingResourcePath);
                updateNodeType(resolver, startingResourcePath, reqProperties, changes, versioningConfiguration);
                updateMixins(resolver, startingResourcePath, reqProperties, changes, versioningConfiguration);
            } else {
                int pos = startingResourcePath.lastIndexOf('/');
                if (pos > 0) {
                    startingResourcePath = startingResourcePath.substring(0, pos);
                } else {
                    startingResourcePath = "/";
                }
            }
        }
    }
    // is the searched resource already existing?
    if (startingResourcePath.length() == path.length()) {
        return startingResource;
    }
    // create nodes
    int from = (startingResourcePath.length() == 1 ? 1 : startingResourcePath.length() + 1);
    Resource resource = startingResource;
    while (from > 0) {
        final int to = path.indexOf('/', from);
        final String name = to < 0 ? path.substring(from) : path.substring(from, to);
        // although the resource should not exist (according to the first test
        // above)
        // we do a sanety check.
        final Resource child = resource.getChild(name);
        if (child != null && !ResourceUtil.isSyntheticResource(child)) {
            resource = child;
            updateNodeType(resolver, resource.getPath(), reqProperties, changes, versioningConfiguration);
            updateMixins(resolver, resource.getPath(), reqProperties, changes, versioningConfiguration);
        } else {
            final String tmpPath = to < 0 ? path : path.substring(0, to);
            // check for node type
            final String nodeType = getPrimaryType(reqProperties, tmpPath);
            this.jcrSsupport.checkoutIfNecessary(resource, changes, versioningConfiguration);
            try {
                final Map<String, Object> props = new HashMap<>();
                if (nodeType != null) {
                    props.put("jcr:primaryType", nodeType);
                }
                // check for mixin types
                final String[] mixinTypes = getMixinTypes(reqProperties, tmpPath);
                if (mixinTypes != null) {
                    props.put("jcr:mixinTypes", mixinTypes);
                }
                resource = resolver.create(resource, name, props);
            } catch (final PersistenceException e) {
                log.error("Unable to create resource named " + name + " in " + resource.getPath());
                throw e;
            }
            changes.add(Modification.onCreated(resource.getPath()));
        }
        from = to + 1;
    }
    return resource;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 52 with PersistenceException

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

the class CheckoutOperation method doRun.

@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws PersistenceException {
    try {
        Iterator<Resource> res = getApplyToResources(request);
        if (res == null) {
            Resource resource = request.getResource();
            Node node = resource.adaptTo(Node.class);
            if (node == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND, "Missing source " + resource + " for checkout");
                return;
            }
            node.getSession().getWorkspace().getVersionManager().checkout(node.getPath());
            changes.add(Modification.onCheckout(resource.getPath()));
        } else {
            while (res.hasNext()) {
                Resource resource = res.next();
                Node node = resource.adaptTo(Node.class);
                if (node != null) {
                    node.getSession().getWorkspace().getVersionManager().checkout(node.getPath());
                    changes.add(Modification.onCheckout(resource.getPath()));
                }
            }
        }
    } catch (final RepositoryException re) {
        throw new PersistenceException(re.getMessage(), re);
    }
}
Also used : Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Example 53 with PersistenceException

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

the class MockResourceResolver method create.

@Override
public Resource create(Resource parent, String name, Map<String, Object> properties) throws PersistenceException {
    final String path = (parent.getPath().equals("/") ? parent.getPath() + name : parent.getPath() + '/' + name);
    if (this.temporaryResources.containsKey(path)) {
        throw new PersistenceException("Path already exists: " + path);
    }
    synchronized (this.resources) {
        if (this.resources.containsKey(path) && !this.deletedResources.contains(path)) {
            throw new PersistenceException("Path already exists: " + path);
        }
    }
    this.deletedResources.remove(path);
    if (properties == null) {
        properties = new HashMap<String, Object>();
    }
    Resource mockResource = new MockResource(path, properties, this);
    this.temporaryResources.put(path, ResourceUtil.getValueMap(mockResource));
    return mockResource;
}
Also used : PersistenceException(org.apache.sling.api.resource.PersistenceException) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource)

Example 54 with PersistenceException

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

the class MongoDBResourceProvider method commit.

/**
     * @see org.apache.sling.api.resource.ModifyingResourceProvider#commit(ResourceResolver)
     */
public void commit(final ResourceResolver resolver) throws PersistenceException {
    try {
        for (final String deleted : this.deletedResources) {
            final String[] info = this.extractResourceInfo(deleted);
            // check if the collection still exists
            final DBCollection col = this.getCollection(info[0]);
            if (col != null) {
                if (col.findAndRemove(QueryBuilder.start(getPROP_PATH()).is(info[1]).get()) != null) {
                    this.context.notifyRemoved(info);
                }
            }
        }
        for (final MongoDBResource changed : this.changedResources.values()) {
            final DBCollection col = this.context.getDatabase().getCollection(changed.getCollection());
            if (col != null) {
                final String[] info = new String[] { changed.getCollection(), changed.getProperties().get(getPROP_PATH()).toString() };
                // create or update?
                if (changed.getProperties().get(PROP_ID) != null) {
                    col.update(QueryBuilder.start(getPROP_PATH()).is(changed.getProperties().get(getPROP_PATH())).get(), changed.getProperties());
                    this.context.notifyUpdated(info);
                } else {
                    // create
                    col.save(changed.getProperties());
                    this.context.notifyUpdated(info);
                }
            } else {
                throw new PersistenceException("Unable to create collection " + changed.getCollection(), null, changed.getPath(), null);
            }
        }
    } finally {
        this.revert(resolver);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 55 with PersistenceException

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

the class MongoDBResourceProvider method create.

/**
     * @see org.apache.sling.api.resource.ModifyingResourceProvider#create(org.apache.sling.api.resource.ResourceResolver, java.lang.String, java.util.Map)
     */
public Resource create(final ResourceResolver resolver, final String path, final Map<String, Object> properties) throws PersistenceException {
    final String[] info = this.extractResourceInfo(path);
    if (info != null && info.length == 2) {
        final boolean deleted = this.deletedResources.remove(path);
        final MongoDBResource oldResource = (MongoDBResource) this.getResource(resolver, path, info);
        if (!deleted && oldResource != null) {
            throw new PersistenceException("Resource already exists at " + path, null, path, null);
        }
        final DBObject dbObj = new BasicDBObject();
        dbObj.put(getPROP_PATH(), info[1]);
        if (properties != null) {
            for (Map.Entry<String, Object> entry : properties.entrySet()) {
                final String key = propNameToKey(entry.getKey());
                dbObj.put(key, entry.getValue());
            }
        }
        if (deleted && oldResource != null) {
            dbObj.put(PROP_ID, oldResource.getProperties().get(PROP_ID));
        }
        final MongoDBResource rsrc = new MongoDBResource(resolver, path, info[0], dbObj, this);
        this.changedResources.put(path, rsrc);
        return rsrc;
    }
    throw new PersistenceException("Illegal path - unable to create resource at " + path, null, path, null);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) PersistenceException(org.apache.sling.api.resource.PersistenceException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map)

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