Search in sources :

Example 6 with EmailDelivery

use of org.alfresco.service.cmr.email.EmailDelivery in project alfresco-repository by Alfresco.

the class EmailServiceImplTest method testMessagesToDocument.

/**
 * ALF-12297
 *
 * Test messages being sent to a cm:content node
 */
public void testMessagesToDocument() throws Exception {
    logger.debug("Start testMessagesToDocument");
    String TEST_EMAIL = "buffy@sunnydale.high";
    String TEST_SUBJECT = "Practical Bee Keeping";
    String TEST_LONG_SUBJECT = "This is a very very long name in particular it is greater than eitghty six characters which was a problem explored in ALF-9544";
    // TODO Investigate why setting PROP_EMAIL on createPerson does not work.
    NodeRef person = personService.getPerson(TEST_USER);
    if (person == null) {
        logger.debug("new person created");
        Map<QName, Serializable> props = new HashMap<QName, Serializable>();
        props.put(ContentModel.PROP_USERNAME, TEST_USER);
        props.put(ContentModel.PROP_EMAIL, TEST_EMAIL);
        person = personService.createPerson(props);
    }
    nodeService.setProperty(person, ContentModel.PROP_EMAIL, TEST_EMAIL);
    Set<String> auths = authorityService.getContainedAuthorities(null, "GROUP_EMAIL_CONTRIBUTORS", true);
    if (!auths.contains(TEST_USER)) {
        authorityService.addAuthority("GROUP_EMAIL_CONTRIBUTORS", TEST_USER);
    }
    String companyHomePathInStore = "/app:company_home";
    String storePath = "workspace://SpacesStore";
    StoreRef storeRef = new StoreRef(storePath);
    NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
    List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, companyHomePathInStore, null, namespaceService, false);
    NodeRef companyHomeNodeRef = nodeRefs.get(0);
    assertNotNull("company home is null", companyHomeNodeRef);
    String companyHomeDBID = ((Long) nodeService.getProperty(companyHomeNodeRef, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    // String testUserDBID = ((Long)nodeService.getProperty(person, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    NodeRef testUserHomeFolder = (NodeRef) nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER);
    assertNotNull("testUserHomeFolder is null", testUserHomeFolder);
    // String testUserHomeDBID = ((Long)nodeService.getProperty(testUserHomeFolder, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    // Clean up old messages in test folder
    List<ChildAssociationRef> assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    for (ChildAssociationRef assoc : assocs) {
        nodeService.deleteNode(assoc.getChildRef());
    }
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
    properties.put(ContentModel.PROP_NAME, "bees");
    properties.put(ContentModel.PROP_DESCRIPTION, "bees - test doc for email tests");
    ChildAssociationRef testDoc = nodeService.createNode(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "bees"), ContentModel.TYPE_CONTENT, properties);
    NodeRef testDocNodeRef = testDoc.getChildRef();
    String testDocDBID = ((Long) nodeService.getProperty(testDocNodeRef, ContentModel.PROP_NODE_DBID)).toString();
    /**
     * Send From the test user TEST_EMAIL to the test user's home
     */
    String from = TEST_EMAIL;
    String to = testDocDBID + "@alfresco.com";
    String content = "hello world";
    Session sess = Session.getDefaultInstance(new Properties());
    assertNotNull("sess is null", sess);
    SMTPMessage msg = new SMTPMessage(sess);
    InternetAddress[] toa = { new InternetAddress(to) };
    msg.setFrom(new InternetAddress(TEST_EMAIL));
    msg.setRecipients(Message.RecipientType.TO, toa);
    msg.setSubject(TEST_SUBJECT);
    msg.setContent(content, "text/plain");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    msg.writeTo(bos);
    InputStream is = IOUtils.toInputStream(bos.toString());
    assertNotNull("is is null", is);
    SubethaEmailMessage m = new SubethaEmailMessage(is);
    /**
     * Turn on overwriteDuplicates
     */
    logger.debug("Step 1: send an email to a doc");
    EmailDelivery delivery = new EmailDelivery(to, from, null);
    emailService.importMessage(delivery, m);
    assertTrue(nodeService.hasAspect(testDocNodeRef, ForumModel.ASPECT_DISCUSSABLE));
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) SubethaEmailMessage(org.alfresco.email.server.impl.subetha.SubethaEmailMessage) Serializable(java.io.Serializable) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Properties(java.util.Properties) NodeRef(org.alfresco.service.cmr.repository.NodeRef) EmailDelivery(org.alfresco.service.cmr.email.EmailDelivery) Session(javax.mail.Session)

