Search in sources :

Example 6 with XWikiServletResponseStub

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

the class XWikiContextCopierTest method setup.

@Before
public void setup() throws Exception {
    Utils.setComponentManager(mocker);
    originalResponse = new XWikiServletResponseStub();
    original = new XWikiContext();
    // Set some values
    original.setWikiId("wiki");
    DocumentReference userReference = new DocumentReference("wiki", "Space", "Page");
    EntityReferenceSerializer<String> serializer = mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    when(serializer.serialize(userReference)).thenReturn("wiki:Space.Page");
    mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
    original.setUserReference(userReference);
    // Set the mock request
    this.originalRequest = mock(XWikiRequest.class);
    original.setRequest(this.originalRequest);
    Copier<XWikiRequest> requestCopier = mocker.getInstance(new DefaultParameterizedType(null, Copier.class, XWikiRequest.class));
    when(requestCopier.copy(this.originalRequest)).thenReturn(this.originalRequest);
    // Set the stubbed response
    original.setResponse(originalResponse);
    // XWiki mock
    XWiki xwiki = mock(XWiki.class);
    original.setWiki(xwiki);
    // Store mock
    // Simulate the existence of a hibernate session in context
    original.put(HIBSESSION, "opened session");
    store = mock(XWikiStoreInterface.class);
    // clean up will remove the session in the given context
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) {
            XWikiContext context = (XWikiContext) invocation.getArguments()[0];
            context.put(HIBSESSION, null);
            return null;
        }
    }).when(store).cleanUp(any(XWikiContext.class));
    when(xwiki.getStore()).thenReturn(store);
    // URL factory mock
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    XWikiURLFactoryService urlFactoryService = mock(XWikiURLFactoryService.class);
    when(urlFactoryService.createURLFactory(anyInt(), any(XWikiContext.class))).thenReturn(urlFactory);
    when(xwiki.getURLFactoryService()).thenReturn(urlFactoryService);
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiURLFactoryService(com.xpn.xwiki.web.XWikiURLFactoryService) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 7 with XWikiServletResponseStub

use of com.xpn.xwiki.web.XWikiServletResponseStub 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)

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