Search in sources :

Example 31 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentService method getAttachmentForFullname.

Attachment getAttachmentForFullname(String imgFullname) {
    AttachmentURLCommand attURL = new AttachmentURLCommand();
    Attachment att = null;
    try {
        XWikiDocument attDoc = getContext().getWiki().getDocument(webUtils.resolveDocumentReference(attURL.getPageFullName(imgFullname)), getContext());
        XWikiAttachment xatt = attService.getAttachmentNameEqual(attDoc, attURL.getAttachmentName(imgFullname));
        att = attService.getApiAttachment(xatt);
    } catch (XWikiException xwe) {
        LOGGER.error("Exception getting attachment Document.", xwe);
    } catch (AttachmentNotExistsException anee) {
        LOGGER.error("Attachment [{}] not found.", imgFullname, anee);
    } catch (NoAccessRightsException nore) {
        LOGGER.error("No access rights on attachment [{}]", imgFullname, nore);
    }
    return att;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) AttachmentURLCommand(com.celements.web.plugin.cmd.AttachmentURLCommand) AttachmentNotExistsException(com.celements.model.access.exception.AttachmentNotExistsException) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) NoAccessRightsException(com.celements.rights.access.exceptions.NoAccessRightsException) XWikiException(com.xpn.xwiki.XWikiException)

Example 32 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentServiceTest method testAddAttachment.

@Test
public void testAddAttachment() throws Exception {
    DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.pdf"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    replayDefault();
    service.addAttachment("Test.Img;file.pdf");
    verifyDefault();
    List<Attachment> atts = service.getAttachmentList(false);
    assertNotNull(atts);
    assertEquals(1, atts.size());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 33 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentServiceTest method testGetAttachmentForFullname.

@Test
public void testGetAttachmentForFullname() throws Exception {
    DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.jpg"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    replayDefault();
    assertEquals(att.getFilename(), service.getAttachmentForFullname("Test.Img;file.jpg").getFilename());
    verifyDefault();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 34 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentServiceTest method testEmbedImagesInContent_inner_externalURLwww.

@Test
public void testEmbedImagesInContent_inner_externalURLwww() throws Exception {
    DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.jpg"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    String imgTag = "<img class=\"abc\" src=\"www.test.com/download/Test/Img/file.jpg?" + "bla=123\" />";
    String content = "Test text with " + imgTag + " image included";
    Set<String> tags = new HashSet<>();
    tags.add(imgTag);
    replayDefault();
    String result = service.embedImagesInContent(content, tags);
    verifyDefault();
    assertTrue(result, result.contains("src=\"cid:file.jpg\""));
    assertFalse(result, result.contains("/download/"));
    assertFalse(result, result.contains("www"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentServiceTest method testEmbedImagesInContent_inner_externalURLhttp.

@Test
public void testEmbedImagesInContent_inner_externalURLhttp() throws Exception {
    DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.jpg"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    String imgTag = "<img class=\"abc\" src=\"http://www.test.com/download/Test/Img/" + "file.jpg?bla=123\" />";
    String content = "Test text with " + imgTag + " image included";
    Set<String> tags = new HashSet<>();
    tags.add(imgTag);
    replayDefault();
    String result = service.embedImagesInContent(content, tags);
    verifyDefault();
    assertTrue(result, result.contains("src=\"cid:file.jpg\""));
    assertFalse(result, result.contains("/download/"));
    assertFalse(result, result.contains("http"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Attachment (com.xpn.xwiki.api.Attachment)38 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)24 Test (org.junit.Test)21 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 Document (com.xpn.xwiki.api.Document)14 DocumentReference (org.xwiki.model.reference.DocumentReference)14 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)8 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)6 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 InputStream (java.io.InputStream)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)3 File (java.io.File)3 IOException (java.io.IOException)3