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;
}
}
}
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);
}
Aggregations