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.io.IOException;
020    import java.io.InputStream;
021    
022    import javax.mail.BodyPart;
023    import javax.mail.Message;
024    import javax.mail.MessagingException;
025    import javax.mail.Multipart;
026    import javax.mail.internet.MimeMultipart;
027    
028    import org.apache.camel.Converter;
029    import org.apache.camel.converter.IOConverter;
030    
031    /**
032     * JavaMail specific converters.
033     *
034     * @version $Revision: 735893 $
035     */
036    @Converter
037    public final class MailConverters {
038        
039        private MailConverters() {
040            //Utility Class
041        }
042    
043        /**
044         * Converts the given JavaMail message to a String body.
045         * Can return null.
046         */
047        @Converter
048        public static String toString(Message message) throws MessagingException, IOException {
049            Object content = message.getContent();
050            if (content instanceof MimeMultipart) {
051                MimeMultipart multipart = (MimeMultipart) content;
052                if (multipart.getCount() > 0) {
053                    BodyPart part = multipart.getBodyPart(0);
054                    content = part.getContent();
055                }
056            }
057            if (content != null) {
058                return content.toString();
059            }
060            return null;
061        }
062    
063        /**
064         * Converts the given JavaMail multipart to a String body, where the contenttype of the multipart
065         * must be text based (ie start with text). Can return null.
066         */
067        @Converter
068        public static String toString(Multipart multipart) throws MessagingException, IOException {
069            int size = multipart.getCount();
070            for (int i = 0; i < size; i++) {
071                BodyPart part = multipart.getBodyPart(i);
072                if (part.getContentType().startsWith("text")) {
073                    return part.getContent().toString();
074                }
075            }
076            return null;
077        }
078    
079        /**
080         * Converts the given JavaMail message to an InputStream.
081         * Can return null.
082         */
083        @Converter
084        public static InputStream toInputStream(Message message) throws IOException, MessagingException {
085            String s = toString(message);
086            if (s == null) {
087                return null;
088            }
089            return IOConverter.toInputStream(s, null);
090        }
091    
092        /**
093         * Converts the given JavaMail multipart to a InputStream body, where the contenttype of the multipart
094         * must be text based (ie start with text). Can return null.
095         */
096        @Converter
097        public static InputStream toInputStream(Multipart multipart) throws IOException, MessagingException {
098            String s = toString(multipart);
099            if (s == null) {
100                return null;
101            }
102            return IOConverter.toInputStream(s, null);
103        }
104    
105    }