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);
}
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;
}
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;
}
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());
}
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);
}
Aggregations