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