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()));
}
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;
}
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;
}
Aggregations