Search in sources :

Example 36 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project APM by Cognifide.

the class ReplicationExecutor method runReplicated.

private JobResult runReplicated(ResourceResolver resolver, String searchPath) {
    JobResult result = JobResult.FAILED;
    final Script script = scriptFinder.find(searchPath, resolver);
    if (script == null) {
        LOG.warn("Replicated script cannot be found by script manager: {}", searchPath);
    } else if (ExecutionMode.ON_DEMAND.equals(script.getExecutionMode()) && script.isPublishRun()) {
        try {
            process(script, resolver);
            result = JobResult.OK;
        } catch (PersistenceException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return result;
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 37 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project APM by Cognifide.

the class Checksum method save.

public void save(ResourceResolver resolver, String checksum) {
    try {
        Resource storage = getOrCreateChecksumStorage(resolver, CHECKSUM_STORAGE_PATH);
        storage.adaptTo(ModifiableValueMap.class).put(filename, checksum);
        resolver.commit();
    } catch (PersistenceException e) {
        LOG.error("Cannot update checksum for {} script", filename, e);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 38 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project APM by Cognifide.

the class ScriptConfigServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final ResourceResolver resolver = request.getResourceResolver();
    final String searchPath = request.getParameter("scriptPath");
    final Script script = scriptFinder.find(searchPath, resolver);
    if (script == null) {
        ServletUtils.writeMessage(response, "error", "Script not found: " + searchPath);
        return;
    }
    final ModifiableScript modifiableScript = new ModifiableScriptWrapper(resolver, script);
    try {
        final String executionMode = request.getParameter("executionMode");
        if (executionMode != null) {
            modifiableScript.setExecutionMode(ExecutionMode.valueOf(executionMode.toUpperCase()));
        }
        final String executionEnabled = request.getParameter("executionEnabled");
        if (executionEnabled != null) {
            modifiableScript.setExecutionEnabled(BooleanUtils.toBoolean(executionEnabled));
        }
        ServletUtils.writeMessage(response, "success", "Script configuration updated");
    } catch (PersistenceException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ServletUtils.writeMessage(response, "error", "Cannot update script configuration: " + e.getMessage());
    }
}
Also used : ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) Script(com.cognifide.cq.cqsm.api.scripts.Script) ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) ModifiableScriptWrapper(com.cognifide.cq.cqsm.core.scripts.ModifiableScriptWrapper) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 39 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class JCRSupportImpl method checkoutIfNecessary.

public void checkoutIfNecessary(final Resource resource, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
    if (resource != null && versioningConfiguration.isAutoCheckout()) {
        final Node node = resource.adaptTo(Node.class);
        if (node != null) {
            try {
                Node versionableNode = findVersionableAncestor(node);
                if (versionableNode != null) {
                    if (!versionableNode.isCheckedOut()) {
                        versionableNode.getSession().getWorkspace().getVersionManager().checkout(versionableNode.getPath());
                        changes.add(Modification.onCheckout(versionableNode.getPath()));
                    }
                }
            } catch (final RepositoryException re) {
                throw new PersistenceException(re.getMessage(), re);
            }
        }
    }
}
Also used : Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Example 40 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class JCRSupportImpl method orderNode.

/**
     * Orders the given node according to the specified command. The following
     * syntax is supported: &lt;xmp&gt; | first | before all child nodes | before A |
     * before child node A | after A | after child node A | last | after all
     * nodes | N | at a specific position, N being an integer &lt;/xmp&gt;
     *
     * @param request The http request
     * @param item node to order
     * @param changes The list of modifications
     * @throws RepositoryException if an error occurs
     */
public void orderNode(final SlingHttpServletRequest request, final Resource resource, final List<Modification> changes) throws PersistenceException {
    final String command = request.getParameter(SlingPostConstants.RP_ORDER);
    if (command == null || command.length() == 0) {
        // nothing to do
        return;
    }
    final Node node = resource.adaptTo(Node.class);
    if (node == null) {
        return;
    }
    try {
        final Node parent = node.getParent();
        String next = null;
        if (command.equals(SlingPostConstants.ORDER_FIRST)) {
            next = parent.getNodes().nextNode().getName();
        } else if (command.equals(SlingPostConstants.ORDER_LAST)) {
            next = "";
        } else if (command.startsWith(SlingPostConstants.ORDER_BEFORE)) {
            next = command.substring(SlingPostConstants.ORDER_BEFORE.length());
        } else if (command.startsWith(SlingPostConstants.ORDER_AFTER)) {
            String name = command.substring(SlingPostConstants.ORDER_AFTER.length());
            NodeIterator iter = parent.getNodes();
            while (iter.hasNext()) {
                Node n = iter.nextNode();
                if (n.getName().equals(name)) {
                    if (iter.hasNext()) {
                        next = iter.nextNode().getName();
                    } else {
                        next = "";
                    }
                }
            }
        } else {
            // check for integer
            try {
                // 01234
                // abcde move a -> 2 (above 3)
                // bcade move a -> 1 (above 1)
                // bacde
                int newPos = Integer.parseInt(command);
                next = "";
                NodeIterator iter = parent.getNodes();
                while (iter.hasNext() && newPos >= 0) {
                    Node n = iter.nextNode();
                    if (n.getName().equals(node.getName())) {
                        // if old node is found before index, need to
                        // inc index
                        newPos++;
                    }
                    if (newPos == 0) {
                        next = n.getName();
                        break;
                    }
                    newPos--;
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("provided node ordering command is invalid: " + command);
            }
        }
        if (next != null) {
            if (next.equals("")) {
                next = null;
            }
            parent.orderBefore(node.getName(), next);
            changes.add(Modification.onOrder(node.getPath(), next));
            if (logger.isDebugEnabled()) {
                logger.debug("Node {} moved '{}'", node.getPath(), command);
            }
        } else {
            throw new IllegalArgumentException("provided node ordering command is invalid: " + command);
        }
    } catch (final RepositoryException re) {
        throw new PersistenceException("Unable to order resource", re, resource.getPath(), null);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

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