Search in sources :

Example 1 with XWikiServletRequest

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

the class XWikiRequestCopier method copy.

@Override
public XWikiRequest copy(XWikiRequest originalRequest) throws CloneFailedException {
    if (originalRequest == null) {
        return null;
    }
    XWikiServletRequestStub dummyRequest = new XWikiServletRequestStub();
    dummyRequest.setHost(originalRequest.getHeader("x-forwarded-host"));
    dummyRequest.setScheme(originalRequest.getScheme());
    dummyRequest.setContextPath(originalRequest.getContextPath());
    dummyRequest.setrequestURL(originalRequest.getRequestURL());
    dummyRequest.setServerName(originalRequest.getServerName());
    return new XWikiServletRequest(dummyRequest);
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub)

Example 2 with XWikiServletRequest

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

the class XWikiContextInitializationFilter method initializeXWikiContext.

/**
 * Initializes the XWiki context.
 *
 * @param request the request being processed
 * @param response the response
 * @throws ServletException if the initialization fails
 */
protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException {
    try {
        // Not all request types specify an action (e.g. GWT-RPC) so we default to the empty string.
        String action = "";
        XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext());
        XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request);
        XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response);
        // Create the XWiki context.
        XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine);
        // parameter is specified.
        if (this.mode >= 0) {
            context.setMode(this.mode);
        }
        // Initialize the Container component which is the new way of transporting the Context in the new component
        // architecture. Further initialization might require the Container component.
        initializeContainerComponent(context);
        // 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);
        // Initialize the current user.
        XWikiUser user = context.getWiki().checkAuth(context);
        if (user != null) {
            DocumentReferenceResolver<String> documentReferenceResolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
            SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId()));
            DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace);
            context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
        }
    } catch (XWikiException e) {
        throw new ServletException("Failed to initialize the XWiki context.", e);
    }
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiServletContext(com.xpn.xwiki.web.XWikiServletContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ServletException(javax.servlet.ServletException) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with XWikiServletRequest

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

the class ImagePluginTest method testCacheOfScaledAttachment.

@Test
public void testCacheOfScaledAttachment() throws Exception {
    XWikiContext xcontext = this.oldCore.getXWikiContext();
    XWikiAttachment attachment = Mockito.mock(XWikiAttachment.class);
    Mockito.when(attachment.getMimeType(xcontext)).thenReturn("image/png");
    InputStream attachmentInputStream = new ByteArrayInputStream(testPngImageContent);
    Mockito.when(attachment.getContentInputStream(xcontext)).thenReturn(attachmentInputStream);
    Mockito.when(attachment.clone()).thenReturn(attachment);
    XWikiAttachmentContent attachmentContent = Mockito.mock(XWikiAttachmentContent.class);
    Mockito.when(attachment.getAttachment_content()).thenReturn(attachmentContent);
    Mockito.when(attachmentContent.getContentInputStream()).thenReturn(attachmentInputStream);
    OutputStream attachmentOutputStream = Mockito.mock(OutputStream.class);
    Mockito.when(attachmentContent.getContentOutputStream()).thenReturn(attachmentOutputStream);
    CacheManager cacheManager = this.oldCore.getMocker().getInstance(CacheManager.class);
    Cache<Object> imageCache = Mockito.mock(Cache.class);
    Mockito.when(cacheManager.createNewLocalCache(ArgumentMatchers.any())).thenReturn(imageCache);
    XWikiServletRequest request = Mockito.mock(XWikiServletRequest.class);
    Mockito.when(request.getParameter("width")).thenReturn("30");
    Mockito.when(request.getParameter("height")).thenReturn("30");
    xcontext.setRequest(request);
    Image image = Mockito.mock(Image.class);
    Mockito.when(image.getWidth(null)).thenReturn(400);
    Mockito.when(image.getHeight(null)).thenReturn(300);
    Mockito.when(imageProcessor.readImage(attachmentInputStream)).thenReturn(image);
    RenderedImage renderedImage = Mockito.mock(RenderedImage.class);
    Mockito.when(imageProcessor.scaleImage(image, 30, 30)).thenReturn(renderedImage);
    XWikiAttachment scaled = plugin.downloadAttachment(attachment, xcontext);
    String cacheKey = "0;null;30;30;false;-1.0";
    Mockito.when(imageCache.get(cacheKey)).thenReturn(scaled);
    // Load again, this time from cache.
    Assert.assertSame(scaled, plugin.downloadAttachment(attachment, xcontext));
    Mockito.verify(imageProcessor, Mockito.times(1)).writeImage(renderedImage, "image/png", .5F, attachmentOutputStream);
    Mockito.verify(imageCache, Mockito.times(1)).set(cacheKey, attachment);
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Image(java.awt.Image) RenderedImage(java.awt.image.RenderedImage) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) ByteArrayInputStream(java.io.ByteArrayInputStream) CacheManager(org.xwiki.cache.CacheManager) RenderedImage(java.awt.image.RenderedImage) Test(org.junit.Test)

Example 4 with XWikiServletRequest

use of com.xpn.xwiki.web.XWikiServletRequest 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 5 with XWikiServletRequest

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

Aggregations

XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)8 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)4 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)4 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XWikiServletContext (com.xpn.xwiki.web.XWikiServletContext)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)1 XWikiUser (com.xpn.xwiki.user.api.XWikiUser)1 XWikiResponse (com.xpn.xwiki.web.XWikiResponse)1 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)1 Image (java.awt.Image)1 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1