Search in sources :

Example 11 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class OfficeImporterScriptServiceTest method saveWithOverwrite.

@Test
public void saveWithOverwrite() throws Exception {
    XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    DocumentReference parentReference = new DocumentReference("wiki", "Space", "Parent");
    String syntaxId = "test/1.0";
    String title = "Office Document Title";
    String content = "Office Document Content";
    String fileName = "logo.png";
    byte[] fileContent = new byte[] { 65, 82 };
    when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
    when(doc.getContentAsString(syntaxId)).thenReturn(content);
    when(doc.getArtifacts()).thenReturn(Collections.singletonMap(fileName, fileContent));
    assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, parentReference, title, false));
    verify(documentAccessBridge).setDocumentSyntaxId(documentReference, syntaxId);
    verify(documentAccessBridge).setDocumentContent(documentReference, content, "Created by office importer.", false);
    verify(documentAccessBridge).setDocumentParentReference(documentReference, parentReference);
    verify(documentAccessBridge).setDocumentTitle(documentReference, title);
    verify(documentAccessBridge).setAttachmentContent(new AttachmentReference(fileName, documentReference), fileContent);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 12 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class OfficeMacro method getResourceReference.

private ResourceReference getResourceReference(MacroBlock block, OfficeMacroParameters parameters) throws MacroExecutionException {
    ResourceReference resourceReference = parameters.getReference();
    // Make sure to provide full reference for attachment
    if (resourceReference == null || resourceReference.getType().equals(ResourceType.ATTACHMENT) || !resourceReference.isTyped()) {
        AttachmentReference attachmentReference;
        if (resourceReference == null) {
            // Support former way of indicating the file to view
            String reference = parameters.getAttachment();
            if (StringUtils.isEmpty(reference)) {
                throw new MacroExecutionException("You must specify the 'reference' parameter pointing to the office file to display.");
            }
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(reference, block);
        } else {
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(resourceReference.getReference(), block);
        }
        resourceReference = new AttachmentResourceReference(this.serializer.serialize(attachmentReference));
    }
    return resourceReference;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 13 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class HibernateAttachmentRecycleBinStore method resolveDeletedAttachmentContent.

private DeletedAttachment resolveDeletedAttachmentContent(DeletedAttachment deletedAttachment, boolean bTransaction, boolean failIfNoContent) throws XWikiException {
    AttachmentRecycleBinContentStore contentStore = getAttachmentRecycleBinContentStore(deletedAttachment.getContentStore());
    if (contentStore != null) {
        AttachmentReference reference = deletedAttachment.getAttachmentReference();
        DeletedAttachmentContent content = contentStore.get(reference, deletedAttachment.getDate(), deletedAttachment.getId(), bTransaction);
        if (content == null) {
            if (failIfNoContent) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, "Can't find any content for deleted attachment [" + reference + "] with id [" + deletedAttachment.getId() + "]");
            } else {
                this.logger.warn("Can't find any content for deleted attachment [{}] with id [{}]", reference, deletedAttachment.getId());
            }
        }
        try {
            FieldUtils.writeDeclaredField(deletedAttachment, "content", content, true);
        } catch (IllegalAccessException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to set deleted document content", e);
        }
    }
    return deletedAttachment;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) DeletedAttachmentContent(com.xpn.xwiki.doc.DeletedAttachmentContent) HibernateDeletedAttachmentContent(com.xpn.xwiki.internal.store.hibernate.HibernateDeletedAttachmentContent) AttachmentRecycleBinContentStore(com.xpn.xwiki.store.AttachmentRecycleBinContentStore) XWikiException(com.xpn.xwiki.XWikiException)

Example 14 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class MockitoDownloadActionTest method renderWhenZipExplorerPluginURL.

@Test
public void renderWhenZipExplorerPluginURL() 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/space/page/file.ext/some/path");
    xcontext.setRequest(request);
    // Set the current doc and current wiki
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getAttachment("path")).thenReturn(null);
    xcontext.setDoc(document);
    xcontext.setWikiId("wiki");
    xcontext.setAction("download");
    // 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("path", new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")), EntityResourceAction.VIEW));
    // 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");
    when(attachment.clone()).thenReturn(attachment);
    // Configure an existing doc in the store
    XWiki xwiki = this.oldcore.getSpyXWiki();
    XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    backwardCompatDocument.addAttachment(attachment);
    xwiki.saveDocument(backwardCompatDocument, xcontext);
    // Make sure the user has permission to access the doc
    doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
    // Setup ExecutionContextManager & VelocityManager using in the context backup
    ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
    ExecutionContext ec = this.oldcore.getExecutionContext();
    when(ecm.clone(ec)).thenReturn(ec);
    VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
    // Set the Plugin Manager
    XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
    when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    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) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 15 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AttachmentQueryFilterTest method filterResults.

@Test
public void filterResults() throws Exception {
    List<Object[]> results = new ArrayList<>();
    results.add(new Object[] { "A.B", "image.png" });
    DocumentReferenceResolver<String> currentDocumentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
    DocumentReference documentReference = new DocumentReference("wiki", "A", "B");
    when(currentDocumentReferenceResolver.resolve("A.B")).thenReturn(documentReference);
    List<AttachmentReference> attachmentReferences = this.mocker.getComponentUnderTest().filterResults(results);
    AttachmentReference expectedAttachmentReference = new AttachmentReference("image.png", documentReference);
    assertEquals(Collections.singletonList(expectedAttachmentReference), attachmentReferences);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3