Search in sources :

Example 1 with XWikiResponse

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

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

the class SVGPlugin method outputSVGImageFromFile.

public void outputSVGImageFromFile(String filename, XWikiContext context) throws IOException {
    File ofile = getTempFile(filename);
    byte[] svgbytes = readSVGImage(ofile);
    XWikiResponse response = context.getResponse();
    context.setFinished(true);
    response.setDateHeader("Last-Modified", ofile.lastModified());
    response.setContentLength(svgbytes.length);
    response.setContentType(context.getEngineContext().getMimeType(filename));
    OutputStream os = response.getOutputStream();
    os.write(svgbytes);
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) OutputStream(java.io.OutputStream) File(java.io.File)

Example 3 with XWikiResponse

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

the class XWikiAuthServiceImplTest method before.

@Before
public void before() throws Exception {
    this.authService = new XWikiAuthServiceImpl();
    // Dummy response
    XWikiResponse xwikiResponse = mock(XWikiResponse.class);
    when(xwikiResponse.encodeURL(any())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArgument(0);
        }
    });
    this.oldcore.getXWikiContext().setResponse(xwikiResponse);
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Example 4 with XWikiResponse

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

the class XWikiAuthServiceImplTest method testStripContextPathFromURLWhenRootWebAppAndJSessionId.

@Test
public void testStripContextPathFromURLWhenRootWebAppAndJSessionId() throws Exception {
    doReturn("").when(this.oldcore.getSpyXWiki()).getWebAppPath(any(XWikiContext.class));
    // Simulate a rewrite filter that would add a jsession id and add a leading slash!
    XWikiResponse xwikiResponse = mock(XWikiResponse.class);
    when(xwikiResponse.encodeURL("http://localhost:8080")).thenReturn("http://localhost:8080/;jsessionid=0AF95AFB8997826B936C0397DF6A0C7F");
    this.oldcore.getXWikiContext().setResponse(xwikiResponse);
    assertEquals("/something", this.authService.stripContextPathFromURL(new URL("http://localhost:8080/something"), this.oldcore.getXWikiContext()));
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL) Test(org.junit.Test)

Example 5 with XWikiResponse

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

the class XWikiAuthServiceImplTest method testStripContextPathFromURLWhenOutBoundRewriteRuleChangingContextPath.

/**
 * Simulates the use case when the {@code HttpServletResponse.encodeURL()} changes the context path.
 */
@Test
public void testStripContextPathFromURLWhenOutBoundRewriteRuleChangingContextPath() throws Exception {
    doReturn("xwiki/").when(this.oldcore.getSpyXWiki()).getWebAppPath(any(XWikiContext.class));
    XWikiResponse xwikiResponse = mock(XWikiResponse.class);
    when(xwikiResponse.encodeURL(any())).thenReturn("http://localhost:8080/anothercontext;jsessionid=0AF95AFB8997826B936C0397DF6A0C7F?language=en");
    this.oldcore.getXWikiContext().setResponse(xwikiResponse);
    // Note: the passed URL to stripContextPathFromURL() has also gone through encodeURL() which is why its
    // context path has been changed from "xwiki" to "anothercontext".
    assertEquals("/something", this.authService.stripContextPathFromURL(new URL("http://localhost:8080/anothercontext/something"), this.oldcore.getXWikiContext()));
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL) Test(org.junit.Test)

Aggregations

XWikiResponse (com.xpn.xwiki.web.XWikiResponse)11 OutputStream (java.io.OutputStream)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 URL (java.net.URL)4 File (java.io.File)3 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)2 Test (org.junit.Test)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)1 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)1 CachePolicy (com.xpn.xwiki.web.sx.SxSource.CachePolicy)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SchedulerException (org.quartz.SchedulerException)1