Search in sources :

Example 11 with Script

use of com.cognifide.cq.cqsm.api.scripts.Script in project APM by Cognifide.

the class ScriptRemoveServlet method removeSingleFile.

private void removeSingleFile(ResourceResolver resolver, SlingHttpServletResponse response, String fileName) throws IOException {
    if (StringUtils.isEmpty(fileName)) {
        ServletUtils.writeMessage(response, "error", "File name to be removed cannot be empty");
    } else {
        final Script script = scriptFinder.find(fileName, resolver);
        if (script == null) {
            ServletUtils.writeMessage(response, "error", String.format("Script not found: '%s'", fileName));
        } else {
            final String scriptPath = script.getPath();
            try {
                scriptStorage.remove(script, resolver);
                ServletUtils.writeMessage(response, "success", String.format("Script removed successfully: %s", scriptPath));
            } catch (RepositoryException e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                ServletUtils.writeJson(response, String.format("Cannot remove script: '%s'." + " Repository error: %s", scriptPath, e.getMessage()));
            }
        }
    }
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) RepositoryException(javax.jcr.RepositoryException)

Example 12 with Script

use of com.cognifide.cq.cqsm.api.scripts.Script in project APM by Cognifide.

the class ScriptRunServlet method doPost.

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
    ResourceResolver resolver = request.getResourceResolver();
    final String searchPath = request.getParameter("file");
    final String modeName = request.getParameter("mode");
    if (StringUtils.isEmpty(searchPath)) {
        ServletUtils.writeMessage(response, "error", "Please set the script file name: -d \"file=[name]\"");
        return;
    }
    if (StringUtils.isEmpty(modeName)) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        ServletUtils.writeMessage(response, "error", "Running mode not specified.");
        return;
    }
    final Script script = scriptFinder.find(searchPath, resolver);
    if (script == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        ServletUtils.writeMessage(response, "error", String.format("Script not found: %s", searchPath));
        return;
    }
    try {
        final Mode mode = Mode.fromString(modeName, Mode.DRY_RUN);
        final Progress progressLogger = scriptManager.process(script, mode, resolver);
        if (progressLogger.isSuccess()) {
            ServletUtils.writeJson(response, ProgressHelper.toJson(progressLogger.getEntries()));
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            ServletUtils.writeJson(response, ProgressHelper.toJson(progressLogger.getLastError()));
        }
    } catch (RepositoryException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ServletUtils.writeMessage(response, "error", String.format("Script cannot be executed because of" + " repository error: %s", e.getMessage()));
    }
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) Progress(com.cognifide.cq.cqsm.api.logger.Progress) Mode(com.cognifide.cq.cqsm.api.executors.Mode) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) RepositoryException(javax.jcr.RepositoryException)

Example 13 with Script

use of com.cognifide.cq.cqsm.api.scripts.Script in project APM by Cognifide.

the class ScriptUploadServlet method doPost.

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
    try {
        final Map<String, InputStream> files = Maps.newHashMap();
        for (RequestParameter file : request.getRequestParameters("file")) {
            files.put(file.getFileName(), file.getInputStream());
        }
        final List<Script> scripts = scriptStorage.saveAll(files, isOverwrite(request), request.getResourceResolver());
        if (shouldRedirect(request)) {
            response.sendRedirect(getRedirectTo(request));
        } else {
            ServletUtils.writeJson(response, ScriptUtils.toJson(scripts));
        }
    } catch (RepositoryException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ServletUtils.writeJson(response, "Cannot save script in repository: " + e.getMessage());
    }
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) InputStream(java.io.InputStream) RequestParameter(org.apache.sling.api.request.RequestParameter) RepositoryException(javax.jcr.RepositoryException)

Example 14 with Script

use of com.cognifide.cq.cqsm.api.scripts.Script in project APM by Cognifide.

the class ModifyExecutor method runModified.

private void runModified(ResourceResolver resolver) throws PersistenceException {
    final List<Script> scripts = scriptFinder.findAll(filterOnModify(resolver), resolver);
    if (scripts.size() > 0) {
        LOG.info("Executor will try to run following scripts: {}", scripts.size());
        LOG.info(MessagingUtils.describeScripts(scripts));
        for (Script script : scripts) {
            runSafe(resolver, script);
        }
    } else {
        LOG.info("Executor has not detected any changes");
    }
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script)

Example 15 with Script

use of com.cognifide.cq.cqsm.api.scripts.Script in project APM by Cognifide.

the class ScriptRunnerJobConsumer method process.

@Override
public JobResult process(final Job job) {
    LOG.info("Script runner job consumer started");
    final String id = job.getId();
    final Mode mode = getMode(job);
    final String userId = getUserId(job);
    return SlingHelper.resolveDefault(resolverFactory, userId, new ResolveCallback<JobResult>() {

        @Override
        public JobResult resolve(ResourceResolver resolver) {
            JobResult result = JobResult.FAILED;
            final Script script = getScript(job, resolver);
            if (script != null && mode != null) {
                try {
                    final Progress progressLogger = scriptManager.process(script, mode, resolver);
                    jobResultsCache.put(id, progressLogger);
                    result = JobResult.OK;
                } catch (RepositoryException | PersistenceException e) {
                    LOG.error("Script manager failed to process script", e);
                    result = JobResult.FAILED;
                }
            }
            return result;
        }
    }, JobResult.FAILED);
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) Progress(com.cognifide.cq.cqsm.api.logger.Progress) Mode(com.cognifide.cq.cqsm.api.executors.Mode) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Aggregations

Script (com.cognifide.cq.cqsm.api.scripts.Script)21 RepositoryException (javax.jcr.RepositoryException)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 Progress (com.cognifide.cq.cqsm.api.logger.Progress)4 ModifiableScript (com.cognifide.cq.cqsm.api.scripts.ModifiableScript)4 Mode (com.cognifide.cq.cqsm.api.executors.Mode)3 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 Resource (org.apache.sling.api.resource.Resource)3 ModifiableScriptWrapper (com.cognifide.cq.cqsm.core.scripts.ModifiableScriptWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Session (javax.jcr.Session)2 ActionCreationException (com.cognifide.cq.cqsm.api.exceptions.ActionCreationException)1 ExecutionException (com.cognifide.cq.cqsm.api.exceptions.ExecutionException)1 InstanceDetails (com.cognifide.cq.cqsm.api.history.InstanceDetails)1 ScriptImpl (com.cognifide.cq.cqsm.core.scripts.ScriptImpl)1 OperateCallback (com.cognifide.cq.cqsm.core.utils.sling.OperateCallback)1