Search in sources :

Example 6 with XWikiServletRequest

use of com.xpn.xwiki.web.XWikiServletRequest in project xwiki-platform by xwiki.

the class XWikiTest method testLanguageSelection.

public void testLanguageSelection() throws Exception {
    getContext().setRequest(new XWikiServletRequest(null) {

        @SuppressWarnings("unchecked")
        @Override
        public Enumeration getLocales() {
            ArrayList<Locale> locales = new ArrayList<Locale>();
            locales.add(new Locale("*"));
            locales.add(new Locale("en_US"));
            locales.add(new Locale("fr"));
            locales.add(new Locale("de"));
            return IteratorUtils.asEnumeration(locales.iterator());
        }

        @Override
        public String getHeader(String s) {
            if ("language".equals(s)) {
                return null;
            }
            return "en";
        }

        @Override
        public Cookie getCookie(String cookieName) {
            return null;
        }
    });
    // Set the wiki to multilingual mode.
    getConfigurationSource().setProperty("multilingual", "1");
    assertEquals("fr", this.xwiki.getLanguagePreference(getContext()));
}
Also used : Locale(java.util.Locale) Cookie(javax.servlet.http.Cookie) XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 7 with XWikiServletRequest

use of com.xpn.xwiki.web.XWikiServletRequest in project xwiki-platform by xwiki.

the class SchedulerPlugin method prepareJobStubContext.

/**
 * Create and feed a stub context for the job execution thread. Stub context data are retrieved from job object
 * fields "contextUser", "contextLang", "contextDatabase". If one of this field is empty (this would typically
 * happen on the first schedule operation), it is instead retrieved from the passed context, and the job object is
 * updated with this value. This mean that this method may modify the passed object.
 *
 * @param job the job for which the context will be prepared
 * @param context the XWikiContext at preparation time. This is a real context associated with a servlet request
 * @return the stub context prepared with job data
 */
private XWikiContext prepareJobStubContext(BaseObject job, XWikiContext context) throws SchedulerPluginException {
    boolean jobNeedsUpdate = false;
    String cUser = job.getStringValue("contextUser");
    if (cUser.equals("")) {
        // The context user has not been filled yet.
        // We can suppose it's the first scheduling. Let's assume it's the context user
        cUser = context.getUser();
        job.setStringValue("contextUser", cUser);
        jobNeedsUpdate = true;
    }
    String cLang = job.getStringValue("contextLang");
    if (cLang.equals("")) {
        cLang = context.getLanguage();
        job.setStringValue("contextLang", cLang);
        jobNeedsUpdate = true;
    }
    String iDb = context.getWikiId();
    String cDb = job.getStringValue("contextDatabase");
    if (cDb.equals("") || !cDb.equals(iDb)) {
        cDb = context.getWikiId();
        job.setStringValue("contextDatabase", cDb);
        jobNeedsUpdate = true;
    }
    if (jobNeedsUpdate) {
        try {
            context.setWikiId(cDb);
            XWikiDocument jobHolder = context.getWiki().getDocument(job.getName(), context);
            jobHolder.setMinorEdit(true);
            context.getWiki().saveDocument(jobHolder, context);
        } catch (XWikiException e) {
            throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, "Failed to prepare context for job with job name " + job.getStringValue("jobName"), e);
        } finally {
            context.setWikiId(iDb);
        }
    }
    // lets now build the stub context
    XWikiContext scontext = context.clone();
    scontext.setWiki(context.getWiki());
    context.getWiki().getStore().cleanUp(context);
    // We are sure the context request is a real servlet request
    // So we force the dummy request with the current host
    XWikiServletRequestStub dummy = new XWikiServletRequestStub();
    dummy.setHost(context.getRequest().getHeader("x-forwarded-host"));
    dummy.setScheme(context.getRequest().getScheme());
    dummy.setContextPath(context.getRequest().getContextPath());
    XWikiServletRequest request = new XWikiServletRequest(dummy);
    scontext.setRequest(request);
    // Force forged context response to a stub response, since the current context response
    // will not mean anything anymore when running in the scheduler's thread, and can cause
    // errors.
    XWikiResponse stub = new XWikiServletResponseStub();
    scontext.setResponse(stub);
    // feed the dummy context
    scontext.setUser(cUser);
    scontext.setLanguage(cLang);
    scontext.setWikiId(cDb);
    scontext.setMainXWiki(context.getMainXWiki());
    if (scontext.getURL() == null) {
        try {
            scontext.setURL(new URL("http://www.mystuburl.com/"));
        } catch (Exception e) {
        // the URL is well formed, I promise
        }
    }
    com.xpn.xwiki.web.XWikiURLFactory xurf = context.getURLFactory();
    if (xurf == null) {
        xurf = context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context);
    }
    scontext.setURLFactory(xurf);
    try {
        XWikiDocument cDoc = context.getWiki().getDocument(job.getDocumentReference(), context);
        scontext.setDoc(cDoc);
    } catch (Exception e) {
        throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, "Failed to prepare context for job with job name " + job.getStringValue("jobName"), e);
    }
    return scontext;
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL) XWikiException(com.xpn.xwiki.XWikiException) SchedulerException(org.quartz.SchedulerException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with XWikiServletRequest

use of com.xpn.xwiki.web.XWikiServletRequest in project xwiki-platform by xwiki.

the class DefaultXWikiContextInitializer method initializeXWikiContext.

private static XWikiContext initializeXWikiContext(HttpServletRequest request, HttpServletResponse response) throws XWikiException {
    XWikiServletContext xwikiEngine = new XWikiServletContext(request.getServletContext());
    XWikiServletRequest xwikiRequest = new XWikiServletRequest(request);
    XWikiServletResponse xwikiResponse = new XWikiServletResponse(response);
    // Create the XWiki context.
    XWikiContext context = Utils.prepareContext("", xwikiRequest, xwikiResponse, xwikiEngine);
    // Initialize the XWiki database. XWiki#getXWiki(XWikiContext) calls XWikiContext.setWiki(XWiki).
    XWiki xwiki = XWiki.getXWiki(context);
    // Initialize the URL factory.
    context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context));
    // Prepare the localized resources, according to the selected language.
    xwiki.prepareResources(context);
    return context;
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) XWikiServletContext(com.xpn.xwiki.web.XWikiServletContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki)

Aggregations

XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)8 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)4 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)4 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XWikiServletContext (com.xpn.xwiki.web.XWikiServletContext)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)1 XWikiUser (com.xpn.xwiki.user.api.XWikiUser)1 XWikiResponse (com.xpn.xwiki.web.XWikiResponse)1 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)1 Image (java.awt.Image)1 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1