Search in sources :

Example 1 with Request

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) + ")");
    }
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Request(org.xwiki.container.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Job(org.xwiki.job.Job)

Example 2 with Request

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) Request(org.xwiki.container.Request) XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) XWikiContext(com.xpn.xwiki.XWikiContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Request

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;
}
Also used : ServletRequest(org.xwiki.container.servlet.ServletRequest) Request(org.xwiki.container.Request) ServletRequest(org.xwiki.container.servlet.ServletRequest) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 4 with Request

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;
}
Also used : Request(org.xwiki.container.Request)

Aggregations

Request (org.xwiki.container.Request)4 ServletRequest (org.xwiki.container.servlet.ServletRequest)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ServletResponse (org.xwiki.container.servlet.ServletResponse)1 Job (org.xwiki.job.Job)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1