001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * 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, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.mail;
019    
020    import org.apache.camel.impl.DefaultMessage;
021    import org.apache.camel.util.CollectionHelper;
022    
023    import javax.mail.Header;
024    import javax.mail.Message;
025    import javax.mail.MessagingException;
026    import java.util.Enumeration;
027    import java.util.Map;
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            }
049            else {
050                return "MailMessage: " + getBody();
051            }
052        }
053    
054        @Override
055        public MailExchange getExchange() {
056            return (MailExchange) super.getExchange();
057        }
058    
059        /**
060         * Returns the underlying Mail message
061         *
062         * @return the underlying Mail message
063         */
064        public Message getMessage() {
065            return mailMessage;
066        }
067    
068        public void setMessage(Message mailMessage) {
069            this.mailMessage = mailMessage;
070        }
071    
072        public Object getHeader(String name) {
073            String[] answer = null;
074            if (mailMessage != null) {
075                try {
076                    answer = mailMessage.getHeader(name);
077                }
078                catch (MessagingException e) {
079                    throw new MessageHeaderAccessException(name, e);
080                }
081            }
082            if (answer == null) {
083                return super.getHeader(name);
084            }
085            if (answer.length == 1) {
086                return answer[0];
087            }
088            return answer;
089        }
090    
091        @Override
092        public MailMessage newInstance() {
093            return new MailMessage();
094        }
095    
096        @Override
097        protected Object createBody() {
098            if (mailMessage != null) {
099                return getExchange().getBinding().extractBodyFromMail(getExchange(), mailMessage);
100            }
101            return null;
102        }
103    
104        @Override
105        protected void populateInitialHeaders(Map<String, Object> map) {
106            if (mailMessage != null) {
107                Enumeration names;
108                try {
109                    names = mailMessage.getAllHeaders();
110                }
111                catch (MessagingException e) {
112                    throw new MessageHeaderNamesAccessException(e);
113                }
114                try {
115                    while (names.hasMoreElements()) {
116                        Header header = (Header) names.nextElement();
117                        String value = header.getValue();
118                        String name = header.getName();
119                        CollectionHelper.appendValue(map, name, value);
120                    }
121                }
122                catch (Throwable e) {
123                    throw new MessageHeaderNamesAccessException(e);
124                }
125            }
126        }
127    }