use of com.xpn.xwiki.web.XWikiServletResponseStub in project xwiki-platform by xwiki.
the class XWikiContextCopier method copy.
/**
* {@inheritDoc}
*
* Any in progress session/transaction on the store retained by the original context will be closed/rollbacked
* prior cloning (see {@link com.xpn.xwiki.store.XWikiStoreInterface#cleanUp(XWikiContext)}). Therefore,
* the copy operation has a side effect on the original context. You should never copy a context
* while a create/update transaction is in progress, since some changes would get rollbacked.
*/
@Override
public XWikiContext copy(XWikiContext originalXWikiContext) {
// Clean up the store session/transaction before cloning. For the hibernate store, in progress
// session/transaction is stored in the context, and would be swallow copied when the context is cloned.
// Cleaning after clone would not help, since it would close/rollback the session/transaction still referenced
// in the original context as well, causing this context to be corrupted.
// The correct way would be to not shallow clone the session/transaction when cloning the original context,
// since session/transaction are lazy initialize on request when missing.
originalXWikiContext.getWiki().getStore().cleanUp(originalXWikiContext);
// This is still a shallow clone, but at least for stuff like wikiID and userReference it gets the job done.
XWikiContext clonedXWikiContext = originalXWikiContext.clone();
// lets now build the stub context
// Copy the request from the context.
clonedXWikiContext.setRequest(this.xwikiRequestCloner.copy(originalXWikiContext.getRequest()));
// 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();
clonedXWikiContext.setResponse(stub);
// feed the dummy context
if (clonedXWikiContext.getURL() == null) {
try {
clonedXWikiContext.setURL(new URL("http://www.mystuburl.com/"));
} catch (Exception e) {
// the URL is clearly well formed
}
}
XWikiURLFactory xurf = originalXWikiContext.getWiki().getURLFactoryService().createURLFactory(originalXWikiContext.getMode(), originalXWikiContext);
clonedXWikiContext.setURLFactory(xurf);
return clonedXWikiContext;
}
use of com.xpn.xwiki.web.XWikiServletResponseStub in project xwiki-platform by xwiki.
the class DefaultXWikiStubContextProvider method initialize.
@Override
public void initialize(XWikiContext context) {
XWikiContext newContext = context.clone();
newContext.setCacheDuration(0);
newContext.setUserReference(null);
newContext.setLocale(null);
newContext.setWikiId(context.getMainXWiki());
// Cleanup
newContext.flushClassCache();
// So we force the dummy request with the current host
if (newContext.getRequest() != null) {
XWikiServletRequestStub initialRequest = new XWikiServletRequestStub();
initialRequest.setHost(newContext.getRequest().getHeader("x-forwarded-host"));
initialRequest.setScheme(newContext.getRequest().getScheme());
XWikiServletRequest request = new XWikiServletRequest(initialRequest);
newContext.setRequest(request);
}
// Get rid of the real response
if (newContext.getResponse() != null) {
XWikiServletResponseStub initialResponse = new XWikiServletResponseStub();
// anything to keep ?
XWikiServletResponse response = new XWikiServletResponse(initialResponse);
newContext.setResponse(response);
}
// We decide arbitrarily to use the Servlet URL Factory since it's the "standard" factory.
if (newContext.getURLFactory() != null) {
XWikiURLFactory urlf = newContext.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_SERVLET, context);
newContext.setURLFactory(urlf);
}
this.initialContext = newContext;
this.logger.debug("Stub context initialized.");
}
use of com.xpn.xwiki.web.XWikiServletResponseStub in project xwiki-platform by xwiki.
the class DefaultXWikiStubContextProvider method createStubContext.
@Override
public XWikiContext createStubContext() {
XWikiContext stubContext;
if (this.initialContext != null) {
stubContext = this.initialContext.clone();
// We make sure to not share the same Request instance with several threads
if (this.initialContext.getRequest() != null) {
XWikiServletRequestStub stubRequest = new XWikiServletRequestStub();
stubRequest.setHost(this.initialContext.getRequest().getHeader("x-forwarded-host"));
stubRequest.setScheme(this.initialContext.getRequest().getScheme());
XWikiServletRequest request = new XWikiServletRequest(stubRequest);
stubContext.setRequest(request);
}
// We make sure to not share the same Response instance with several threads
if (this.initialContext.getResponse() != null) {
XWikiServletResponseStub stubResponse = new XWikiServletResponseStub();
XWikiServletResponse response = new XWikiServletResponse(stubResponse);
stubContext.setResponse(response);
}
// We make sure to not share the same document instance with several threads
stubContext.setDoc(new XWikiDocument(this.defaultDocumentReferenceProvider.get()));
} else {
stubContext = null;
}
return stubContext;
}
use of com.xpn.xwiki.web.XWikiServletResponseStub in project xwiki-platform by xwiki.
the class PageTest method setUpForPageTest.
/**
* Configures the various Components and their mocks with default values for page tests.
*
* @throws Exception in case of errors
*/
@Before
public void setUpForPageTest() throws Exception {
// Configure mocks from OldcoreRule
context = oldcore.getXWikiContext();
xwiki = oldcore.getSpyXWiki();
// We need this one because some component in its init creates a query...
when(oldcore.getQueryManager().createQuery(any(String.class), any(String.class))).thenReturn(mock(Query.class));
// Set up a fake Request
// Configure request so that $!request.outputSyntax" == 'plain
// Need to be executed before ecm.initialize() so that XWikiScriptContextInitializer will initialize the
// script context properly
request = new XWikiServletRequestStub();
request.setScheme("http");
context.setRequest(request);
response = new XWikiServletResponseStub();
context.setResponse(response);
ExecutionContextManager ecm = mocker.getInstance(ExecutionContextManager.class);
ecm.initialize(oldcore.getExecutionContext());
// Let the user have view access to all pages
when(oldcore.getMockRightService().hasAccessLevel(eq("view"), eq("XWiki.XWikiGuest"), any(), eq(context))).thenReturn(true);
// Set up URL Factory
URLFactorySetup.setUp(xwiki, context);
// Set up Localization
LocalizationSetup.setUp(mocker);
// Set up Skin Extensions
SkinExtensionSetup.setUp(xwiki, context);
}
use of com.xpn.xwiki.web.XWikiServletResponseStub in project xwiki-platform by xwiki.
the class OldCoreHelper method createXWikiContext.
/**
* @param wikiId id of the wiki for which to prepare the XWiki Context (e.g. {@code xwiki})
* @param hibernateConfig the Hibernate config fill containing the database definition (JDBC driver, username and
* password, etc)
* @return a valid XWikiContext using the passed Hibernate configuration and passed database name
* @throws Exception failed to initialize context.
*/
// TODO: Replace the Hibernate config file with a list of parameters required for the packaging operation
private XWikiContext createXWikiContext() throws Exception {
Utils.setComponentManager(this.componentManager);
this.xcontext = new XWikiContext();
this.xcontext.put(ComponentManager.class.getName(), this.componentManager);
// Initialize the Container fields (request, response, session).
try {
ExecutionContext econtext = new ExecutionContext();
// Bridge with old XWiki Context, required for old code.
this.xcontext.declareInExecutionContext(econtext);
this.ecim.initialize(econtext);
} catch (ExecutionContextException e) {
throw new Exception("Failed to initialize Execution Context.", e);
}
this.xcontext.setWikiId(wikiId);
this.xcontext.setMainXWiki(wikiId);
// Use a dummy Request/Response even in daemon mode so that XWiki's initialization can create a Servlet URL
// Factory and any code requiring those objects will work.
this.xcontext.setRequest(new XWikiServletRequestStub());
this.xcontext.setResponse(new XWikiServletResponseStub());
// Use a dummy URL so that XWiki's initialization can create a Servlet URL Factory. We could also have
// registered a custom XWikiURLFactory against XWikiURLFactoryService but it's more work.
this.xcontext.setURL(new URL("http://localhost/xwiki/bin/DummyAction/DumySpace/DummyPage"));
// Set a dummy Document in the context to act as the current document since when a document containing
// objects is imported it'll generate Object diff events and the algorithm to compute an object diff
// currently requires rendering object properties, which requires a current document in the context.
this.xcontext.setDoc(new XWikiDocument(new DocumentReference(wikiId, "dummySpace", "dummyPage")));
XWikiConfig config = new XWikiConfig();
config.put("xwiki.store.class", "com.xpn.xwiki.store.XWikiHibernateStore");
// The XWikiConfig object requires path to be in unix format (i.e. with forward slashes)
String hibernateConfigInUnixFormat = hibernateConfig.getPath().replace('\\', '/');
config.put("xwiki.store.hibernate.path", hibernateConfigInUnixFormat);
config.put("xwiki.store.hibernate.updateschema", "1");
// Enable backlinks so that when documents are imported their backlinks will be saved too
config.put("xwiki.backlinks", "1");
XWiki xwiki = new XWiki(config, this.xcontext, null, true);
this.xcontext.setUserReference(new DocumentReference("xwiki", "XWiki", "superadmin"));
try {
this.xcontext.setURLFactory(new XWikiServletURLFactory(new URL("http://localhost:8080"), "xwiki/", "bin/"));
} catch (MalformedURLException e) {
// doesn't work with external code.
throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to set up URL Factory", e);
}
// Trigger extensions that need to initialize the database (create classes, etc.)
xwiki.initializeWiki(this.xcontext.getMainXWiki(), true, this.xcontext);
return this.xcontext;
}
Aggregations