use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.
the class ScriptManagerImpl method evaluate.
@Override
public Progress evaluate(String scriptContent, Mode mode, Map<String, String> customDefinitions, ResourceResolver resolver) throws RepositoryException, PersistenceException {
Script script = scriptFinder.find(ScriptManager.FILE_FOR_EVALUATION, false, resolver);
if (script != null) {
scriptStorage.remove(script, resolver);
}
InputStream stream = new ByteArrayInputStream(scriptContent.getBytes(StandardCharsets.UTF_8));
script = scriptStorage.save(FILE_FOR_EVALUATION, stream, true, resolver);
Progress progress = process(script, mode, customDefinitions, resolver);
scriptStorage.remove(script, resolver);
return progress;
}
use of com.cognifide.cq.cqsm.api.logger.Progress 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.logger.Progress in project APM by Cognifide.
the class ScriptValidationServlet method doPost.
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final String content = request.getParameter("content");
if (StringUtils.isEmpty(content)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
ServletUtils.writeMessage(response, "error", "Script content is required");
return;
}
try {
final Progress progress = scriptManager.evaluate(content, Mode.VALIDATION, request.getResourceResolver());
if (progress.isSuccess()) {
ServletUtils.writeMessage(response, "success", "Script passes validation");
} else {
final String message = progress.getLastError().getLastMessageText();
final Map<String, Object> context = new HashMap<>();
if (message != null) {
context.put("error", message);
}
ServletUtils.writeMessage(response, "error", "Script does not pass validation", context);
}
} catch (RepositoryException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeMessage(response, "error", String.format("Script' cannot be validated because of " + "repository error: %s", e.getMessage()));
}
}
use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.
the class StartupExecutor method runScript.
private void runScript(ResourceResolver resolver, Script script) throws PersistenceException {
final String scriptPath = script.getPath();
try {
scriptManager.process(script, Mode.VALIDATION, resolver);
if (script.isValid()) {
final Progress progress = scriptManager.process(script, Mode.AUTOMATIC_RUN, resolver);
logStatus(scriptPath, progress.isSuccess());
} else {
LOG.warn("Startup executor cannot execute script which is not valid: {}", scriptPath);
}
} catch (RepositoryException e) {
LOG.error("Script cannot be processed because of repository error: {}", e);
}
}
use of com.cognifide.cq.cqsm.api.logger.Progress 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