Search in sources :

Example 71 with XWikiAttachment

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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Timestamp(java.sql.Timestamp) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) AttachmentRecycleBinStore(com.xpn.xwiki.store.AttachmentRecycleBinStore) Test(org.junit.Test)

Example 72 with XWikiAttachment

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()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 73 with XWikiAttachment

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;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 74 with XWikiAttachment

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 75 with XWikiAttachment

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, "&"), "&amp;"));
    }
    try {
        return normalizeURL(new URL(getServerURL(xwikidb, context), path.toString()), context);
    } catch (Exception e) {
        return null;
    }
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException) URL(java.net.URL) XWikiException(com.xpn.xwiki.XWikiException) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7