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.Exchange;
021    import org.apache.camel.converter.ObjectConverter;
022    
023    import javax.mail.Address;
024    import javax.mail.Message;
025    import javax.mail.MessagingException;
026    import javax.mail.internet.InternetAddress;
027    import javax.mail.internet.MimeMessage;
028    import java.util.Iterator;
029    import java.util.Map;
030    import java.util.Set;
031    
032    /**
033     * A Strategy used to convert between a Camel {@Exchange} and {@Message} to and from a
034     * Mail {@link MimeMessage}
035     *
036     * @version $Revision: 521240 $
037     */
038    public class MailBinding {
039        public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange) {
040            try {
041                appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
042    
043                String destination = endpoint.getConfiguration().getDestination();
044                if (destination != null) {
045                    mimeMessage.setRecipients(Message.RecipientType.TO, destination);
046                }
047    
048                if (empty(mimeMessage.getFrom())) {
049                    // lets default the address to the endpoint destination
050                    String from = endpoint.getConfiguration().getFrom();
051                    mimeMessage.setFrom(new InternetAddress(from));
052                }
053                mimeMessage.setText(exchange.getIn().getBody(String.class));
054            }
055            catch (Exception e) {
056                throw new RuntimeMailException("Failed to populate body due to: " + e + ". Exchange: " + exchange, e);
057            }
058        }
059    
060        protected boolean empty(Address[] addresses) {
061            return addresses == null || addresses.length == 0;
062        }
063    
064        /**
065         * Extracts the body from the Mail message
066         *
067         * @param exchange
068         * @param message
069         */
070        public Object extractBodyFromMail(MailExchange exchange, Message message) {
071            try {
072                return message.getContent();
073            }
074            catch (Exception e) {
075                throw new RuntimeMailException("Failed to extract body due to: " + e + ". Message: " + message, e);
076            }
077        }
078    
079        /**
080         * Appends the Mail headers from the Camel {@link MailMessage}
081         */
082        protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange exchange, org.apache.camel.Message camelMessage) throws MessagingException {
083            Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
084            for (Map.Entry<String, Object> entry : entries) {
085                String headerName = entry.getKey();
086                Object headerValue = entry.getValue();
087                if (headerValue != null) {
088                    if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
089    
090                        // Mail messages can repeat the same header...
091                        if (ObjectConverter.isCollection(headerValue)) {
092                            Iterator iter = ObjectConverter.iterator(headerValue);
093                            while (iter.hasNext()) {
094                                Object value = iter.next();
095                                mimeMessage.addHeader(headerName, asString(exchange, value));
096                            }
097                        }
098                        else {
099                            mimeMessage.setHeader(headerName, asString(exchange, headerValue));
100                        }
101                    }
102                }
103            }
104        }
105    
106        /**
107         * Converts the given object value to a String
108         */
109        protected String asString(Exchange exchange, Object value) {
110            return exchange.getContext().getTypeConverter().convertTo(String.class, value);
111        }
112    
113        /**
114         * Strategy to allow filtering of headers which are put on the Mail message
115         */
116        protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) {
117            return true;
118        }
119    }