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.util.Map; 021 022 import javax.activation.DataHandler; 023 import javax.mail.Message; 024 import javax.mail.MessagingException; 025 import javax.mail.Multipart; 026 import javax.mail.Part; 027 028 import org.apache.camel.RuntimeCamelException; 029 import org.apache.camel.impl.DefaultMessage; 030 import org.apache.camel.util.CollectionHelper; 031 032 /** 033 * Represents a {@link org.apache.camel.Message} for working with Mail 034 * 035 * @version $Revision:520964 $ 036 */ 037 public class MailMessage extends DefaultMessage { 038 private Message mailMessage; 039 040 public MailMessage() { 041 } 042 043 public MailMessage(Message message) { 044 this.mailMessage = message; 045 } 046 047 @Override 048 public String toString() { 049 if (mailMessage != null) { 050 return "MailMessage: " + MailUtils.dumpMessage(mailMessage); 051 } else { 052 return "MailMessage: " + getBody(); 053 } 054 } 055 056 @Override 057 public MailExchange getExchange() { 058 return (MailExchange)super.getExchange(); 059 } 060 061 public MailMessage copy() { 062 MailMessage answer = (MailMessage)super.copy(); 063 answer.mailMessage = mailMessage; 064 return answer; 065 } 066 067 /** 068 * Returns the underlying Mail message 069 */ 070 public Message getMessage() { 071 return mailMessage; 072 } 073 074 public void setMessage(Message mailMessage) { 075 this.mailMessage = mailMessage; 076 } 077 078 @Override 079 public Object getHeader(String name) { 080 Object answer = super.getHeader(name); 081 082 // mimic case insensitive search of mail message getHeader 083 if (answer == null) { 084 answer = super.getHeader(name.toLowerCase()); 085 } 086 return answer; 087 } 088 089 @Override 090 public MailMessage newInstance() { 091 return new MailMessage(); 092 } 093 094 @Override 095 protected Object createBody() { 096 if (mailMessage != null) { 097 return getExchange().getBinding().extractBodyFromMail(getExchange(), mailMessage); 098 } 099 return null; 100 } 101 102 @Override 103 protected void populateInitialHeaders(Map<String, Object> map) { 104 if (mailMessage != null) { 105 try { 106 map.putAll(getExchange().getBinding().extractHeadersFromMail(mailMessage)); 107 } catch (MessagingException e) { 108 throw new RuntimeCamelException("Error accessing headers due to: " + e.getMessage(), e); 109 } 110 } 111 } 112 113 @Override 114 protected void populateInitialAttachments(Map<String, DataHandler> map) { 115 if (mailMessage != null) { 116 try { 117 extractAttachments(mailMessage, map); 118 } catch (Exception e) { 119 throw new RuntimeCamelException("Error populating the initial mail message attachments", e); 120 } 121 } 122 } 123 124 public void copyFrom(org.apache.camel.Message that) { 125 super.copyFrom(that); 126 if (that instanceof MailMessage) { 127 MailMessage mailMessage = (MailMessage) that; 128 this.mailMessage = mailMessage.mailMessage; 129 } 130 } 131 132 /** 133 * Parses the attachments of the given mail message and adds them to the map 134 * 135 * @param message the mail message with attachments 136 * @param map the map to add found attachments (attachmentFilename is the key) 137 */ 138 protected static void extractAttachments(Message message, Map<String, DataHandler> map) 139 throws javax.mail.MessagingException, IOException { 140 141 Object content = message.getContent(); 142 if (content instanceof Multipart) { 143 extractFromMultipart((Multipart)content, map); 144 } 145 } 146 147 protected static void extractFromMultipart(Multipart mp, Map<String, DataHandler> map) 148 throws javax.mail.MessagingException, IOException { 149 150 for (int i = 0; i < mp.getCount(); i++) { 151 Part part = mp.getBodyPart(i); 152 if (part.isMimeType("multipart/*")) { 153 extractFromMultipart((Multipart)part.getContent(), map); 154 } else { 155 String disposition = part.getDisposition(); 156 if (disposition != null) { 157 if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) { 158 // only add named attachments 159 if (part.getFileName() != null) { 160 // Parts marked with a disposition of Part.ATTACHMENT 161 // are clearly attachments 162 CollectionHelper.appendValue(map, part.getFileName(), part.getDataHandler()); 163 } 164 } 165 } 166 } 167 } 168 } 169 170 }