use of org.xwiki.job.Job in project xwiki-platform by xwiki.
the class XarExtensionHandler method initJobPackageConfiguration.
private void initJobPackageConfiguration(Request request, boolean defaultConflict) throws InterruptedException {
ExecutionContext context = this.execution.getContext();
if (context != null && context.getProperty(CONTEXTKEY_PACKAGECONFIGURATION) == null) {
Job currentJob = null;
try {
currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
} catch (Exception e) {
this.logger.error("Failed to lookup JobContext, it will be impossible to do interactive install");
}
if (currentJob != null) {
PackageConfiguration configuration = new PackageConfiguration();
context.setProperty(CONTEXTKEY_PACKAGECONFIGURATION, configuration);
DocumentReference userReference = getRequestUserReference(AbstractExtensionValidator.PROPERTY_USERREFERENCE, request);
configuration.setInteractive(request.isInteractive());
configuration.setUser(userReference);
configuration.setVerbose(request.isVerbose());
configuration.setSkipMandatorytDocuments(true);
configuration.setXarExtensionPlan(getXARExtensionPlan());
configuration.setJobStatus(currentJob.getStatus());
// Non blocker conflicts
configuration.setConflictAction(ConflictType.CURRENT_DELETED, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_DELETED), GlobalAction.CURRENT);
configuration.setConflictAction(ConflictType.MERGE_SUCCESS, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_SUCCESS), GlobalAction.MERGED);
// Blocker conflicts
configuration.setConflictAction(ConflictType.CURRENT_EXIST, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_EXIST), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.NEXT);
configuration.setConflictAction(ConflictType.MERGE_FAILURE, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_FAILURE), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.MERGED);
// If user asked to be asked about conflict behavior
if (defaultConflict && currentJob.getStatus().getRequest().isInteractive()) {
XWikiContext xcontext = xcontextProvider.get();
// Make sure the context has the right user
xcontext.setUserReference(userReference);
int extensionConflictSetup = NumberUtils.toInt(xcontext.getWiki().getUserPreference("extensionConflictSetup", xcontext), 0);
if (extensionConflictSetup == 1) {
DefaultConflictActionQuestion question = new DefaultConflictActionQuestion(configuration);
currentJob.getStatus().ask(question, 1, TimeUnit.HOURS);
}
}
}
}
}
use of org.xwiki.job.Job in project xwiki-platform by xwiki.
the class XarExtensionHandler method uninstall.
@Override
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) throws UninstallException {
try {
initializePagesIndex(request);
initJobPackageConfiguration(request, false);
} catch (Exception e) {
throw new UninstallException("Failed to initialize extension plan index", e);
}
// probably not be in an expected state)
if (!request.isRemote()) {
Job currentJob;
try {
currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
} catch (ComponentLookupException e) {
currentJob = null;
}
if (currentJob == null) {
String wiki;
try {
wiki = XarHandlerUtils.getWikiFromNamespace(namespace);
} catch (UnsupportedNamespaceException e) {
throw new UninstallException("Failed to extract wiki id from namespace", e);
}
PackageConfiguration configuration = createPackageConfiguration(null, request, wiki);
try {
XarInstalledExtension xarLocalExtension = (XarInstalledExtension) this.xarRepository.resolve(installedExtension.getId());
Collection<XarEntry> pages = xarLocalExtension.getXarPackage().getEntries();
this.packager.unimportPages(pages, configuration);
} catch (Exception e) {
// Not supposed to be possible
throw new UninstallException("Failed to get xar extension [" + installedExtension.getId() + "] from xar repository", e);
}
} else {
// The actual delete of pages is done in XarExtensionJobFinishedListener
}
}
}
use of org.xwiki.job.Job in project xwiki-platform by xwiki.
the class QuestionJobResourceReferenceHandler method handle.
@Override
public void handle(ParentResourceReference reference) throws ResourceReferenceHandlerException {
List<String> jobId = reference.getPathSegments();
Job job = this.executor.getJob(jobId);
if (job == null) {
throw new ResourceReferenceHandlerException("Cannot find any running job with id " + jobId);
}
Object question = job.getStatus().getQuestion();
if (question == null) {
throw new ResourceReferenceHandlerException("The job with id " + jobId + " does not have any question");
}
Request request = this.container.getRequest();
String prefix = "question/";
String contentType = "text/html; charset=utf-8";
// POST request means answer
if (request instanceof ServletRequest) {
HttpServletRequest httpRequest = ((ServletRequest) request).getHttpServletRequest();
if (httpRequest.getMethod().equals("POST")) {
String token = httpRequest.getParameter("form_token");
// TODO: should probably move this check in some filter triggered by an annotation
if (this.csrf.isTokenValid(token)) {
answer(httpRequest, job, jobId, question);
prefix = "answer/";
contentType = "application/json";
} else {
// TODO: Throw some exception
}
}
}
String jobType = job.getType();
// Provide informations about the job to the template
this.scriptContextManager.getCurrentScriptContext().setAttribute("job", job, ScriptContext.ENGINE_SCOPE);
String[] templates = getTemplates(question.getClass(), jobType, prefix);
if (!tryTemplates(contentType, templates)) {
throw new ResourceReferenceHandlerException("Cannot find any template for the job with id" + jobId + " (" + Arrays.toString(templates) + ")");
}
}
use of org.xwiki.job.Job in project xwiki-platform by xwiki.
the class DeleteAction method deleteToRecycleBin.
protected boolean deleteToRecycleBin(EntityReference entityReference, XWikiContext context) throws XWikiException {
Job deleteJob = startDeleteJob(entityReference, context);
// If the user have asked for an asynchronous delete action...
if (isAsync(context.getRequest())) {
List<String> jobId = deleteJob.getRequest().getId();
// We don't redirect to the delete action because by the time the redirect request reaches the server the
// specified entity may be already deleted and the current user may not have the delete right anymore (e.g.
// the current user is no longer the creator).
sendRedirect(context.getResponse(), Utils.getRedirect("view", "xpage=delete&jobId=" + serializeJobId(jobId), context));
// A redirect has been performed.
return true;
}
// Otherwise...
try {
deleteJob.join();
} catch (InterruptedException e) {
throw new XWikiException(String.format("Failed to delete [%s]", entityReference), e);
}
// No redirect has been performed.
return false;
}
use of org.xwiki.job.Job in project xwiki-platform by xwiki.
the class AbstractJobTest method run.
protected Job run(Request request) throws Throwable {
Job job = getMocker().getComponentUnderTest();
job.initialize(request);
job.run();
Throwable error = job.getStatus().getError();
if (job.getStatus().getError() != null) {
throw error;
}
return job;
}
Aggregations