Search in sources :

Example 11 with PersistenceException

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

the class MockedResourceResolver method delete.

@Override
public void delete(Resource resource) throws PersistenceException {
    if (resources.contains(resource)) {
        resources.remove(resource);
        Node node = resource.adaptTo(Node.class);
        try {
            node.remove();
        } catch (RepositoryException e) {
            throw new PersistenceException("RepositoryException: " + e, e);
        }
    } else {
        throw new UnsupportedOperationException("Not implemented");
    }
}
Also used : Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Example 12 with PersistenceException

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

the class ResourceHelper method moveResourceWithResourceAPI.

/**
     * Move resource to given path with Sling Resource API.
     * @param res Source resource
     * @param path target path
     * @throws PersistenceException
     */
private static void moveResourceWithResourceAPI(Resource res, String path) throws PersistenceException {
    String parentPath = ResourceUtil.getParent(path);
    Resource parent = res.getResourceResolver().getResource(parentPath);
    if (parent == null) {
        throw new PersistenceException("Parent resource does not exist: " + parentPath);
    }
    // make move with copy + delete
    copyResourceWithResourceAPI(res, parent, ResourceUtil.getName(path));
    res.getResourceResolver().delete(res);
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 13 with PersistenceException

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

the class SyncTokenService method storeMySyncToken.

private boolean storeMySyncToken(String syncTokenId) {
    logger.trace("storeMySyncToken: start");
    if (slingId == null) {
        logger.info("storeMySyncToken: not yet activated (slingId is null)");
        return false;
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        final Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
        ModifiableValueMap syncTokens = resource.adaptTo(ModifiableValueMap.class);
        boolean updateToken = false;
        if (!syncTokens.containsKey(slingId)) {
            updateToken = true;
        } else {
            Object existingToken = syncTokens.get(slingId);
            if (existingToken == null || !existingToken.equals(syncTokenId)) {
                updateToken = true;
            }
        }
        if (updateToken) {
            syncTokens.put(slingId, syncTokenId);
            resourceResolver.commit();
            logger.info("storeMySyncToken: stored syncToken of slingId=" + slingId + " as=" + syncTokenId);
        } else {
            logger.info("storeMySyncToken: syncToken was left unchanged for slingId=" + slingId + " at=" + syncTokenId);
        }
        return true;
    } catch (LoginException e) {
        logger.error("storeMySyncToken: could not login for storing my syncToken: " + e, e);
        return false;
    } catch (PersistenceException e) {
        logger.error("storeMySyncToken: got PersistenceException while storing my syncToken: " + e, e);
        return false;
    } finally {
        logger.trace("storeMySyncToken: end");
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 14 with PersistenceException

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

the class ResourceHelper method moveResource.

/**
     * Move resource to given path. Try to do it optimized via JCR API.
     * If JCR is not available, fallback to Sling Resource API. 
     * @param res Source resource
     * @param path Target path
     * @throws PersistenceException
     */
public static void moveResource(Resource res, String path) throws PersistenceException {
    Node node = res.adaptTo(Node.class);
    if (node != null) {
        try {
            Session session = node.getSession();
            session.move(res.getPath(), path);
        } catch (RepositoryException re) {
            throw new PersistenceException("Move from " + res.getPath() + " to " + path + " failed.", re);
        }
    } else {
        moveResourceWithResourceAPI(res, path);
    }
}
Also used : Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 15 with PersistenceException

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

the class MockedResourceResolver method delete.

@Override
public void delete(Resource resource) throws PersistenceException {
    if (resources.contains(resource)) {
        resources.remove(resource);
        Node node = resource.adaptTo(Node.class);
        try {
            node.remove();
        } catch (RepositoryException e) {
            throw new PersistenceException("RepositoryException: " + e, e);
        }
    } else {
        throw new UnsupportedOperationException("Not implemented");
    }
}
Also used : Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

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