Search in sources :

Example 1 with PersistenceException

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;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 2 with PersistenceException

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);
    }
}
Also used : JcrModifiableValueMap(org.apache.sling.jcr.resource.internal.JcrModifiableValueMap) Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException) Map(java.util.Map) HashMap(java.util.HashMap) JcrModifiableValueMap(org.apache.sling.jcr.resource.internal.JcrModifiableValueMap)

Example 3 with PersistenceException

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;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) LoginException(org.apache.sling.api.resource.LoginException) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 4 with PersistenceException

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;
}
Also used : Resource(org.apache.sling.api.resource.Resource) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 5 with PersistenceException

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);
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException) PersistenceException(org.apache.sling.api.resource.PersistenceException)

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