Example 7 with EmailDelivery

use of org.alfresco.service.cmr.email.EmailDelivery in project alfresco-repository by Alfresco.

the class EmailServiceImplTest method testMultipleMessagesToFolder.

/**
 * ALF-1878
 *
 * Duplicate incoming email Subjects over-write each other
 */
public void testMultipleMessagesToFolder() throws Exception {
    logger.debug("Start testFromName");
    String TEST_EMAIL = "buffy@sunnydale.high";
    String TEST_SUBJECT = "Practical Bee Keeping";
    String TEST_LONG_SUBJECT = "This is a very very long name in particular it is greater than eitghty six characters which was a problem explored in ALF-9544";
    // TODO Investigate why setting PROP_EMAIL on createPerson does not work.
    NodeRef person = personService.getPerson(TEST_USER);
    if (person == null) {
        logger.debug("new person created");
        Map<QName, Serializable> props = new HashMap<QName, Serializable>();
        props.put(ContentModel.PROP_USERNAME, TEST_USER);
        props.put(ContentModel.PROP_EMAIL, TEST_EMAIL);
        person = personService.createPerson(props);
    }
    nodeService.setProperty(person, ContentModel.PROP_EMAIL, TEST_EMAIL);
    Set<String> auths = authorityService.getContainedAuthorities(null, "GROUP_EMAIL_CONTRIBUTORS", true);
    if (!auths.contains(TEST_USER)) {
        authorityService.addAuthority("GROUP_EMAIL_CONTRIBUTORS", TEST_USER);
    }
    String companyHomePathInStore = "/app:company_home";
    String storePath = "workspace://SpacesStore";
    StoreRef storeRef = new StoreRef(storePath);
    NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
    List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, companyHomePathInStore, null, namespaceService, false);
    NodeRef companyHomeNodeRef = nodeRefs.get(0);
    assertNotNull("company home is null", companyHomeNodeRef);
    String companyHomeDBID = ((Long) nodeService.getProperty(companyHomeNodeRef, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    String testUserDBID = ((Long) nodeService.getProperty(person, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    NodeRef testUserHomeFolder = (NodeRef) nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER);
    assertNotNull("testUserHomeFolder is null", testUserHomeFolder);
    String testUserHomeDBID = ((Long) nodeService.getProperty(testUserHomeFolder, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
    // Clean up old messages in test folder
    List<ChildAssociationRef> assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    for (ChildAssociationRef assoc : assocs) {
        nodeService.deleteNode(assoc.getChildRef());
    }
    /**
     * Send From the test user TEST_EMAIL to the test user's home
     */
    String from = TEST_EMAIL;
    String to = testUserHomeDBID;
    String content = "hello world";
    Session sess = Session.getDefaultInstance(new Properties());
    assertNotNull("sess is null", sess);
    SMTPMessage msg = new SMTPMessage(sess);
    InternetAddress[] toa = { new InternetAddress(to) };
    msg.setFrom(new InternetAddress(TEST_EMAIL));
    msg.setRecipients(Message.RecipientType.TO, toa);
    msg.setSubject(TEST_SUBJECT);
    msg.setContent(content, "text/plain");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    msg.writeTo(bos);
    InputStream is = IOUtils.toInputStream(bos.toString());
    assertNotNull("is is null", is);
    SubethaEmailMessage m = new SubethaEmailMessage(is);
    /**
     * Turn on overwriteDuplicates
     */
    logger.debug("Step 1: turn on Overwite Duplicates");
    folderEmailMessageHandler.setOverwriteDuplicates(true);
    EmailDelivery delivery = new EmailDelivery(to, from, null);
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 1", 1, assocs.size());
    assertEquals("name of link not as expected", assocs.get(0).getQName(), QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_SUBJECT));
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 1", 1, assocs.size());
    /**
     * Turn off overwrite Duplicates
     */
    logger.debug("Step 2: turn off Overwite Duplicates");
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 2", 2, assocs.size());
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 3", 3, assocs.size());
    /**
     * Check assoc rename with long names.   So truncation and rename need to work together.
     */
    logger.debug("Step 3: turn off Overwite Duplicates with long subject name");
    msg.setSubject(TEST_LONG_SUBJECT);
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    msg.writeTo(bos2);
    is = IOUtils.toInputStream(bos2.toString());
    assertNotNull("is is null", is);
    m = new SubethaEmailMessage(is);
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 4", 4, assocs.size());
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    assertEquals("assocs not 5", 5, assocs.size());
    /**
     * Check assoc rename with long names and an extension. So truncation and rename need to
     * work together and not muck up a .extension.
     */
    logger.debug("Step 4: turn off Overwite Duplicates with long subject name with extension");
    String EXT_NAME = "Blob.xls";
    msg.setSubject(EXT_NAME);
    ByteArrayOutputStream bos3 = new ByteArrayOutputStream();
    msg.writeTo(bos3);
    is = IOUtils.toInputStream(bos3.toString());
    assertNotNull("is is null", is);
    m = new SubethaEmailMessage(is);
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    emailService.importMessage(delivery, m);
    emailService.importMessage(delivery, m);
    emailService.importMessage(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    List<QName> assocNames = new Vector<QName>();
    for (ChildAssociationRef assoc : assocs) {
        logger.debug("assocName: " + assoc.getQName());
        System.out.println(assoc.getQName());
        assocNames.add(assoc.getQName());
    }
    assertTrue(EXT_NAME + "not found", assocNames.contains(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "Blob.xls")));
    assertTrue("Blob(1).xls not found", assocNames.contains(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "Blob(1).xls")));
    assertTrue("Blob(2).xls not found", assocNames.contains(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "Blob(2).xls")));
    assertTrue(TEST_SUBJECT + "not found", assocNames.contains(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_SUBJECT)));
    assertTrue(TEST_SUBJECT + "(1) not found", assocNames.contains(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "Practical Bee Keeping(1)")));
    /**
     * Check concurrent deliver of the same message. Reuse message from the previous test.
     */
    logger.debug("Step 5: turn off Overwite Duplicates and check concurrent deliver of the same message");
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    int numBeforeConcurrentDeliver = assocs.size();
    deliverConcurrently(delivery, m);
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
    int numAfterConcurrentDeliver = assocs.size();
    assertEquals("Two messages must be added", numBeforeConcurrentDeliver + 2, numAfterConcurrentDeliver);
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) SubethaEmailMessage(org.alfresco.email.server.impl.subetha.SubethaEmailMessage) Serializable(java.io.Serializable) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Properties(java.util.Properties) NodeRef(org.alfresco.service.cmr.repository.NodeRef) EmailDelivery(org.alfresco.service.cmr.email.EmailDelivery) Vector(java.util.Vector) Session(javax.mail.Session)

Aggregations

SMTPMessage (com.sun.mail.smtp.SMTPMessage)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 InputStream (java.io.InputStream)7 Serializable (java.io.Serializable)7 HashMap (java.util.HashMap)7 Properties (java.util.Properties)7 Session (javax.mail.Session)7 InternetAddress (javax.mail.internet.InternetAddress)7 SubethaEmailMessage (org.alfresco.email.server.impl.subetha.SubethaEmailMessage)7 EmailDelivery (org.alfresco.service.cmr.email.EmailDelivery)7 NodeRef (org.alfresco.service.cmr.repository.NodeRef)7 StoreRef (org.alfresco.service.cmr.repository.StoreRef)7 QName (org.alfresco.service.namespace.QName)7 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)4 EmailMessageException (org.alfresco.service.cmr.email.EmailMessageException)2 Vector (java.util.Vector)1