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