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");
}
}
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);
}
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();
}
}
}
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);
}
}
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");
}
}
Aggregations