1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.mailet.base.GenericMailet;
23 import org.apache.mailet.Mail;
24 import org.apache.mailet.MailetException;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.MimeBodyPart;
28 import javax.mail.internet.MimeMessage;
29 import javax.mail.internet.MimeMultipart;
30
31 import java.io.BufferedInputStream;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Iterator;
36 import java.util.Map;
37
38 /**
39 * <p>
40 * This mailet takes an attachment stored in an attribute and attach it back to
41 * the message
42 * </p>
43 * <p>
44 * This may be used to place back attachment stripped by StripAttachment and
45 * stored in the attribute
46 * <code>org.apache.james.transport.mailets.StripAttachment.saved</code>
47 * </p>
48 * <p>
49 *
50 * <pre>
51 * <mailet match="All" class="RecoverAttachment" >
52 * <attribute>my.attribute.name</attribute>
53 * </mailet >
54 * </pre>
55 *
56 * </p>
57 */
58 public class RecoverAttachment extends GenericMailet {
59
60 public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
61
62 private String attributeName = null;
63
64 /**
65 * Checks if the mandatory parameters are present
66 *
67 * @throws MailetException
68 */
69 public void init() throws MailetException {
70 attributeName = getInitParameter(ATTRIBUTE_PARAMETER_NAME);
71
72 if (attributeName == null) {
73 throw new MailetException(ATTRIBUTE_PARAMETER_NAME
74 + " is a mandatory parameter");
75 }
76
77 log("RecoverAttachment is initialised with attribute [" + attributeName
78 + "]");
79 }
80
81 /**
82 * Service the mail: check for the attribute and attach the attachment to
83 * the mail.
84 *
85 * @param mail
86 * The mail to service
87 * @throws MailetException
88 * Thrown when an error situation is encountered.
89 */
90 public void service(Mail mail) throws MailetException {
91 Map attachments = (Map) mail.getAttribute(attributeName);
92 if (attachments != null) {
93
94 MimeMessage message = null;
95 try {
96 message = mail.getMessage();
97 } catch (MessagingException e) {
98 throw new MailetException(
99 "Could not retrieve message from Mail object", e);
100 }
101
102 Iterator i = attachments.values().iterator();
103 try {
104 while (i.hasNext()) {
105 byte[] bytes = (byte[]) i.next();
106 InputStream is = new BufferedInputStream(
107 new ByteArrayInputStream(bytes));
108 MimeBodyPart p = new MimeBodyPart(is);
109 if (!(message.isMimeType("multipart/*") && (message
110 .getContent() instanceof MimeMultipart))) {
111 Object content = message.getContent();
112 String contentType = message.getContentType();
113 MimeMultipart mimeMultipart = new MimeMultipart();
114 message.setContent(mimeMultipart);
115 // This saveChanges is required when the MimeMessage has
116 // been created from
117 // an InputStream, otherwise it is not saved correctly.
118 message.saveChanges();
119 mimeMultipart.setParent(message);
120 MimeBodyPart bodyPart = new MimeBodyPart();
121 mimeMultipart.addBodyPart(bodyPart);
122 bodyPart.setContent(content, contentType);
123 }
124 ((MimeMultipart) message.getContent()).addBodyPart(p);
125 }
126 message.saveChanges();
127 } catch (MessagingException e) {
128 log("MessagingException in recoverAttachment", e);
129 } catch (IOException e) {
130 log("IOException in recoverAttachment", e);
131 }
132 }
133 }
134
135 /**
136 * returns a String describing this mailet.
137 *
138 * @return A desciption of this mailet
139 */
140 public String getMailetInfo() {
141 return "RecoverAttachment Mailet";
142 }
143
144 }