source: trunk/LogicMail/src/org/logicprobe/LogicMail/message/MimeMessageContentFactory.java @ 766

Revision 766, 7.8 KB checked in by octorian, 14 months ago (diff)

Message load process refactoring, work in progress

Line 
1/*-
2 * Copyright (c) 2009, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31package org.logicprobe.LogicMail.message;
32
33/**
34 * Creates message content objects.
35 */
36public class MimeMessageContentFactory {
37    /**
38         * Creates a new MessageContent object.
39         *
40         * @param mimeMessagePart The message part describing the MIME properties for the content.
41         * @param encodedData The encoded data from which to decode the content.
42         *
43         * @return The message content object.
44         *
45         * @throws UnsupportedContentException Thrown if the content type was not supported or the data could not be decoded.
46         */
47        public static MimeMessageContent createContentEncoded(MimeMessagePart mimeMessagePart, byte[] encodedData) throws UnsupportedContentException {
48        MimeMessageContent content;
49        if(mimeMessagePart instanceof TextPart) {
50                TextPart textPart = (TextPart)mimeMessagePart;
51                content = new TextContent(textPart, textPart.getEncoding(), textPart.getCharset(), encodedData);
52        }
53        else if(mimeMessagePart instanceof ImagePart) {
54                ImagePart imagePart = (ImagePart)mimeMessagePart;
55                content = new ImageContent(imagePart, imagePart.getEncoding(), encodedData);
56        }
57        else if(mimeMessagePart instanceof ApplicationPart) {
58                ApplicationPart applicationPart = (ApplicationPart)mimeMessagePart;
59                content = new ApplicationContent(applicationPart, applicationPart.getEncoding(), encodedData);
60        }
61        else if(mimeMessagePart instanceof AudioPart) {
62                AudioPart audioPart = (AudioPart)mimeMessagePart;
63                content = new AudioContent(audioPart, audioPart.getEncoding(), encodedData);
64        }
65        else if(mimeMessagePart instanceof VideoPart) {
66                VideoPart videoPart = (VideoPart)mimeMessagePart;
67                content = new VideoContent(videoPart, videoPart.getEncoding(), encodedData);
68        }
69        else if(mimeMessagePart instanceof MessagePart) {
70                MessagePart messagePart = (MessagePart)mimeMessagePart;
71                content = new MessageContent(messagePart, messagePart.getEncoding(), encodedData);
72        }
73        else {
74                throw new UnsupportedContentException("Unsupported content type");
75        }
76        return content;
77    }
78
79    /**
80     * Creates a new MessageContent object from raw data.
81     *
82     * @param mimeMessagePart The message part describing the MIME properties for the content.
83     * @param rawData The raw data from which to encode the content.
84     *
85     * @return The message content object.
86     *
87     * @throws UnsupportedContentException Thrown if the content type was not supported or the data could not be decoded.
88     */
89    public static MimeMessageContent createContentRaw(MimeMessagePart mimeMessagePart, byte[] rawData) throws UnsupportedContentException {
90        return createContentRaw(mimeMessagePart, rawData, true);
91    }
92   
93    /**
94     * Creates a new MessageContent object from raw data.
95     *
96     * @param mimeMessagePart The message part describing the MIME properties for the content.
97     * @param rawData The raw data from which to encode the content.
98     * @param decode True, if the data should be decoded into a form usable for
99     *   display.  This should always be the case, unless the content is being
100     *   created solely for the purposes of serialization.
101     *
102     * @return The message content object.
103     *
104     * @throws UnsupportedContentException Thrown if the content type was not supported or the data could not be decoded.
105     */
106        public static MimeMessageContent createContentRaw(MimeMessagePart mimeMessagePart, byte[] rawData, boolean decode) throws UnsupportedContentException {
107            MimeMessageContent content;
108        if(mimeMessagePart instanceof TextPart) {
109            TextPart textPart = (TextPart)mimeMessagePart;
110            content = new TextContent(textPart, rawData, decode);
111        }
112        else if(mimeMessagePart instanceof ImagePart) {
113            ImagePart imagePart = (ImagePart)mimeMessagePart;
114            content = new ImageContent(imagePart, rawData, decode);
115        }
116        else if(mimeMessagePart instanceof ApplicationPart) {
117            ApplicationPart applicationPart = (ApplicationPart)mimeMessagePart;
118            content = new ApplicationContent(applicationPart, rawData);
119        }
120        else if(mimeMessagePart instanceof AudioPart) {
121            AudioPart audioPart = (AudioPart)mimeMessagePart;
122            content = new AudioContent(audioPart, rawData);
123        }
124        else if(mimeMessagePart instanceof VideoPart) {
125            VideoPart videoPart = (VideoPart)mimeMessagePart;
126            content = new VideoContent(videoPart, rawData);
127        }
128        else if(mimeMessagePart instanceof MessagePart) {
129            MessagePart messagePart = (MessagePart)mimeMessagePart;
130            content = new MessageContent(messagePart, rawData);
131        }
132        else {
133            throw new UnsupportedContentException("Unsupported content type");
134        }
135            return content;
136        }
137       
138        /**
139     * Find out if a particular message content type is supported
140     * without having to create it.  This is useful to optimize
141     * downloads on protocols that support selective retrieval
142     * of message content.
143     *
144     * @param mimeMessagePart Message part object
145     * @return True if supported, false if unsupported
146     */
147    public static boolean isContentSupported(MimeMessagePart mimeMessagePart) {
148        boolean result;
149        if(mimeMessagePart instanceof TextPart) {
150                result = TextContent.isPartSupported((TextPart)mimeMessagePart);
151        }
152        else if(mimeMessagePart instanceof ImagePart) {
153                result = ImageContent.isPartSupported((ImagePart)mimeMessagePart);
154        }
155        else if(mimeMessagePart instanceof ApplicationPart) {
156                result = ApplicationContent.isPartSupported((ApplicationPart)mimeMessagePart);
157        }
158        else if(mimeMessagePart instanceof AudioPart) {
159                result = AudioContent.isPartSupported((AudioPart)mimeMessagePart);
160        }
161        else if(mimeMessagePart instanceof VideoPart) {
162                result = VideoContent.isPartSupported((VideoPart)mimeMessagePart);
163        }
164        else if(mimeMessagePart instanceof MessagePart) {
165                result = MessageContent.isPartSupported((MessagePart)mimeMessagePart);
166        }
167        else {
168                result = false;
169        }
170        return result;
171    }
172}
Note: See TracBrowser for help on using the repository browser.