001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.mail;
018    
019    import java.util.Enumeration;
020    import java.util.Map;
021    
022    import javax.mail.Header;
023    import javax.mail.Message;
024    import javax.mail.MessagingException;
025    
026    import org.apache.camel.impl.DefaultMessage;
027    import org.apache.camel.util.CollectionHelper;
028    
029    /**
030     * Represents a {@link org.apache.camel.Message} for working with Mail
031     * 
032     * @version $Revision:520964 $
033     */
034    public class MailMessage extends DefaultMessage {
035        private Message mailMessage;
036    
037        public MailMessage() {
038        }
039    
040        public MailMessage(Message message) {
041            this.mailMessage = message;
042        }
043    
044        @Override
045        public String toString() {
046            if (mailMessage != null) {
047                return "MailMessage: " + mailMessage;
048            } else {
049                return "MailMessage: " + getBody();
050            }
051        }
052    
053        @Override
054        public MailExchange getExchange() {
055            return (MailExchange)super.getExchange();
056        }
057    
058        public MailMessage copy() {
059            MailMessage answer = (MailMessage)super.copy();
060            answer.mailMessage = mailMessage;
061            return answer;
062        }
063    
064        /**
065         * Returns the underlying Mail message
066         * 
067         * @return the underlying Mail message
068         */
069        public Message getMessage() {
070            return mailMessage;
071        }
072    
073        public void setMessage(Message mailMessage) {
074            this.mailMessage = mailMessage;
075        }
076    
077        public Object getHeader(String name) {
078            String[] answer = null;
079            if (mailMessage != null) {
080                try {
081                    answer = mailMessage.getHeader(name);
082                } catch (MessagingException e) {
083                    throw new MessageHeaderAccessException(name, e);
084                }
085            }
086            if (answer == null) {
087                return super.getHeader(name);
088            }
089            if (answer.length == 1) {
090                return answer[0];
091            }
092            return answer;
093        }
094    
095        @Override
096        public MailMessage newInstance() {
097            return new MailMessage();
098        }
099    
100        @Override
101        protected Object createBody() {
102            if (mailMessage != null) {
103                return getExchange().getBinding().extractBodyFromMail(getExchange(), mailMessage);
104            }
105            return null;
106        }
107    
108        @Override
109        protected void populateInitialHeaders(Map<String, Object> map) {
110            if (mailMessage != null) {
111                Enumeration names;
112                try {
113                    names = mailMessage.getAllHeaders();
114                } catch (MessagingException e) {
115                    throw new MessageHeaderNamesAccessException(e);
116                }
117                try {
118                    while (names.hasMoreElements()) {
119                        Header header = (Header)names.nextElement();
120                        String value = header.getValue();
121                        String name = header.getName();
122                        CollectionHelper.appendValue(map, name, value);
123                    }
124                } catch (Throwable e) {
125                    throw new MessageHeaderNamesAccessException(e);
126                }
127            }
128        }
129    
130        public void copyFrom(org.apache.camel.Message that) {
131            super.copyFrom(that);
132            if (that instanceof MailMessage) {
133                MailMessage mailMessage = (MailMessage) that;
134                this.mailMessage = mailMessage.mailMessage;
135            }
136        }
137    }