root/trunk/LogicMail/src/org/logicprobe/LogicMail/message/MimeMessageContentFactory.java

Revision 689, 6.9 KB (checked in by octorian, 2 weeks ago)

Conversion of connection layer to be byte-based, and complete rewrite of the IMAP paren-string parser (#224, #222)

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.
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            MimeMessageContent content;
91        if(mimeMessagePart instanceof TextPart) {
92            TextPart textPart = (TextPart)mimeMessagePart;
93            content = new TextContent(textPart, rawData);
94        }
95        else if(mimeMessagePart instanceof ImagePart) {
96            ImagePart imagePart = (ImagePart)mimeMessagePart;
97            content = new ImageContent(imagePart, rawData);
98        }
99        else if(mimeMessagePart instanceof ApplicationPart) {
100            ApplicationPart applicationPart = (ApplicationPart)mimeMessagePart;
101            content = new ApplicationContent(applicationPart, rawData);
102        }
103        else if(mimeMessagePart instanceof AudioPart) {
104            AudioPart audioPart = (AudioPart)mimeMessagePart;
105            content = new AudioContent(audioPart, rawData);
106        }
107        else if(mimeMessagePart instanceof VideoPart) {
108            VideoPart videoPart = (VideoPart)mimeMessagePart;
109            content = new VideoContent(videoPart, rawData);
110        }
111        else if(mimeMessagePart instanceof MessagePart) {
112            MessagePart messagePart = (MessagePart)mimeMessagePart;
113            content = new MessageContent(messagePart, rawData);
114        }
115        else {
116            throw new UnsupportedContentException("Unsupported content type");
117        }
118            return content;
119        }
120       
121        /**
122     * Find out if a particular message content type is supported
123     * without having to create it.  This is useful to optimize
124     * downloads on protocols that support selective retrieval
125     * of message content.
126     *
127     * @param mimeMessagePart Message part object
128     * @return True if supported, false if unsupported
129     */
130    public static boolean isContentSupported(MimeMessagePart mimeMessagePart) {
131        boolean result;
132        if(mimeMessagePart instanceof TextPart) {
133                result = TextContent.isPartSupported((TextPart)mimeMessagePart);
134        }
135        else if(mimeMessagePart instanceof ImagePart) {
136                result = ImageContent.isPartSupported((ImagePart)mimeMessagePart);
137        }
138        else if(mimeMessagePart instanceof ApplicationPart) {
139                result = ApplicationContent.isPartSupported((ApplicationPart)mimeMessagePart);
140        }
141        else if(mimeMessagePart instanceof AudioPart) {
142                result = AudioContent.isPartSupported((AudioPart)mimeMessagePart);
143        }
144        else if(mimeMessagePart instanceof VideoPart) {
145                result = VideoContent.isPartSupported((VideoPart)mimeMessagePart);
146        }
147        else if(mimeMessagePart instanceof MessagePart) {
148                result = MessageContent.isPartSupported((MessagePart)mimeMessagePart);
149        }
150        else {
151                result = false;
152        }
153        return result;
154    }
155}
Note: See TracBrowser for help on using the browser.