Search in sources :

Example 1 with EmailMessageException

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

the class EmailServiceImplTest method testEmailContributorsAuthority.

// end of test sending to trx:transferReport
/**
 * The Email contributors authority controls who can add email.
 *
 * This test switches between the EMAIL_CONTRIBUTORS group and EVERYONE
 */
public void testEmailContributorsAuthority() throws Exception {
    EmailServiceImpl emailServiceImpl = (EmailServiceImpl) emailService;
    folderEmailMessageHandler.setOverwriteDuplicates(true);
    logger.debug("Start testEmailContributorsAuthority");
    String TEST_EMAIL = "buffy@sunnydale.high";
    // 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.removeAuthority("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";
    /**
     * Step 1
     * Set the email contributors authority to EVERYONE
     *
     * Test that TEST_USER is allowed to send email - so even though TEST_USER is not
     * a contributor
     */
    emailServiceImpl.setEmailContributorsAuthority("EVERYONE");
    String from = "admin";
    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("JavaMail APIs transport.java Test");
    msg.setContent(content, "text/plain");
    StringBuffer sb = new StringBuffer();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    msg.writeTo(bos);
    InputStream is = IOUtils.toInputStream(bos.toString());
    assertNotNull("is is null", is);
    SubethaEmailMessage m = new SubethaEmailMessage(is);
    EmailDelivery delivery = new EmailDelivery(to, from, null);
    emailService.importMessage(delivery, m);
    /**
     * Step 2
     * Negative test
     *
     * Send From the test user TEST_EMAIL to the test user's home
     */
    try {
        logger.debug("Step 2");
        emailServiceImpl.setEmailContributorsAuthority("EMAIL_CONTRIBUTORS");
        emailService.importMessage(delivery, m);
        fail("not thrown out");
    } catch (EmailMessageException e) {
    // Check the exception is for the anonymous user.
    // assertTrue(e.getMessage().contains("anonymous"));
    }
}
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) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) NodeRef(org.alfresco.service.cmr.repository.NodeRef) EmailDelivery(org.alfresco.service.cmr.email.EmailDelivery) Session(javax.mail.Session)

Example 2 with EmailMessageException

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

the class EmailServiceImplTest method testFromName.

/**
 * Test the from name.
 *
 * Step 1:
 * User admin will map to the "unknownUser"  which out of the box is "anonymous"
 * Sending email From "admin" will fail.
 *
 * Step 2:
 * Send from the test user to the test' user's home folder.
 */
public void testFromName() throws Exception {
    folderEmailMessageHandler.setOverwriteDuplicates(true);
    logger.debug("Start testFromName");
    String TEST_EMAIL = "buffy@sunnydale.high";
    // 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";
    /**
     * Step 1
     * Negative test - send from "Bert" who does not exist.
     * User will be mapped to anonymous who is not an email contributor.
     */
    try {
        String from = "admin";
        String to = "bertie";
        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("Bert"));
        msg.setRecipients(Message.RecipientType.TO, toa);
        msg.setSubject("JavaMail APIs transport.java Test");
        msg.setContent(content, "text/plain");
        StringBuffer sb = new StringBuffer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        msg.writeTo(bos);
        InputStream is = IOUtils.toInputStream(bos.toString());
        assertNotNull("is is null", is);
        SubethaEmailMessage m = new SubethaEmailMessage(is);
        EmailDelivery delivery = new EmailDelivery(to, from, null);
        emailService.importMessage(delivery, m);
        fail("anonymous user not rejected");
    } catch (EmailMessageException e) {
        // Check the exception is for the anonymous user.
        assertTrue("Message is not for anonymous", e.getMessage().contains("anonymous"));
    }
    /**
     * Step 2
     *
     * Send From the test user TEST_EMAIL to the test user's home
     */
    {
        logger.debug("Step 2");
        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("JavaMail APIs transport.java Test");
        msg.setContent(content, "text/plain");
        StringBuffer sb = new StringBuffer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        msg.writeTo(bos);
        InputStream is = IOUtils.toInputStream(bos.toString());
        assertNotNull("is is null", is);
        SubethaEmailMessage m = new SubethaEmailMessage(is);
        EmailDelivery delivery = new EmailDelivery(to, from, null);
        emailService.importMessage(delivery, m);
    }
    /**
     * Step 3
     *
     * message.from From with "name" < name@ domain > format
     * SMTP.FROM="dummy"
     *
     * Send From the test user <TEST_EMAIL> to the test user's home
     */
    {
        logger.debug("Step 3");
        String from = " \"Joe Bloggs\" <" + 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(from));
        msg.setRecipients(Message.RecipientType.TO, toa);
        msg.setSubject("JavaMail APIs transport.java Test");
        msg.setContent(content, "text/plain");
        StringBuffer sb = new StringBuffer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        msg.writeTo(System.out);
        msg.writeTo(bos);
        InputStream is = IOUtils.toInputStream(bos.toString());
        assertNotNull("is is null", is);
        SubethaEmailMessage m = new SubethaEmailMessage(is);
        EmailDelivery delivery = new EmailDelivery(to, "dummy", null);
        emailService.importMessage(delivery, m);
    }
    /**
     * Step 4
     *
     * From with "name" < name@ domain > format
     *
     * Send From the test user <TEST_EMAIL> to the test user's home
     */
    {
        logger.debug("Step 4");
        String from = " \"Joe Bloggs\" <" + 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(from));
        msg.setRecipients(Message.RecipientType.TO, toa);
        msg.setSubject("JavaMail APIs transport.java Test");
        msg.setContent(content, "text/plain");
        StringBuffer sb = new StringBuffer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        msg.writeTo(System.out);
        msg.writeTo(bos);
        InputStream is = IOUtils.toInputStream(bos.toString());
        assertNotNull("is is null", is);
        SubethaEmailMessage m = new SubethaEmailMessage(is);
        InternetAddress a = new InternetAddress(from);
        String x = a.getAddress();
        EmailDelivery delivery = new EmailDelivery(to, x, null);
        emailService.importMessage(delivery, m);
    }
