1 21 22 27 28 package javax.mail.internet; 29 30 import javax.mail.*; 31 import javax.activation.*; 32 import java.io.*; 33 import java.net.UnknownServiceException ; 34 35 43 44 public class MimePartDataSource implements DataSource, MessageAware { 45 50 protected MimePart part; 51 52 private MessageContext context; 53 54 private static boolean ignoreMultipartEncoding = true; 55 56 static { 57 try { 58 String s = System.getProperty("mail.mime.ignoremultipartencoding"); 59 ignoreMultipartEncoding = s == null || !s.equalsIgnoreCase("false"); 61 } catch (SecurityException sex) { 62 } 64 } 65 66 69 public MimePartDataSource(MimePart part) { 70 this.part = part; 71 } 72 73 89 public InputStream getInputStream() throws IOException { 90 InputStream is; 91 92 try { 93 if (part instanceof MimeBodyPart ) 94 is = ((MimeBodyPart )part).getContentStream(); 95 else if (part instanceof MimeMessage ) 96 is = ((MimeMessage )part).getContentStream(); 97 else 98 throw new MessagingException("Unknown part"); 99 100 String encoding = restrictEncoding(part.getEncoding(), part); 101 if (encoding != null) 102 return MimeUtility.decode(is, encoding); 103 else 104 return is; 105 } catch (MessagingException mex) { 106 throw new IOException(mex.getMessage()); 107 } 108 } 109 110 115 private static String restrictEncoding(String encoding, MimePart part) 116 throws MessagingException { 117 if (!ignoreMultipartEncoding || encoding == null) 118 return encoding; 119 120 if (encoding.equalsIgnoreCase("7bit") || 121 encoding.equalsIgnoreCase("8bit") || 122 encoding.equalsIgnoreCase("binary")) 123 return encoding; 125 String type = part.getContentType(); 126 if (type == null) 127 return encoding; 128 129 try { 130 135 ContentType cType = new ContentType (type); 136 if (cType.match("multipart/*") || cType.match("message/*")) 137 return null; 138 } catch (ParseException pex) { 139 } 141 return encoding; 142 } 143 144 145 150 public OutputStream getOutputStream() throws IOException { 151 throw new UnknownServiceException (); 152 } 153 154 160 public String getContentType() { 161 try { 162 return part.getContentType(); 163 } catch (MessagingException mex) { 164 return null; 165 } 166 } 167 168 173 public String getName() { 174 try { 175 if (part instanceof MimeBodyPart ) 176 return ((MimeBodyPart )part).getFileName(); 177 } catch (MessagingException mex) { 178 } 180 return ""; 181 } 182 183 187 public synchronized MessageContext getMessageContext() { 188 if (context == null) 189 context = new MessageContext(part); 190 return context; 191 } 192 } 193 | Popular Tags |