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.mime4j.samples.transform; |
---|
21 | |
---|
22 | import java.io.ByteArrayInputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.util.Date; |
---|
25 | import java.util.Random; |
---|
26 | |
---|
27 | import org.apache.james.mime4j.dom.Body; |
---|
28 | import org.apache.james.mime4j.dom.Entity; |
---|
29 | import org.apache.james.mime4j.dom.Message; |
---|
30 | import org.apache.james.mime4j.dom.Multipart; |
---|
31 | import org.apache.james.mime4j.dom.TextBody; |
---|
32 | import org.apache.james.mime4j.dom.field.ParseException; |
---|
33 | import org.apache.james.mime4j.field.address.AddressBuilder; |
---|
34 | import org.apache.james.mime4j.message.BodyPart; |
---|
35 | import org.apache.james.mime4j.message.MessageImpl; |
---|
36 | import org.apache.james.mime4j.message.MimeBuilder; |
---|
37 | import org.apache.james.mime4j.message.MimeWriter; |
---|
38 | import org.apache.james.mime4j.message.MultipartImpl; |
---|
39 | import org.apache.james.mime4j.storage.DefaultStorageProvider; |
---|
40 | import org.apache.james.mime4j.storage.StorageBodyFactory; |
---|
41 | import org.apache.james.mime4j.storage.StorageProvider; |
---|
42 | import org.apache.james.mime4j.storage.TempFileStorageProvider; |
---|
43 | |
---|
44 | /** |
---|
45 | * This code should illustrate how to transform a message into another message |
---|
46 | * without modifying the original. |
---|
47 | */ |
---|
48 | public class TransformMessage { |
---|
49 | |
---|
50 | // Host name used in message identifiers. |
---|
51 | private static final String HOSTNAME = "localhost"; |
---|
52 | |
---|
53 | public static void main(String[] args) throws Exception { |
---|
54 | // Explicitly set a strategy for storing body parts. Usually not |
---|
55 | // necessary; for most applications the default setting is appropriate. |
---|
56 | StorageProvider storageProvider = new TempFileStorageProvider(); |
---|
57 | DefaultStorageProvider.setInstance(storageProvider); |
---|
58 | |
---|
59 | // Create a template message. It would be possible to load a message |
---|
60 | // from an input stream but for this example a message object is created |
---|
61 | // from scratch for demonstration purposes. |
---|
62 | Message template = createTemplate(); |
---|
63 | |
---|
64 | // Create a new message by transforming the template. |
---|
65 | Message transformed = transform(template); |
---|
66 | |
---|
67 | // Print transformed message. |
---|
68 | System.out.println("\n\nTransformed message:\n--------------------\n"); |
---|
69 | MimeWriter.DEFAULT.writeMessage(transformed, System.out); |
---|
70 | |
---|
71 | // Messages should be disposed of when they are no longer needed. |
---|
72 | // Disposing of a message also disposes of all child elements (e.g. body |
---|
73 | // parts) of the message. |
---|
74 | transformed.dispose(); |
---|
75 | |
---|
76 | // Print original message to illustrate that it was not affected by the |
---|
77 | // transformation. |
---|
78 | System.out.println("\n\nOriginal template:\n------------------\n"); |
---|
79 | MimeWriter.DEFAULT.writeMessage(template, System.out); |
---|
80 | |
---|
81 | // Original message is no longer needed. |
---|
82 | template.dispose(); |
---|
83 | |
---|
84 | // At this point all temporary files have been deleted because all |
---|
85 | // messages and body parts have been disposed of properly. |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Copies the given message and makes some arbitrary changes to the copy. |
---|
90 | * @throws ParseException on bad arguments |
---|
91 | */ |
---|
92 | private static Message transform(Message original) throws IOException, ParseException { |
---|
93 | // Create a copy of the template. The copy can be modified without |
---|
94 | // affecting the original. |
---|
95 | Message message = MimeBuilder.DEFAULT.copy(original); |
---|
96 | |
---|
97 | // In this example we know we have a multipart message. Use |
---|
98 | // Message#isMultipart() if uncertain. |
---|
99 | Multipart multipart = (Multipart) message.getBody(); |
---|
100 | |
---|
101 | // Insert a new text/plain body part after every body part of the |
---|
102 | // template. |
---|
103 | final int count = multipart.getCount(); |
---|
104 | for (int i = 0; i < count; i++) { |
---|
105 | String text = "Text inserted after part " + (i + 1); |
---|
106 | BodyPart bodyPart = createTextPart(text); |
---|
107 | multipart.addBodyPart(bodyPart, 2 * i + 1); |
---|
108 | } |
---|
109 | |
---|
110 | // For no particular reason remove the second binary body part (now |
---|
111 | // at index four). |
---|
112 | Entity removed = multipart.removeBodyPart(4); |
---|
113 | |
---|
114 | // The removed body part no longer has a parent entity it belongs to so |
---|
115 | // it should be disposed of. |
---|
116 | removed.dispose(); |
---|
117 | |
---|
118 | // Set some headers on the transformed message |
---|
119 | message.createMessageId(HOSTNAME); |
---|
120 | message.setSubject("Transformed message"); |
---|
121 | message.setDate(new Date()); |
---|
122 | message.setFrom(AddressBuilder.DEFAULT.parseMailbox("John Doe <jdoe@machine.example>")); |
---|
123 | |
---|
124 | return message; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Creates a multipart/mixed message that consists of three parts (one text, |
---|
129 | * two binary). |
---|
130 | */ |
---|
131 | private static Message createTemplate() throws IOException { |
---|
132 | Multipart multipart = new MultipartImpl("mixed"); |
---|
133 | |
---|
134 | BodyPart part1 = createTextPart("This is the first part of the template.."); |
---|
135 | multipart.addBodyPart(part1); |
---|
136 | |
---|
137 | BodyPart part2 = createRandomBinaryPart(200); |
---|
138 | multipart.addBodyPart(part2); |
---|
139 | |
---|
140 | BodyPart part3 = createRandomBinaryPart(300); |
---|
141 | multipart.addBodyPart(part3); |
---|
142 | |
---|
143 | MessageImpl message = new MessageImpl(); |
---|
144 | message.setMultipart(multipart); |
---|
145 | |
---|
146 | message.setSubject("Template message"); |
---|
147 | |
---|
148 | return message; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Creates a text part from the specified string. |
---|
153 | */ |
---|
154 | private static BodyPart createTextPart(String text) { |
---|
155 | TextBody body = new StorageBodyFactory().textBody(text, "UTF-8"); |
---|
156 | |
---|
157 | BodyPart bodyPart = new BodyPart(); |
---|
158 | bodyPart.setText(body); |
---|
159 | bodyPart.setContentTransferEncoding("quoted-printable"); |
---|
160 | |
---|
161 | return bodyPart; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Creates a binary part with random content. |
---|
166 | */ |
---|
167 | private static BodyPart createRandomBinaryPart(int numberOfBytes) |
---|
168 | throws IOException { |
---|
169 | byte[] data = new byte[numberOfBytes]; |
---|
170 | new Random().nextBytes(data); |
---|
171 | |
---|
172 | Body body = new StorageBodyFactory() |
---|
173 | .binaryBody(new ByteArrayInputStream(data)); |
---|
174 | |
---|
175 | BodyPart bodyPart = new BodyPart(); |
---|
176 | bodyPart.setBody(body, "application/octet-stream"); |
---|
177 | bodyPart.setContentTransferEncoding("base64"); |
---|
178 | |
---|
179 | return bodyPart; |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|