use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiMockitoTest method rollbackDoesNotSaveUnchangedAttachment.
/**
* @see "XWIKI-9399: Attachment version is incremented when a document is rolled back even if the attachment did not
* change"
*/
@Test
public void rollbackDoesNotSaveUnchangedAttachment() throws Exception {
String version = "1.1";
String fileName = "logo.png";
Date date = new Date();
XWikiAttachment currentAttachment = mock(XWikiAttachment.class);
when(currentAttachment.getAttachmentRevision(version, context)).thenReturn(currentAttachment);
when(currentAttachment.getDate()).thenReturn(new Timestamp(date.getTime()));
when(currentAttachment.getVersion()).thenReturn(version);
when(currentAttachment.getFilename()).thenReturn(fileName);
XWikiAttachment oldAttachment = mock(XWikiAttachment.class);
when(oldAttachment.getFilename()).thenReturn(fileName);
when(oldAttachment.getVersion()).thenReturn(version);
when(oldAttachment.getDate()).thenReturn(date);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
XWikiDocument document = mock(XWikiDocument.class);
when(document.getDocumentReference()).thenReturn(documentReference);
when(document.getAttachmentList()).thenReturn(Arrays.asList(currentAttachment));
when(document.getAttachment(fileName)).thenReturn(currentAttachment);
XWikiDocument result = mock(XWikiDocument.class);
when(result.clone()).thenReturn(result);
when(result.getDocumentReference()).thenReturn(documentReference);
when(result.getAttachmentList()).thenReturn(Arrays.asList(oldAttachment));
when(result.getAttachment(fileName)).thenReturn(oldAttachment);
String revision = "3.5";
when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
AttachmentRecycleBinStore attachmentRecycleBinStore = mock(AttachmentRecycleBinStore.class);
xwiki.setAttachmentRecycleBinStore(attachmentRecycleBinStore);
DocumentReference reference = document.getDocumentReference();
this.mocker.registerMockComponent(ContextualLocalizationManager.class);
when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))).thenReturn(new XWikiDocument(reference));
xwiki.rollback(document, revision, context);
verify(attachmentRecycleBinStore, never()).saveToRecycleBin(same(currentAttachment), any(String.class), any(Date.class), same(context), eq(true));
verify(oldAttachment, never()).setMetaDataDirty(true);
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiTest method testParseTemplateConsidersObjectFieldBeforeAttachment.
/**
* See XWIKI-2098
*/
public void testParseTemplateConsidersObjectFieldBeforeAttachment() throws Exception {
DocumentReference skinReference = new DocumentReference("xwiki", "XWiki", "XWikiSkins");
XWikiDocument skinClass = new XWikiDocument(skinReference);
skinClass.getXClass().addTextAreaField("template.vm", "template", 80, 20);
this.xwiki.saveDocument(skinClass, getContext());
DocumentReference mySkinReference = new DocumentReference("xwiki", "XWiki", "Skin");
XWikiDocument skinDocument = new XWikiDocument(mySkinReference);
BaseObject obj = skinDocument.newXObject(skinReference, getContext());
obj.setLargeStringValue("template.vm", "parsing a field");
XWikiAttachment attachment = new XWikiAttachment();
skinDocument.getAttachmentList().add(attachment);
attachment.setContent(new ByteArrayInputStream("parsing an attachment".getBytes()));
attachment.setFilename("template.vm");
attachment.setDoc(skinDocument);
this.xwiki.saveDocument(skinDocument, getContext());
getContext().put("skin", "XWiki.Skin");
assertEquals("XWiki.Skin", this.xwiki.getSkin(getContext()));
assertFalse(this.xwiki.getDocument(mySkinReference, getContext()).isNew());
assertEquals(skinDocument, this.xwiki.getDocument(mySkinReference, getContext()));
assertEquals("parsing a field", this.xwiki.parseTemplate("template.vm", getContext()));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class UploadAction method uploadAttachment.
/**
* Attach a file to the current document.
*
* @param fieldName the target file field
* @param filename
* @param fileupload the {@link FileUploadPlugin} holding the form data
* @param doc the target document
* @param context the current request context
* @return {@code true} if the file was successfully attached, {@code false} otherwise.
* @throws XWikiException if the form data cannot be accessed, or if the database operation failed
*/
public boolean uploadAttachment(String fieldName, String filename, FileUploadPlugin fileupload, XWikiDocument doc, XWikiContext context) throws XWikiException {
XWikiResponse response = context.getResponse();
DocumentReference usernameReference = context.getUserReference();
XWikiAttachment attachment;
try {
InputStream contentInputStream = fileupload.getFileItemInputStream(fieldName, context);
attachment = doc.setAttachment(filename, contentInputStream, context);
} catch (IOException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION, "Exception while reading uploaded parsed file", e);
}
// Set the document author
doc.setAuthorReference(usernameReference);
if (doc.isNew()) {
doc.setCreatorReference(usernameReference);
}
// Calculate and store mime type
attachment.resetMimeType(context);
// Add a comment to the attachment history.
String attachmentComment = StringUtils.defaultString(context.getRequest().getParameter("comment"));
attachment.setComment(attachmentComment);
// Add a comment to the document history. Include the attachment name, revision and comment.
String documentComment;
ArrayList<String> params = new ArrayList<>();
params.add(filename);
String nextRev = attachment.getNextVersion();
if (StringUtils.isBlank(attachmentComment)) {
params.add(nextRev);
} else {
params.add(String.format("%s (%s)", nextRev, attachmentComment));
}
if (attachment.isImage(context)) {
documentComment = localizePlainOrKey("core.comment.uploadImageComment", params.toArray());
} else {
documentComment = localizePlainOrKey("core.comment.uploadAttachmentComment", params.toArray());
}
// Save the document.
try {
context.getWiki().saveDocument(doc, documentComment, context);
} catch (XWikiException e) {
// check Exception is ERROR_XWIKI_APP_JAVA_HEAP_SPACE when saving Attachment
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
context.put("message", "javaheapspace");
return true;
}
throw e;
}
return false;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiServletURLFactory method findAttachmentForDocRevision.
public XWikiAttachment findAttachmentForDocRevision(XWikiDocument doc, String docRevision, String filename, XWikiContext context) throws XWikiException {
XWikiAttachment attachment = null;
XWikiDocument rdoc = context.getWiki().getDocument(doc, docRevision, context);
if (filename != null) {
attachment = rdoc.getAttachment(filename);
}
return attachment;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiServletURLFactory method createAttachmentURL.
@Override
public URL createAttachmentURL(String filename, String spaces, String name, String action, String querystring, String xwikidb, XWikiContext context) {
if ((context != null) && "viewrev".equals(context.getAction()) && context.get("rev") != null && isContextDoc(xwikidb, spaces, name, context)) {
try {
String docRevision = context.get("rev").toString();
XWikiAttachment attachment = findAttachmentForDocRevision(context.getDoc(), docRevision, filename, context);
if (attachment == null) {
action = "viewattachrev";
} else {
long arbId = findDeletedAttachmentForDocRevision(context.getDoc(), docRevision, filename, context);
return createAttachmentRevisionURL(filename, spaces, name, attachment.getVersion(), arbId, querystring, xwikidb, context);
}
} catch (XWikiException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Exception while trying to get attachment version !", e);
}
}
}
StringBuilder path = new StringBuilder(this.contextPath);
addServletPath(path, xwikidb, context);
// Parse the spaces list into Space References
EntityReference spaceReference = getRelativeEntityReferenceResolver().resolve(spaces, EntityType.SPACE);
addAction(path, spaceReference, action, context);
addSpaces(path, spaceReference, action, context);
addName(path, name, action, context);
addFileName(path, filename, context);
if (!StringUtils.isEmpty(querystring)) {
path.append("?");
path.append(StringUtils.removeEnd(StringUtils.removeEnd(querystring, "&"), "&"));
}
try {
return normalizeURL(new URL(getServerURL(xwikidb, context), path.toString()), context);
} catch (Exception e) {
return null;
}
}
Aggregations