Search in sources :

Example 1 with MailboxList

use of org.apache.james.mime4j.dom.address.MailboxList in project tika by apache.

the class MailContentHandler method field.

/**
     * Header for the whole message or its parts
     *
     * @see <a href="http://james.apache.org/mime4j/apidocs/org/apache/james/mime4j/parser/">
     *     http://james.apache.org/mime4j/apidocs/org/apache/james/mime4j/parser/</a>
     * Field.html
     */
public void field(Field field) throws MimeException {
    // whole message or its parts
    if (inPart) {
        return;
    }
    try {
        String fieldname = field.getName();
        ParsedField parsedField = LenientFieldParser.getParser().parse(field, DecodeMonitor.SILENT);
        if (fieldname.equalsIgnoreCase("From")) {
            MailboxListField fromField = (MailboxListField) parsedField;
            MailboxList mailboxList = fromField.getMailboxList();
            if (fromField.isValidField() && mailboxList != null) {
                for (Address address : mailboxList) {
                    String from = getDisplayString(address);
                    MailUtil.setPersonAndEmail(from, Message.MESSAGE_FROM_NAME, Message.MESSAGE_FROM_EMAIL, metadata);
                    metadata.add(Metadata.MESSAGE_FROM, from);
                    metadata.add(TikaCoreProperties.CREATOR, from);
                }
            } else {
                String from = stripOutFieldPrefix(field, "From:");
                MailUtil.setPersonAndEmail(from, Message.MESSAGE_FROM_NAME, Message.MESSAGE_FROM_EMAIL, metadata);
                if (from.startsWith("<")) {
                    from = from.substring(1);
                }
                if (from.endsWith(">")) {
                    from = from.substring(0, from.length() - 1);
                }
                metadata.add(Metadata.MESSAGE_FROM, from);
                metadata.add(TikaCoreProperties.CREATOR, from);
            }
        } else if (fieldname.equalsIgnoreCase("Subject")) {
            metadata.set(TikaCoreProperties.TRANSITION_SUBJECT_TO_DC_TITLE, ((UnstructuredField) parsedField).getValue());
        } else if (fieldname.equalsIgnoreCase("To")) {
            processAddressList(parsedField, "To:", Metadata.MESSAGE_TO);
        } else if (fieldname.equalsIgnoreCase("CC")) {
            processAddressList(parsedField, "Cc:", Metadata.MESSAGE_CC);
        } else if (fieldname.equalsIgnoreCase("BCC")) {
            processAddressList(parsedField, "Bcc:", Metadata.MESSAGE_BCC);
        } else if (fieldname.equalsIgnoreCase("Date")) {
            DateTimeField dateField = (DateTimeField) parsedField;
            Date date = dateField.getDate();
            if (date == null) {
                date = tryOtherDateFormats(field.getBody());
            }
            metadata.set(TikaCoreProperties.CREATED, date);
        } else {
            metadata.add(Metadata.MESSAGE_RAW_HEADER_PREFIX + parsedField.getName(), field.getBody());
        }
    } catch (RuntimeException me) {
        if (strictParsing) {
            throw me;
        }
    }
}
Also used : Address(org.apache.james.mime4j.dom.address.Address) ParsedField(org.apache.james.mime4j.dom.field.ParsedField) MailboxListField(org.apache.james.mime4j.dom.field.MailboxListField) UnstructuredField(org.apache.james.mime4j.dom.field.UnstructuredField) MailboxList(org.apache.james.mime4j.dom.address.MailboxList) DateTimeField(org.apache.james.mime4j.dom.field.DateTimeField) Date(java.util.Date)

Example 2 with MailboxList

use of org.apache.james.mime4j.dom.address.MailboxList in project k-9 by k9mail.

the class Address method parse.

/**
     * Parse a comma separated list of addresses in RFC-822 format and return an
     * array of Address objects.
     *
     * @param addressList
     * @return An array of 0 or more Addresses.
     */
public static Address[] parse(String addressList) {
    if (TextUtils.isEmpty(addressList)) {
        return EMPTY_ADDRESS_ARRAY;
    }
    List<Address> addresses = new ArrayList<Address>();
    try {
        MailboxList parsedList = AddressBuilder.DEFAULT.parseAddressList(addressList).flatten();
        for (int i = 0, count = parsedList.size(); i < count; i++) {
            org.apache.james.mime4j.dom.address.Address address = parsedList.get(i);
            if (address instanceof Mailbox) {
                Mailbox mailbox = (Mailbox) address;
                addresses.add(new Address(mailbox.getLocalPart() + "@" + mailbox.getDomain(), mailbox.getName(), false));
            } else {
                Log.e(LOG_TAG, "Unknown address type from Mime4J: " + address.getClass().toString());
            }
        }
    } catch (MimeException pe) {
        Log.e(LOG_TAG, "MimeException in Address.parse()", pe);
        //but we do an silent failover : we just use the given string as name with empty address
        addresses.add(new Address(null, addressList, false));
    }
    return addresses.toArray(EMPTY_ADDRESS_ARRAY);
}
Also used : Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ArrayList(java.util.ArrayList) MimeException(org.apache.james.mime4j.MimeException) MailboxList(org.apache.james.mime4j.dom.address.MailboxList)

Aggregations

MailboxList (org.apache.james.mime4j.dom.address.MailboxList)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 MimeException (org.apache.james.mime4j.MimeException)1 Address (org.apache.james.mime4j.dom.address.Address)1 Mailbox (org.apache.james.mime4j.dom.address.Mailbox)1 DateTimeField (org.apache.james.mime4j.dom.field.DateTimeField)1 MailboxListField (org.apache.james.mime4j.dom.field.MailboxListField)1 ParsedField (org.apache.james.mime4j.dom.field.ParsedField)1 UnstructuredField (org.apache.james.mime4j.dom.field.UnstructuredField)1