// /**
// * Step 5
// *
// * From with <e=name@domain> format
// *
// * RFC3696
// *
// * Send From the test user <TEST_EMAIL> to the test user's home
// */
// {
// logger.debug("Step 4 <local tag=name@ domain > format");
// 
// String from = "\"Joe Bloggs\" <e=" + 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(from));
// msg.setRecipients(Message.RecipientType.TO, toa);
// msg.setSubject("JavaMail APIs transport.java Test");
// msg.setContent(content, "text/plain");
// 
// StringBuffer sb = new StringBuffer();
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// msg.writeTo(System.out);
// msg.writeTo(bos);
// InputStream is = IOUtils.toInputStream(bos.toString());
// assertNotNull("is is null", is);
// 
// SubethaEmailMessage m = new SubethaEmailMessage(is);
// 
// emailService.importMessage(m);
// }
}
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) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) NodeRef(org.alfresco.service.cmr.repository.NodeRef) EmailDelivery(org.alfresco.service.cmr.email.EmailDelivery) Session(javax.mail.Session)

Example 3 with EmailMessageException

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

the class EmailServiceImpl method getMessageHandler.

/**
 * @param nodeRef           Target node
 * @return                  Handler that can process message addressed to specific node (target node).
 * @throws                  EmailMessageException is thrown if a suitable message handler isn't found.
 */
private EmailMessageHandler getMessageHandler(NodeRef nodeRef) {
    ParameterCheck.mandatory("nodeRef", nodeRef);
    QName nodeTypeQName = nodeService.getType(nodeRef);
    String prefixedNodeTypeStr = nodeTypeQName.toPrefixString(namespaceService);
    EmailMessageHandler handler = emailMessageHandlerMap.get(prefixedNodeTypeStr);
    if (handler == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("did not find a handler for type:" + prefixedNodeTypeStr);
        }
        // not a direct match on type
        // need to check the super-types (if any) of the target node
        TypeDefinition typeDef = dictionaryService.getType(nodeTypeQName);
        while (typeDef != null) {
            QName parentName = typeDef.getParentName();
            if (parentName != null) {
                String prefixedSubTypeStr = parentName.toPrefixString(namespaceService);
                handler = emailMessageHandlerMap.get(prefixedSubTypeStr);
                if (handler != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("found a handler for a subtype:" + prefixedSubTypeStr);
                    }
                    return handler;
                }
            }
            typeDef = dictionaryService.getType(parentName);
        }
    }
    if (handler == null) {
        throw new EmailMessageException(ERR_HANDLER_NOT_FOUND, prefixedNodeTypeStr);
    }
    return handler;
}
Also used : QName(org.alfresco.service.namespace.QName) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) EmailMessageHandler(org.alfresco.email.server.handler.EmailMessageHandler) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition)

Example 4 with EmailMessageException

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

the class SubethaEmailMessage method processMimeMessage.

