use of org.xwiki.container.Request 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.container.Request in project xwiki-platform by xwiki.
the class DefaultXWikiContextInitializer method initialize.
@Override
public XWikiContext initialize(ExecutionContext econtext) throws XWikiException {
Request request = this.container.getRequest();
XWikiContext xcontext;
if (!(request instanceof ServletRequest)) {
if (this.fallbackOnStub) {
xcontext = this.contextProvider.createStubContext();
} else {
throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Unsupported request type [" + request.getClass() + "]");
}
} else {
try {
HttpServletRequest httpServletRequest = ((ServletRequest) request).getHttpServletRequest();
HttpServletResponse httpServletReponse = ((ServletResponse) this.container.getResponse()).getHttpServletResponse();
xcontext = initializeXWikiContext(httpServletRequest, httpServletReponse);
// Put the XWikiContext in the ExecutionContext in case the authenticator needs it
if (econtext != null) {
xcontext.declareInExecutionContext(econtext);
}
if (this.authenticate) {
authenticate(xcontext);
}
} catch (XWikiException e) {
if (this.fallbackOnStub) {
xcontext = this.contextProvider.createStubContext();
} else {
throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Failed to initialize XWikiContext", e);
}
}
}
// Put the XWikiContext in the ExecutionContext
if (econtext != null) {
xcontext.declareInExecutionContext(econtext);
}
return xcontext;
}
use of org.xwiki.container.Request in project xwiki-platform by xwiki.
the class XWikiServerXwikiDocumentInitializer method updateDocument.
@Override
public boolean updateDocument(XWikiDocument document) {
boolean needsUpdate = super.updateDocument(document);
// Add a descriptor if none already exist
if (document.getXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS) == null) {
XWikiContext xcontext = this.contextProvider.get();
try {
BaseObject xobject = document.newXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS, xcontext);
xobject.setLargeStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION, "Main wiki");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE, "Main.WebHome");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_LANGUAGE, "en");
xobject.setIntValue(XWikiServerClassDocumentInitializer.FIELD_SECURE, 0);
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_STATE, "active");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_VISIBILITY, "public");
xobject.setLargeStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER, XWikiRightService.SUPERADMIN_USER_FULLNAME);
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, "Home");
Request request = this.container.getRequest();
if (request instanceof ServletRequest) {
ServletRequest servletRequest = (ServletRequest) request;
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER, servletRequest.getHttpServletRequest().getServerName());
} else {
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER, "localhost");
}
needsUpdate = true;
} catch (XWikiException e) {
this.logger.error("Faied to initialize main wiki descriptor", e);
}
}
return needsUpdate;
}
use of org.xwiki.container.Request in project xwiki-platform by xwiki.
the class DefaultContainer method getRequest.
@Override
public Request getRequest() {
Request result = null;
Stack<Request> requests = this.request.get();
if (requests != null) {
result = requests.peek();
}
return result;
}
Aggregations