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.jdkim.mailets; |
21 | |
22 | import java.io.IOException; |
23 | |
24 | import javax.mail.MessagingException; |
25 | import javax.mail.internet.MimeMessage; |
26 | import javax.mail.internet.MimeMultipart; |
27 | import javax.mail.internet.MimePart; |
28 | |
29 | import org.apache.mailet.Mail; |
30 | import org.apache.mailet.base.GenericMailet; |
31 | |
32 | /** |
33 | * Make sure the message stream is 7bit. Every 8bit part is encoded to |
34 | * quoted-printable or base64 and the message is saved. |
35 | */ |
36 | public class ConvertTo7Bit extends GenericMailet { |
37 | |
38 | public void service(Mail mail) throws MessagingException { |
39 | MimeMessage message = mail.getMessage(); |
40 | try { |
41 | convertTo7Bit(message); |
42 | message.saveChanges(); |
43 | } catch (IOException e) { |
44 | throw new MessagingException( |
45 | "IOException converting message to 7bit: " + e.getMessage(), |
46 | e); |
47 | } |
48 | } |
49 | |
50 | /** |
51 | * Converts a message to 7 bit. |
52 | * |
53 | * @param part |
54 | */ |
55 | private void convertTo7Bit(MimePart part) throws MessagingException, |
56 | IOException { |
57 | if (part.isMimeType("multipart/*")) { |
58 | MimeMultipart parts = (MimeMultipart) part.getContent(); |
59 | int count = parts.getCount(); |
60 | for (int i = 0; i < count; i++) { |
61 | convertTo7Bit((MimePart) parts.getBodyPart(i)); |
62 | } |
63 | } else if ("8bit".equals(part.getEncoding())) { |
64 | // The content may already be in encoded the form (likely with mail |
65 | // created from a stream). In that case, just changing the encoding |
66 | // to quoted-printable will mangle the result when this is |
67 | // transmitted. |
68 | // We must first convert the content into its native format, set it |
69 | // back, and only THEN set the transfer encoding to force the |
70 | // content to be encoded appropriately. |
71 | |
72 | // if the part doesn't contain text it will be base64 encoded. |
73 | String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" |
74 | : "base64"; |
75 | part.setContent(part.getContent(), part.getContentType()); |
76 | part |
77 | .setHeader("Content-Transfer-Encoding", |
78 | contentTransferEncoding); |
79 | part.addHeader("X-MIME-Autoconverted", "from 8bit to " |
80 | + contentTransferEncoding + " by " |
81 | + getMailetContext().getServerInfo()); |
82 | } |
83 | } |
84 | |
85 | } |