Search in sources :

Example 16 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class XWiki method getXWiki.

/**
 * Return the XWiki object (as in "the Wiki API") corresponding to the requested wiki.
 * <p>
 * Unless <code>wait</code> is false the method return right away null if XWiki is not yet initialized.
 *
 * @param wait wait until XWiki is initialized
 * @param xcontext see {@link XWikiContext}
 * @return an XWiki object configured for the wiki corresponding to the current request
 * @throws XWikiException if the requested URL does not correspond to a real wiki, or if there's an error in the
 *             storage
 */
public static XWiki getXWiki(boolean wait, XWikiContext xcontext) throws XWikiException {
    XWiki xwiki = getMainXWiki(wait, xcontext);
    if (xwiki == null) {
        return null;
    }
    // Extract Entity Resource from URL and put it in the Execution Context
    EntityResourceReference entityResourceReference = initializeResourceFromURL(xcontext);
    // If not an entity resource reference assume main wiki
    if (entityResourceReference == null) {
        return xwiki;
    }
    // Get the wiki id
    String wikiId = entityResourceReference.getEntityReference().extractReference(EntityType.WIKI).getName();
    if (wikiId.equals(xcontext.getMainXWiki())) {
        // The main wiki was requested.
        return xwiki;
    }
    // Check if the wiki exists by checking if a descriptor exists for the wiki id.
    WikiDescriptorManager wikiDescriptorManager = Utils.getComponent(WikiDescriptorManager.class);
    WikiDescriptor descriptor;
    try {
        descriptor = wikiDescriptorManager.getById(wikiId);
    } catch (WikiManagerException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_STORE_MISC, String.format("Failed find wiki descriptor for wiki id [%s]", wikiId), e);
    }
    if (descriptor == null) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_DOES_NOT_EXIST, String.format("The wiki [%s] does not exist", wikiId));
    }
    // Initialize wiki
    xcontext.setWikiId(wikiId);
    xcontext.setOriginalWikiId(wikiId);
    if (!xwiki.initializeWiki(wikiId, wait, xcontext)) {
        // The wiki is still initializing
        return null;
    }
    return xwiki;
}
Also used : WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor)

Example 17 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class DownloadActionTest method setRequestExpectations.

private void setRequestExpectations(String uri, String id, String forceDownload, String range, long modifiedSince, String attachmentName) {
    ResourceReference rr = new EntityResourceReference(new AttachmentReference(attachmentName, this.documentReference), EntityResourceAction.VIEW);
    when(this.request.getRequestURI()).thenReturn(uri);
    when(this.request.getParameter("id")).thenReturn(id);
    when(this.request.getDateHeader("If-Modified-Since")).thenReturn(modifiedSince);
    when(this.request.getParameter("force-download")).thenReturn(forceDownload);
    when(this.request.getHeader("Range")).thenReturn(range);
    when(this.resourceReferenceManager.getResourceReference()).thenReturn(rr);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference)

Example 18 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class MockitoDownloadActionTest method renderWhenAttachmentIsInANestedSpace.

@Test
public void renderWhenAttachmentIsInANestedSpace() throws Exception {
    DownloadAction action = new DownloadAction();
    XWikiContext xcontext = this.oldcore.getXWikiContext();
    // Set the Request URL
    XWikiServletRequestStub request = new XWikiServletRequestStub();
    request.setRequestURI("http://localhost:8080/xwiki/bin/download/space1/space2/page/file.ext");
    xcontext.setRequest(request);
    // Setup the returned attachment
    XWikiAttachment attachment = mock(XWikiAttachment.class);
    when(attachment.getContentSize(xcontext)).thenReturn(100);
    Date now = new Date();
    when(attachment.getDate()).thenReturn(now);
    when(attachment.getFilename()).thenReturn("file.ext");
    when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
    when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
    // Set the current doc
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getAttachment("file.ext")).thenReturn(attachment);
    xcontext.setDoc(document);
    // Set the Plugin Manager
    XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
    when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    // Set the Response
    XWikiResponse response = mock(XWikiResponse.class);
    StubServletOutputStream ssos = new StubServletOutputStream();
    when(response.getOutputStream()).thenReturn(ssos);
    xcontext.setResponse(response);
    // Set the Resource Reference Manager used to parse the URL and extract the attachment name
    ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
    when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("file.ext", new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page")), EntityResourceAction.VIEW));
    // Note: we don't give PR and the attachment is not an authorized mime type.
    assertNull(action.render(xcontext));
    // This is the test, we verify what is set in the response
    verify(response).setContentType("mimetype");
    verify(response).setHeader("Accept-Ranges", "bytes");
    verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
    verify(response).setDateHeader("Last-Modified", now.getTime());
    verify(response).setContentLength(100);
    assertEquals("test", ssos.baos.toString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)18 ResourceReference (org.xwiki.resource.ResourceReference)8 ResourceReferenceManager (org.xwiki.resource.ResourceReferenceManager)7 URL (java.net.URL)5 Test (org.junit.Test)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ResourceType (org.xwiki.resource.ResourceType)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 IOException (java.io.IOException)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 EntityReference (org.xwiki.model.reference.EntityReference)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiPluginManager (com.xpn.xwiki.plugin.XWikiPluginManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Date (java.util.Date)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWiki (com.xpn.xwiki.XWiki)1