use of org.apache.sling.api.resource.PersistenceException in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class FormStructureHelperImpl method updateFormStructure.
@Override
public Resource updateFormStructure(Resource formResource) {
if (formResource != null) {
ResourceResolver resolver = formResource.getResourceResolver();
if (isFormContainer(formResource)) {
// add default action type, form id and action path
ModifiableValueMap formProperties = formResource.adaptTo(ModifiableValueMap.class);
if (formProperties != null) {
try {
if (formProperties.get(FormsConstants.START_PROPERTY_ACTION_TYPE, String.class) == null) {
formProperties.put(FormsConstants.START_PROPERTY_ACTION_TYPE, FormsConstants.DEFAULT_ACTION_TYPE);
String defaultContentPath = "/content/usergenerated" + formResource.getPath().replaceAll("^.content", "").replaceAll("jcr.content.*", "") + "cq-gen" + System.currentTimeMillis() + "/";
formProperties.put(FormsConstants.START_PROPERTY_ACTION_PATH, defaultContentPath);
}
resolver.commit();
} catch (PersistenceException e) {
LOGGER.error("Unable to add default action type and form id " + formResource, e);
}
} else {
LOGGER.error("Resource is not adaptable to ValueMap - unable to add default action type and " + "form id for " + formResource);
}
}
}
return null;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JcrResourceProvider method create.
@Override
public Resource create(@Nonnull final ResolveContext<JcrProviderState> ctx, final String path, final Map<String, Object> properties) throws PersistenceException {
// check for node type
final Object nodeObj = (properties != null ? properties.get(NodeUtil.NODE_TYPE) : null);
// check for sling:resourcetype
final String nodeType;
if (nodeObj != null) {
nodeType = nodeObj.toString();
} else {
final Object rtObj = (properties != null ? properties.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY) : null);
boolean isNodeType = false;
if (rtObj != null) {
final String resourceType = rtObj.toString();
if (resourceType.indexOf(':') != -1 && resourceType.indexOf('/') == -1) {
try {
ctx.getProviderState().getSession().getWorkspace().getNodeTypeManager().getNodeType(resourceType);
isNodeType = true;
} catch (final RepositoryException ignore) {
// we expect this, if this isn't a valid node type, therefore ignoring
}
}
}
if (isNodeType) {
nodeType = rtObj.toString();
} else {
nodeType = null;
}
}
final String jcrPath = path;
if (jcrPath == null) {
throw new PersistenceException("Unable to create node at " + path, null, path, null);
}
Node node = null;
try {
final int lastPos = jcrPath.lastIndexOf('/');
final Node parent;
if (lastPos == 0) {
parent = ctx.getProviderState().getSession().getRootNode();
} else {
parent = (Node) ctx.getProviderState().getSession().getItem(jcrPath.substring(0, lastPos));
}
final String name = jcrPath.substring(lastPos + 1);
if (nodeType != null) {
node = parent.addNode(name, nodeType);
} else {
node = parent.addNode(name);
}
if (properties != null) {
// create modifiable map
final JcrModifiableValueMap jcrMap = new JcrModifiableValueMap(node, ctx.getProviderState().getHelperData());
// check mixin types first
final Object value = properties.get(NodeUtil.MIXIN_TYPES);
if (value != null) {
jcrMap.put(NodeUtil.MIXIN_TYPES, value);
}
for (final Map.Entry<String, Object> entry : properties.entrySet()) {
if (!IGNORED_PROPERTIES.contains(entry.getKey())) {
try {
jcrMap.put(entry.getKey(), entry.getValue());
} catch (final IllegalArgumentException iae) {
try {
node.remove();
} catch (final RepositoryException re) {
// we ignore this
}
throw new PersistenceException(iae.getMessage(), iae, path, entry.getKey());
}
}
}
}
return new JcrNodeResource(ctx.getResourceResolver(), path, null, node, ctx.getProviderState().getHelperData());
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to create node at " + jcrPath, e, path, null);
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class ResourceResolverControl method checkSourceAndDest.
private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
// check source
final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
if (srcNode == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
AuthenticatedResourceProvider srcProvider = null;
try {
srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (srcProvider == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
if (srcResource == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
// check destination
final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
if (destNode == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
AuthenticatedResourceProvider destProvider = null;
try {
destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (destProvider == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
final Resource destResource = destProvider.getResource(destAbsPath, null, null);
if (destResource == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
// check for sub providers of src and dest
if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
return srcProvider;
}
return null;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class DefaultConfigurationPersistenceStrategy method deleteConfiguration.
@Override
public boolean deleteConfiguration(ResourceResolver resourceResolver, String configResourcePath) {
if (!config.enabled()) {
return false;
}
Resource resource = resourceResolver.getResource(configResourcePath);
if (resource != null) {
try {
log.trace("! Delete resource {}", resource.getPath());
resourceResolver.delete(resource);
} catch (PersistenceException ex) {
throw convertPersistenceException("Unable to delete configuration at " + configResourcePath, ex);
}
}
commit(resourceResolver, configResourcePath);
return true;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class DefaultConfigurationPersistenceStrategy method deleteChildren.
private void deleteChildren(Resource resource) {
ResourceResolver resourceResolver = resource.getResourceResolver();
try {
for (Resource child : resource.getChildren()) {
log.trace("! Delete resource {}", child.getPath());
resourceResolver.delete(child);
}
} catch (PersistenceException ex) {
throw convertPersistenceException("Unable to remove children from " + resource.getPath(), ex);
}
}
Aggregations