| 1 | /*- |
|---|
| 2 | * Copyright (c) 2006, 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 | */ |
|---|
| 31 | package org.logicprobe.LogicMail.message; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Creates message parts, doing the necessary decoding. |
|---|
| 35 | */ |
|---|
| 36 | public class MimeMessagePartFactory { |
|---|
| 37 | /** |
|---|
| 38 | * Create a new message part |
|---|
| 39 | * @param mimeType MIME type |
|---|
| 40 | * @param mimeSubtype MIME subtype |
|---|
| 41 | * @param name Name of the message part |
|---|
| 42 | * @param encoding Encoding type (i.e. 7bit, base64) |
|---|
| 43 | * @param param Type-specific parameter (i.e. charset, filename) |
|---|
| 44 | * @param disposition Content disposition for the part |
|---|
| 45 | * @param contentId Content ID |
|---|
| 46 | * @param size Size of the content this part refers to, or -1 if not available |
|---|
| 47 | * @param tag Protocol-specific tag for addressing the part |
|---|
| 48 | */ |
|---|
| 49 | public static MimeMessagePart createMimeMessagePart( |
|---|
| 50 | String mimeType, |
|---|
| 51 | String mimeSubtype, |
|---|
| 52 | String name, |
|---|
| 53 | String encoding, |
|---|
| 54 | String param, |
|---|
| 55 | String disposition, |
|---|
| 56 | String contentId, |
|---|
| 57 | int size, |
|---|
| 58 | String tag) { |
|---|
| 59 | |
|---|
| 60 | // Make sure we aren't creating any null fields |
|---|
| 61 | if(name == null) { name = ""; } |
|---|
| 62 | if(encoding == null) { encoding = ""; } |
|---|
| 63 | if(param == null) { param = ""; } |
|---|
| 64 | if(disposition == null) { disposition = ""; } |
|---|
| 65 | if(contentId == null) { contentId = ""; } |
|---|
| 66 | if(tag == null) { tag = ""; } |
|---|
| 67 | |
|---|
| 68 | MimeMessagePart part; |
|---|
| 69 | if (mimeType.equalsIgnoreCase("multipart")) { |
|---|
| 70 | part = new MultiPart(mimeSubtype, tag); |
|---|
| 71 | } else if (mimeType.equalsIgnoreCase(TextPart.TYPE)) { |
|---|
| 72 | part = new TextPart(mimeSubtype, name, encoding, param, disposition, contentId, size, tag); |
|---|
| 73 | } else if (mimeType.equalsIgnoreCase("image")) { |
|---|
| 74 | part = new ImagePart(mimeSubtype, name, encoding, disposition, contentId, size, tag); |
|---|
| 75 | } else if (mimeType.equalsIgnoreCase("application")) { |
|---|
| 76 | part = new ApplicationPart(mimeSubtype, name, encoding, disposition, contentId, size, tag); |
|---|
| 77 | } else if (mimeType.equalsIgnoreCase("audio")) { |
|---|
| 78 | part = new AudioPart(mimeSubtype, name, encoding, disposition, contentId, size, tag); |
|---|
| 79 | } else if (mimeType.equalsIgnoreCase("video")) { |
|---|
| 80 | part = new VideoPart(mimeSubtype, name, encoding, disposition, contentId, size, tag); |
|---|
| 81 | } else if (mimeType.equalsIgnoreCase("message")) { |
|---|
| 82 | part = new MessagePart(mimeSubtype, name, encoding, disposition, contentId, size, tag); |
|---|
| 83 | } else { |
|---|
| 84 | part = new UnsupportedPart(mimeType, mimeSubtype, tag); |
|---|
| 85 | } |
|---|
| 86 | return part; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Create a new message part |
|---|
| 91 | * @param mimeType MIME type |
|---|
| 92 | * @param mimeSubtype MIME subtype |
|---|
| 93 | * @param name Name of the message part |
|---|
| 94 | * @param encoding Encoding type (i.e. 7bit, base64) |
|---|
| 95 | * @param param Type-specific parameter (i.e. charset, filename) |
|---|
| 96 | * @param disposition Content disposition for the part |
|---|
| 97 | * @param contentId Content ID |
|---|
| 98 | * @param size Size of the content this part refers to, or -1 if not available |
|---|
| 99 | */ |
|---|
| 100 | public static MimeMessagePart createMimeMessagePart( |
|---|
| 101 | String mimeType, |
|---|
| 102 | String mimeSubtype, |
|---|
| 103 | String name, |
|---|
| 104 | String encoding, |
|---|
| 105 | String param, |
|---|
| 106 | String disposition, |
|---|
| 107 | String contentId, |
|---|
| 108 | int size) { |
|---|
| 109 | return MimeMessagePartFactory.createMimeMessagePart(mimeType, mimeSubtype, name, encoding, param, disposition, contentId, size, ""); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Find out if a particular message part type is supported |
|---|
| 114 | * as displayable content. This is useful to optimize |
|---|
| 115 | * downloads on protocols that support selective retrieval |
|---|
| 116 | * of message parts. |
|---|
| 117 | * Right now the supported subtypes are hard-coded, but they |
|---|
| 118 | * should ultimately be programmatically determined upon |
|---|
| 119 | * initialization of this factory. |
|---|
| 120 | * |
|---|
| 121 | * @param mimeType MIME type |
|---|
| 122 | * @param mimeSubtype MIME subtype |
|---|
| 123 | * @return True if supported, false if unsupported |
|---|
| 124 | */ |
|---|
| 125 | public static boolean isMimeMessagePartSupported(String mimeType, |
|---|
| 126 | String mimeSubtype) { |
|---|
| 127 | if (mimeType.equalsIgnoreCase("multipart")) { |
|---|
| 128 | return isMultiPartSupported(mimeSubtype); |
|---|
| 129 | } else if (mimeType.equalsIgnoreCase(TextPart.TYPE)) { |
|---|
| 130 | return isTextPartSupported(mimeSubtype); |
|---|
| 131 | } else if (mimeType.equalsIgnoreCase("image")) { |
|---|
| 132 | return isImagePartSupported(mimeSubtype); |
|---|
| 133 | } else { |
|---|
| 134 | return false; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | private static boolean isMultiPartSupported(String mimeSubtype) { |
|---|
| 139 | return (mimeSubtype.equalsIgnoreCase(MultiPart.MIXED) || |
|---|
| 140 | mimeSubtype.equalsIgnoreCase(MultiPart.RELATED) || |
|---|
| 141 | mimeSubtype.equalsIgnoreCase(MultiPart.ALTERNATIVE) || |
|---|
| 142 | mimeSubtype.equalsIgnoreCase(MultiPart.SIGNED)); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | private static boolean isTextPartSupported(String mimeSubtype) { |
|---|
| 146 | // TODO: Add logic to only load plain or html, not both |
|---|
| 147 | return (mimeSubtype.equalsIgnoreCase(TextPart.SUBTYPE_PLAIN) || |
|---|
| 148 | mimeSubtype.equalsIgnoreCase(TextPart.SUBTYPE_HTML)); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private static boolean isImagePartSupported(String mimeSubtype) { |
|---|
| 152 | return (mimeSubtype.equalsIgnoreCase("gif") || |
|---|
| 153 | mimeSubtype.equalsIgnoreCase("png") || |
|---|
| 154 | mimeSubtype.equalsIgnoreCase("vnd.wap.wbmp") || |
|---|
| 155 | mimeSubtype.equalsIgnoreCase("jpeg") || |
|---|
| 156 | mimeSubtype.equalsIgnoreCase("jpg") || |
|---|
| 157 | mimeSubtype.equalsIgnoreCase("pjpeg") || |
|---|
| 158 | mimeSubtype.equalsIgnoreCase("bmp") || |
|---|
| 159 | mimeSubtype.equalsIgnoreCase("tif")); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|