private void processMimeMessage(MimeMessage mimeMessage) {
    if (from == null) {
        Address[] addresses = null;
        try {
            addresses = mimeMessage.getFrom();
        } catch (MessagingException e) {
            throw new EmailMessageException(ERR_EXTRACTING_FROM_ADDRESS, e.getMessage());
        }
        if (addresses == null || addresses.length == 0) {
        // throw new EmailMessageException(ERR_NO_FROM_ADDRESS);
        } else {
            if (addresses[0] instanceof InternetAddress) {
                from = ((InternetAddress) addresses[0]).getAddress();
            } else {
                from = addresses[0].toString();
            }
        }
    }
    if (to == null) {
        Address[] addresses = null;
        try {
            addresses = mimeMessage.getAllRecipients();
        } catch (MessagingException e) {
            throw new EmailMessageException(ERR_EXTRACTING_TO_ADDRESS, e.getMessage());
        }
        if (addresses == null || addresses.length == 0) {
        // throw new EmailMessageException(ERR_NO_TO_ADDRESS);
        } else {
            if (addresses[0] instanceof InternetAddress) {
                to = ((InternetAddress) addresses[0]).getAddress();
            } else {
                to = addresses[0].toString();
            }
        }
    }
    if (cc == null) {
        try {
            ArrayList<String> list = new ArrayList<String>();
            Address[] cca = mimeMessage.getRecipients(RecipientType.CC);
            if (cca != null) {
                for (Address a : cca) {
                    list.add(a.toString());
                }
            }
            cc = list;
        } catch (MessagingException e) {
            // Do nothing - this is not a show-stopper.
            cc = null;
        }
    }
    try {
        subject = mimeMessage.getSubject();
    // subject = encodeSubject(mimeMessage.getSubject());
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_EXTRACTING_SUBJECT, e.getMessage());
    }
    try {
        sentDate = mimeMessage.getSentDate();
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_EXTRACTING_SENT_DATE, e.getMessage());
    }
    if (sentDate == null) {
        // Just anti-null stub :)
        sentDate = new Date();
    }
    parseMessagePart(mimeMessage);
    attachments = new EmailMessagePart[attachmentList.size()];
    attachmentList.toArray(attachments);
    attachmentList = null;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) Date(java.util.Date)

Example 5 with EmailMessageException

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

the class SubethaEmailMessage method parseMessagePart.

private void parseMessagePart(Part messagePart) {
    try {
        if (messagePart.isMimeType(MIME_PLAIN_TEXT) || messagePart.isMimeType(MIME_HTML_TEXT)) {
            if (log.isDebugEnabled()) {
                log.debug("Text or HTML part was found. ContentType: " + messagePart.getContentType());
            }
            addBody(messagePart);
        } else if (messagePart.isMimeType(MIME_XML_TEXT)) {
            if (log.isDebugEnabled()) {
                log.debug("XML part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_APPLICATION)) {
            if (log.isDebugEnabled()) {
                log.debug("Application part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_IMAGE)) {
            if (log.isDebugEnabled()) {
                log.debug("Image part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_MULTIPART)) {
            // if multipart, this method will be called recursively
            // for each of its parts
            Multipart mp = (Multipart) messagePart.getContent();
            int count = mp.getCount();
            if (log.isDebugEnabled()) {
                log.debug("MULTIPART with " + count + " part(s) found. Processin each part...");
            }
            for (int i = 0; i < count; i++) {
                BodyPart bp = mp.getBodyPart(i);
                if (bp.getContent() instanceof MimeMultipart) {
                    // It's multipart.  Recurse.
                    parseMessagePart(bp);
                } else {
                    // It's the body
                    addBody(bp);
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("MULTIPART processed.");
            }
        } else if (messagePart.isMimeType(MIME_RFC822)) {
            // if rfc822, call this method with its content as the part
            if (log.isDebugEnabled()) {
                log.debug("MIME_RFC822 part found. Processing inside part...");
            }
            parseMessagePart((Part) messagePart.getContent());
            if (log.isDebugEnabled()) {
                log.debug("MIME_RFC822 processed.");
            }
        } else {
            // Actually we don't know what it is.
            if (log.isDebugEnabled()) {
                log.debug("Unrecognized part was found. Put it into attachments.");
            }
            addAttachment(messagePart);
        }
    } catch (IOException e) {
        throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage());
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage());
    }
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) Part(javax.mail.Part) BodyPart(javax.mail.BodyPart) EmailMessagePart(org.alfresco.service.cmr.email.EmailMessagePart) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) IOException(java.io.IOException)

Aggregations

EmailMessageException (org.alfresco.service.cmr.email.EmailMessageException)8 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 InternetAddress (javax.mail.internet.InternetAddress)3 StoreRef (org.alfresco.service.cmr.repository.StoreRef)3 QName (org.alfresco.service.namespace.QName)3 SMTPMessage (com.sun.mail.smtp.SMTPMessage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 MessagingException (javax.mail.MessagingException)2 Session (javax.mail.Session)2 EmailMessageHandler (org.alfresco.email.server.handler.EmailMessageHandler)2 SubethaEmailMessage (org.alfresco.email.server.impl.subetha.SubethaEmailMessage)2 EmailDelivery (org.alfresco.service.cmr.email.EmailDelivery)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Address (javax.mail.Address)1