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