Search in sources :

Example 1 with ServletRequest

use of org.xwiki.container.servlet.ServletRequest 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 ServletRequest

use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.

the class ContextAndActionURLNormalizer method getActionServletMapping.

/**
 * Get the path prefix used for the Struts Action Servlet, either a prefix similar to the one used in the current
 * request if it also passes through the Action servlet, or using the default path configured for it.
 *
 * @return a path used for triggering the Struts Action Servlet (may be the empty string)
 */
protected String getActionServletMapping() {
    String result = this.defaultServletMapping;
    if (this.container.getRequest() instanceof ServletRequest) {
        HttpServletRequest hsRequest = ((ServletRequest) this.container.getRequest()).getHttpServletRequest();
        result = StringUtils.strip(hsRequest.getServletPath(), IGNORED_MAPPING_CHARACTERS);
        if (!this.validServletMappings.contains(result)) {
            // The current request doesn't pass through the Action servlet, don't reuse the path prefix
            result = this.defaultServletMapping;
        }
    }
    return result;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Example 3 with ServletRequest

use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.

the class DefaultCSRFTokenTest method configure.

@Before
public void configure() throws Exception {
    // set up mocked dependencies
    // document access bridge
    final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final CopyStringMatcher returnValue = new CopyStringMatcher(resubmitUrl + "?", "");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getDocumentURL(with(aNonNull(DocumentReference.class)), with("view"), with(returnValue), with(aNull(String.class)));
            will(returnValue);
            allowing(mockDocumentAccessBridge).getDocumentURL(with(aNull(DocumentReference.class)), with("view"), with(aNull(String.class)), with(aNull(String.class)));
            will(returnValue(mockDocumentUrl));
            allowing(mockDocumentAccessBridge).getCurrentDocumentReference();
            will(returnValue(null));
        }
    });
    // configuration
    final CSRFTokenConfiguration mockConfiguration = getComponentManager().getInstance(CSRFTokenConfiguration.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockConfiguration).isEnabled();
            will(returnValue(true));
        }
    });
    // request
    final HttpSession mockSession = getMockery().mock(HttpSession.class);
    final HttpServletRequest httpRequest = getMockery().mock(HttpServletRequest.class);
    final ServletRequest servletRequest = new ServletRequest(httpRequest);
    getMockery().checking(new Expectations() {

        {
            allowing(httpRequest).getRequestURL();
            will(returnValue(new StringBuffer(mockDocumentUrl)));
            allowing(httpRequest).getRequestURI();
            will(returnValue(mockDocumentUrl));
            allowing(httpRequest).getParameterMap();
            will(returnValue(new HashMap<String, String[]>()));
            allowing(httpRequest).getSession();
            will(returnValue(mockSession));
        }
    });
    // session
    getMockery().checking(new Expectations() {

        {
            allowing(mockSession).getAttribute(with(any(String.class)));
            will(returnValue(new HashMap<String, Object>()));
        }
    });
    // container
    final Container mockContainer = getComponentManager().getInstance(Container.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockContainer).getRequest();
            will(returnValue(servletRequest));
        }
    });
    // logging
    getMockery().checking(new Expectations() {

        {
            // Ignore all calls to debug()
            ignoring(any(Logger.class)).method("debug");
        }
    });
    this.csrf = getComponentManager().getInstance(CSRFToken.class);
}
Also used : Expectations(org.jmock.Expectations) ServletRequest(org.xwiki.container.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultCSRFToken(org.xwiki.csrf.internal.DefaultCSRFToken) HttpSession(javax.servlet.http.HttpSession) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Logger(org.slf4j.Logger) HttpServletRequest(javax.servlet.http.HttpServletRequest) Container(org.xwiki.container.Container) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 4 with ServletRequest

use of org.xwiki.container.servlet.ServletRequest 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 5 with ServletRequest

use of org.xwiki.container.servlet.ServletRequest 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)

Aggregations

ServletRequest (org.xwiki.container.servlet.ServletRequest)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Request (org.xwiki.container.Request)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)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 HttpSession (javax.servlet.http.HttpSession)1 Expectations (org.jmock.Expectations)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 Container (org.xwiki.container.Container)1 RequestInitializerManager (org.xwiki.container.RequestInitializerManager)1 ServletContainerException (org.xwiki.container.servlet.ServletContainerException)1 ServletResponse (org.xwiki.container.servlet.ServletResponse)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)1