~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic-proposed

« back to all changes in this revision

Viewing changes to examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.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;
37
 
 
38
 
/**
39
 
 * This code should illustrate how to transform a message into another message
40
 
 * without modifying the original.
41
 
 */
42
 
public class TransformMessage {
43
 
 
44
 
    // Host name used in message identifiers.
45
 
    private static final String HOSTNAME = "localhost";
46
 
 
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);
52
 
 
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();
57
 
 
58
 
        // Create a new message by transforming the template.
59
 
        Message transformed = transform(template);
60
 
 
61
 
        // Print transformed message.
62
 
        System.out.println("\n\nTransformed message:\n--------------------\n");
63
 
        transformed.writeTo(System.out);
64
 
 
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();
69
 
 
70
 
        // Print original message to illustrate that it was not affected by the
71
 
        // transformation.
72
 
        System.out.println("\n\nOriginal template:\n------------------\n");
73
 
        template.writeTo(System.out);
74
 
 
75
 
        // Original message is no longer needed.
76
 
        template.dispose();
77
 
 
78
 
        // At this point all temporary files have been deleted because all
79
 
        // messages and body parts have been disposed of properly.
80
 
    }
81
 
 
82
 
    /**
83
 
     * Copies the given message and makes some arbitrary changes to the copy.
84
 
     */
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);
89
 
 
90
 
        // In this example we know we have a multipart message. Use
91
 
        // Message#isMultipart() if uncertain.
92
 
        Multipart multipart = (Multipart) message.getBody();
93
 
 
94
 
        // Insert a new text/plain body part after every body part of the
95
 
        // template.
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);
101
 
        }
102
 
 
103
 
        // For no particular reason remove the second binary body part (now
104
 
        // at index four).
105
 
        BodyPart removed = multipart.removeBodyPart(4);
106
 
 
107
 
        // The removed body part no longer has a parent entity it belongs to so
108
 
        // it should be disposed of.
109
 
        removed.dispose();
110
 
 
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>"));
116
 
 
117
 
        return message;
118
 
    }
119
 
 
120
 
    /**
121
 
     * Creates a multipart/mixed message that consists of three parts (one text,
122
 
     * two binary).
123
 
     */
124
 
    private static Message createTemplate() throws IOException {
125
 
        Multipart multipart = new Multipart("mixed");
126
 
 
127
 
        BodyPart part1 = createTextPart("This is the first part of the template..");
128
 
        multipart.addBodyPart(part1);
129
 
 
130
 
        BodyPart part2 = createRandomBinaryPart(200);
131
 
        multipart.addBodyPart(part2);
132
 
 
133
 
        BodyPart part3 = createRandomBinaryPart(300);
134
 
        multipart.addBodyPart(part3);
135
 
 
136
 
        Message message = new Message();
137
 
        message.setMultipart(multipart);
138
 
 
139
 
        message.setSubject("Template message");
140
 
 
141
 
        return message;
142
 
    }
143
 
 
144
 
    /**
145
 
     * Creates a text part from the specified string.
146
 
     */
147
 
    private static BodyPart createTextPart(String text) {
148
 
        TextBody body = new BodyFactory().textBody(text, "UTF-8");
149
 
 
150
 
        BodyPart bodyPart = new BodyPart();
151
 
        bodyPart.setText(body);
152
 
        bodyPart.setContentTransferEncoding("quoted-printable");
153
 
 
154
 
        return bodyPart;
155
 
    }
156
 
 
157
 
    /**
158
 
     * Creates a binary part with random content.
159
 
     */
160
 
    private static BodyPart createRandomBinaryPart(int numberOfBytes)
161
 
            throws IOException {
162
 
        byte[] data = new byte[numberOfBytes];
163
 
        new Random().nextBytes(data);
164
 
 
165
 
        Body body = new BodyFactory()
166
 
                .binaryBody(new ByteArrayInputStream(data));
167
 
 
168
 
        BodyPart bodyPart = new BodyPart();
169
 
        bodyPart.setBody(body, "application/octet-stream");
170
 
        bodyPart.setContentTransferEncoding("base64");
171
 
 
172
 
        return bodyPart;
173
 
    }
174
 
 
175
 
}