001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.message;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.util.Date;
021    
022    import org.apache.james.mime4j.MimeException;
023    import org.apache.james.mime4j.MimeIOException;
024    import org.apache.james.mime4j.field.AbstractField;
025    import org.apache.james.mime4j.field.AddressListField;
026    import org.apache.james.mime4j.field.DateTimeField;
027    import org.apache.james.mime4j.field.FieldName;
028    import org.apache.james.mime4j.field.MailboxListField;
029    import org.apache.james.mime4j.field.UnstructuredField;
030    import org.apache.james.mime4j.field.address.AddressList;
031    import org.apache.james.mime4j.field.address.Mailbox;
032    import org.apache.james.mime4j.field.address.MailboxList;
033    import org.apache.james.mime4j.io.MaxHeaderLimitException;
034    import org.apache.james.mime4j.message.Header;
035    import org.apache.james.mime4j.parser.AbstractContentHandler;
036    import org.apache.james.mime4j.parser.Field;
037    import org.apache.james.mime4j.parser.MimeEntityConfig;
038    import org.apache.james.mime4j.parser.MimeStreamParser;
039    import org.springframework.util.CollectionUtils;
040    
041    /**
042     * 
043     * @author Won Chul Doh
044     * @since Mar 8, 2010
045     *
046     */
047    public class MessageHeader {
048            private Header header =  new Header();
049    
050            public MessageHeader(InputStream is) throws MimeIOException, IOException {
051                    final MimeStreamParser parser = createMimeParser();
052                    parser.setContentHandler(new AbstractContentHandler() {
053                            @Override
054                            public void endHeader() {
055                                    parser.stop();
056                            }
057                            @Override
058                            public void field(Field field) throws MimeException {
059                                    Field parsedField = AbstractField.parse(field.getRaw());
060                                    header.addField(parsedField);
061                            }
062                    });
063            try {
064                    parser.parse(is);
065            } catch (MaxHeaderLimitException ex) {
066                    // Ignore this exception
067            } catch (MimeException ex) {
068                    throw new MimeIOException(ex);
069            }
070            }
071            
072            private static MimeStreamParser createMimeParser() {
073                    MimeEntityConfig config = new MimeEntityConfig();
074                    config.setMaxLineLen(-1);
075                    return new MimeStreamParser(config);
076            }
077            
078        public Header getHeader() {
079                    return header;
080            }
081    
082        public String getSubject() {
083                    UnstructuredField field = obtainField(FieldName.SUBJECT);
084                    if (field == null)
085                            return null;
086    
087                    return field.getValue();
088            }
089    
090        public Date getDate() {
091                    DateTimeField dateField = obtainField(FieldName.DATE);
092                    if (dateField == null)
093                            return null;
094    
095                    return dateField.getDate();
096            }
097    
098            public Mailbox getFrom() {
099                    MailboxList mailboxList = getMailboxList(FieldName.FROM);
100                    if (CollectionUtils.isEmpty(mailboxList)) {
101                            Field field = header.getField(FieldName.FROM);
102                            return new Mailbox(field.getBody(), null);
103                    } else {
104                            return mailboxList.get(0);
105                    }
106            }
107            
108            public Mailbox getReplyTo() {
109                    MailboxList mailboxList = getMailboxList(FieldName.REPLY_TO);
110                    if (CollectionUtils.isEmpty(mailboxList)) {
111                            return null;
112                    } else {
113                            return mailboxList.get(0);
114                    }
115            }
116    
117        public AddressList getTo() {
118                    return getAddressList(FieldName.TO);
119            }
120    
121        public AddressList getCc() {
122            return getAddressList(FieldName.CC);
123        }
124    
125        public AddressList getBcc() {
126            return getAddressList(FieldName.BCC);
127        }
128    
129        private MailboxList getMailboxList(String fieldName) {
130            MailboxListField field = obtainField(fieldName);
131            if (field == null)
132                return null;
133    
134            return field.getMailboxList();
135        }
136    
137        private AddressList getAddressList(String fieldName) {
138                    AddressListField field = obtainField(fieldName);
139                    if (field == null)
140                            return null;
141    
142                    return field.getAddressList();
143            }
144        
145            <F extends Field> F obtainField(String fieldName) {
146                    if (header == null)
147                            return null;
148    
149                    @SuppressWarnings("unchecked")
150                    F field = (F) header.getField(fieldName);
151                    return field;
152            }
153            
154    }