001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.message;
021    
022    import java.util.Collection;
023    import java.util.Date;
024    import java.util.Map;
025    import java.util.TimeZone;
026    
027    import org.apache.james.mime4j.codec.DecodeMonitor;
028    import org.apache.james.mime4j.dom.Header;
029    import org.apache.james.mime4j.dom.Message;
030    import org.apache.james.mime4j.dom.address.Address;
031    import org.apache.james.mime4j.dom.address.Mailbox;
032    import org.apache.james.mime4j.dom.field.AddressListField;
033    import org.apache.james.mime4j.dom.field.ContentDispositionField;
034    import org.apache.james.mime4j.dom.field.ContentTransferEncodingField;
035    import org.apache.james.mime4j.dom.field.ContentTypeField;
036    import org.apache.james.mime4j.dom.field.DateTimeField;
037    import org.apache.james.mime4j.dom.field.FieldName;
038    import org.apache.james.mime4j.dom.field.MailboxField;
039    import org.apache.james.mime4j.dom.field.MailboxListField;
040    import org.apache.james.mime4j.dom.field.UnstructuredField;
041    import org.apache.james.mime4j.field.ContentTransferEncodingFieldImpl;
042    import org.apache.james.mime4j.field.ContentTypeFieldImpl;
043    import org.apache.james.mime4j.field.Fields;
044    import org.apache.james.mime4j.field.MimeVersionFieldLenientImpl;
045    import org.apache.james.mime4j.stream.RawField;
046    import org.apache.james.mime4j.util.MimeUtil;
047    
048    /**
049     * Default implementation of {@link Message}.
050     */
051    public class MessageImpl extends AbstractMessage {
052    
053        /**
054         * Creates a new empty <code>Message</code>.
055         */
056        public MessageImpl() {
057            super();
058            Header header = obtainHeader();
059            RawField rawField = new RawField(FieldName.MIME_VERSION, "1.0");
060            header.addField(MimeVersionFieldLenientImpl.PARSER.parse(rawField, DecodeMonitor.SILENT));
061        }
062    
063        @Override
064        protected String newUniqueBoundary() {
065            return MimeUtil.createUniqueBoundary();
066        }
067    
068        @Override
069        protected UnstructuredField newMessageId(String hostname) {
070            return Fields.messageId(hostname);
071        }
072    
073        @Override
074        protected DateTimeField newDate(Date date, TimeZone zone) {
075            return Fields.date(FieldName.DATE, date, zone);
076        }
077    
078        @Override
079        protected MailboxField newMailbox(String fieldName, Mailbox mailbox) {
080            return Fields.mailbox(fieldName, mailbox);
081        }
082    
083        @Override
084        protected MailboxListField newMailboxList(String fieldName,
085                Collection<Mailbox> mailboxes) {
086            return Fields.mailboxList(fieldName, mailboxes);
087        }
088    
089        @Override
090        protected AddressListField newAddressList(String fieldName,
091                Collection<? extends Address> addresses) {
092            return Fields.addressList(fieldName, addresses);
093        }
094    
095        @Override
096        protected UnstructuredField newSubject(String subject) {
097            return Fields.subject(subject);
098        }
099    
100        @Override
101        protected ContentDispositionField newContentDisposition(
102                String dispositionType, String filename, long size,
103                Date creationDate, Date modificationDate, Date readDate) {
104            return Fields.contentDisposition(dispositionType, filename, size,
105                    creationDate, modificationDate, readDate);
106        }
107    
108        @Override
109        protected ContentDispositionField newContentDisposition(
110                String dispositionType, Map<String, String> parameters) {
111            return Fields.contentDisposition(dispositionType, parameters);
112        }
113    
114        @Override
115        protected ContentTypeField newContentType(String mimeType,
116                Map<String, String> parameters) {
117            return Fields.contentType(mimeType, parameters);
118        }
119    
120        @Override
121        protected ContentTransferEncodingField newContentTransferEncoding(
122                String contentTransferEncoding) {
123            return Fields.contentTransferEncoding(contentTransferEncoding);
124        }
125    
126        @Override
127        protected String calcTransferEncoding(ContentTransferEncodingField f) {
128            return ContentTransferEncodingFieldImpl.getEncoding(f);
129        }
130    
131        @Override
132        protected String calcMimeType(ContentTypeField child, ContentTypeField parent) {
133            return ContentTypeFieldImpl.getMimeType(child, parent);
134        }
135    
136        @Override
137        protected String calcCharset(ContentTypeField contentType) {
138            return ContentTypeFieldImpl.getCharset(contentType);
139        }
140    
141    }