Search in sources :

Example 1 with XWikiServletResponseStub

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;
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL)

Example 2 with XWikiServletResponseStub

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.");
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 3 with XWikiServletResponseStub

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;
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 4 with XWikiServletResponseStub

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);
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) Query(org.xwiki.query.Query) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) Before(org.junit.Before)

Example 5 with XWikiServletResponseStub

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;
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletURLFactory(com.xpn.xwiki.web.XWikiServletURLFactory) MalformedURLException(java.net.MalformedURLException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextException(org.xwiki.context.ExecutionContextException) URL(java.net.URL) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiConfig(com.xpn.xwiki.XWikiConfig) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)3 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)3 URL (java.net.URL)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiResponse (com.xpn.xwiki.web.XWikiResponse)2 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)2 Before (org.junit.Before)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiConfig (com.xpn.xwiki.XWikiConfig)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 XWikiServletURLFactory (com.xpn.xwiki.web.XWikiServletURLFactory)1 XWikiURLFactoryService (com.xpn.xwiki.web.XWikiURLFactoryService)1 MalformedURLException (java.net.MalformedURLException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1