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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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
****************************************************************/
20
package org.apache.james.mime4j.samples.transform;
22
import java.io.ByteArrayInputStream;
23
import java.io.IOException;
24
import java.util.Date;
25
import java.util.Random;
27
import org.apache.james.mime4j.field.address.Mailbox;
28
import org.apache.james.mime4j.message.Body;
29
import org.apache.james.mime4j.message.BodyFactory;
30
import org.apache.james.mime4j.message.BodyPart;
31
import org.apache.james.mime4j.message.Message;
32
import org.apache.james.mime4j.message.Multipart;
33
import org.apache.james.mime4j.message.TextBody;
34
import org.apache.james.mime4j.storage.DefaultStorageProvider;
35
import org.apache.james.mime4j.storage.StorageProvider;
36
import org.apache.james.mime4j.storage.TempFileStorageProvider;
39
* This code should illustrate how to transform a message into another message
40
* without modifying the original.
42
public class TransformMessage {
44
// Host name used in message identifiers.
45
private static final String HOSTNAME = "localhost";
47
public static void main(String[] args) throws Exception {
48
// Explicitly set a strategy for storing body parts. Usually not
49
// necessary; for most applications the default setting is appropriate.
50
StorageProvider storageProvider = new TempFileStorageProvider();
51
DefaultStorageProvider.setInstance(storageProvider);
53
// Create a template message. It would be possible to load a message
54
// from an input stream but for this example a message object is created
55
// from scratch for demonstration purposes.
56
Message template = createTemplate();
58
// Create a new message by transforming the template.
59
Message transformed = transform(template);
61
// Print transformed message.
62
System.out.println("\n\nTransformed message:\n--------------------\n");
63
transformed.writeTo(System.out);
65
// Messages should be disposed of when they are no longer needed.
66
// Disposing of a message also disposes of all child elements (e.g. body
67
// parts) of the message.
68
transformed.dispose();
70
// Print original message to illustrate that it was not affected by the
72
System.out.println("\n\nOriginal template:\n------------------\n");
73
template.writeTo(System.out);
75
// Original message is no longer needed.
78
// At this point all temporary files have been deleted because all
79
// messages and body parts have been disposed of properly.
83
* Copies the given message and makes some arbitrary changes to the copy.
85
private static Message transform(Message original) throws IOException {
86
// Create a copy of the template. The copy can be modified without
87
// affecting the original.
88
Message message = new Message(original);
90
// In this example we know we have a multipart message. Use
91
// Message#isMultipart() if uncertain.
92
Multipart multipart = (Multipart) message.getBody();
94
// Insert a new text/plain body part after every body part of the
96
final int count = multipart.getCount();
97
for (int i = 0; i < count; i++) {
98
String text = "Text inserted after part " + (i + 1);
99
BodyPart bodyPart = createTextPart(text);
100
multipart.addBodyPart(bodyPart, 2 * i + 1);
103
// For no particular reason remove the second binary body part (now
105
BodyPart removed = multipart.removeBodyPart(4);
107
// The removed body part no longer has a parent entity it belongs to so
108
// it should be disposed of.
111
// Set some headers on the transformed message
112
message.createMessageId(HOSTNAME);
113
message.setSubject("Transformed message");
114
message.setDate(new Date());
115
message.setFrom(Mailbox.parse("John Doe <jdoe@machine.example>"));
121
* Creates a multipart/mixed message that consists of three parts (one text,
124
private static Message createTemplate() throws IOException {
125
Multipart multipart = new Multipart("mixed");
127
BodyPart part1 = createTextPart("This is the first part of the template..");
128
multipart.addBodyPart(part1);
130
BodyPart part2 = createRandomBinaryPart(200);
131
multipart.addBodyPart(part2);
133
BodyPart part3 = createRandomBinaryPart(300);
134
multipart.addBodyPart(part3);
136
Message message = new Message();
137
message.setMultipart(multipart);
139
message.setSubject("Template message");
145
* Creates a text part from the specified string.
147
private static BodyPart createTextPart(String text) {
148
TextBody body = new BodyFactory().textBody(text, "UTF-8");
150
BodyPart bodyPart = new BodyPart();
151
bodyPart.setText(body);
152
bodyPart.setContentTransferEncoding("quoted-printable");
158
* Creates a binary part with random content.
160
private static BodyPart createRandomBinaryPart(int numberOfBytes)
162
byte[] data = new byte[numberOfBytes];
163
new Random().nextBytes(data);
165
Body body = new BodyFactory()
166
.binaryBody(new ByteArrayInputStream(data));
168
BodyPart bodyPart = new BodyPart();
169
bodyPart.setBody(body, "application/octet-stream");
170
bodyPart.setContentTransferEncoding("base64");