📄 mimebodypart.java
字号:
// XXX - lots of room for optimization here! try { ContentType ct = new ContentType(part.getContentType()); return ct.match(mimeType); } catch (ParseException ex) { return part.getContentType().equalsIgnoreCase(mimeType); } } static void setText(MimePart part, String text, String charset, String subtype) throws MessagingException { if (charset == null) { if (MimeUtility.checkAscii(text) != MimeUtility.ALL_ASCII) charset = MimeUtility.getDefaultMIMECharset(); else charset = "us-ascii"; } // XXX - should at least ensure that subtype is an atom part.setContent(text, "text/" + subtype + "; charset=" + MimeUtility.quote(charset, HeaderTokenizer.MIME)); } static String getDisposition(MimePart part) throws MessagingException { String s = part.getHeader("Content-Disposition", null); if (s == null) return null; ContentDisposition cd = new ContentDisposition(s); return cd.getDisposition(); } static void setDisposition(MimePart part, String disposition) throws MessagingException { if (disposition == null) part.removeHeader("Content-Disposition"); else { String s = part.getHeader("Content-Disposition", null); if (s != null) { /* A Content-Disposition header already exists .. * * Override disposition, but attempt to retain * existing disposition parameters */ ContentDisposition cd = new ContentDisposition(s); cd.setDisposition(disposition); disposition = cd.toString(); } part.setHeader("Content-Disposition", disposition); } } static String getDescription(MimePart part) throws MessagingException { String rawvalue = part.getHeader("Content-Description", null); if (rawvalue == null) return null; try { return MimeUtility.decodeText(MimeUtility.unfold(rawvalue)); } catch (UnsupportedEncodingException ex) { return rawvalue; } } static void setDescription(MimePart part, String description, String charset) throws MessagingException { if (description == null) { part.removeHeader("Content-Description"); return; } try { part.setHeader("Content-Description", MimeUtility.fold(21, MimeUtility.encodeText(description, charset, null))); } catch (UnsupportedEncodingException uex) { throw new MessagingException("Encoding error", uex); } } static String getFileName(MimePart part) throws MessagingException { String filename = null; String s = part.getHeader("Content-Disposition", null); if (s != null) { // Parse the header .. ContentDisposition cd = new ContentDisposition(s); filename = cd.getParameter("filename"); } if (filename == null) { // Still no filename ? Try the "name" ContentType parameter s = part.getHeader("Content-Type", null); if (s != null) { try { ContentType ct = new ContentType(s); filename = ct.getParameter("name"); } catch (ParseException pex) { } // ignore it } } if (decodeFileName && filename != null) { try { filename = MimeUtility.decodeText(filename); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Can't decode filename", ex); } } return filename; } static void setFileName(MimePart part, String name) throws MessagingException { if (encodeFileName && name != null) { try { name = MimeUtility.encodeText(name); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Can't encode filename", ex); } } // Set the Content-Disposition "filename" parameter String s = part.getHeader("Content-Disposition", null); ContentDisposition cd = new ContentDisposition(s == null ? Part.ATTACHMENT : s); cd.setParameter("filename", name); part.setHeader("Content-Disposition", cd.toString()); /* * Also attempt to set the Content-Type "name" parameter, * to satisfy ancient MUAs. XXX - This is not RFC compliant. */ if (setContentTypeFileName) { s = part.getHeader("Content-Type", null); if (s != null) { try { ContentType cType = new ContentType(s); cType.setParameter("name", name); part.setHeader("Content-Type", cType.toString()); } catch (ParseException pex) { } // ignore it } } } static String[] getContentLanguage(MimePart part) throws MessagingException { String s = part.getHeader("Content-Language", null); if (s == null) return null; // Tokenize the header to obtain the Language-tags (skip comments) HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); Vector v = new Vector(); HeaderTokenizer.Token tk; int tkType; while (true) { tk = h.next(); // get a language-tag tkType = tk.getType(); if (tkType == HeaderTokenizer.Token.EOF) break; // done else if (tkType == HeaderTokenizer.Token.ATOM) v.addElement(tk.getValue()); else // invalid token, skip it. continue; } if (v.size() == 0) return null; String[] language = new String[v.size()]; v.copyInto(language); return language; } static void setContentLanguage(MimePart part, String[] languages) throws MessagingException { StringBuffer sb = new StringBuffer(languages[0]); for (int i = 1; i < languages.length; i++) sb.append(',').append(languages[i]); part.setHeader("Content-Language", sb.toString()); } static String getEncoding(MimePart part) throws MessagingException { String s = part.getHeader("Content-Transfer-Encoding", null); if (s == null) return null; s = s.trim(); // get rid of trailing spaces // quick check for known values to avoid unnecessary use // of tokenizer. if (s.equalsIgnoreCase("7bit") || s.equalsIgnoreCase("8bit") || s.equalsIgnoreCase("quoted-printable") || s.equalsIgnoreCase("binary") || s.equalsIgnoreCase("base64")) return s; // Tokenize the header to obtain the encoding (skip comments) HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); HeaderTokenizer.Token tk; int tkType; for (;;) { tk = h.next(); // get a token tkType = tk.getType(); if (tkType == HeaderTokenizer.Token.EOF) break; // done else if (tkType == HeaderTokenizer.Token.ATOM) return tk.getValue(); else // invalid token, skip it. continue; } return s; } static void setEncoding(MimePart part, String encoding) throws MessagingException { part.setHeader("Content-Transfer-Encoding", encoding); } static void updateHeaders(MimePart part) throws MessagingException { DataHandler dh = part.getDataHandler(); if (dh == null) // Huh ? return; try { String type = dh.getContentType(); boolean composite = false; boolean needCTHeader = part.getHeader("Content-Type") == null; ContentType cType = new ContentType(type); if (cType.match("multipart/*")) { // If multipart, recurse composite = true; Object o; if (part instanceof MimeBodyPart) { MimeBodyPart mbp = (MimeBodyPart)part; o = mbp.cachedContent != null ? mbp.cachedContent : dh.getContent(); } else if (part instanceof MimeMessage) { MimeMessage msg = (MimeMessage)part; o = msg.cachedContent != null ? msg.cachedContent : dh.getContent(); } else o = dh.getContent(); if (o instanceof MimeMultipart) ((MimeMultipart)o).updateHeaders(); else throw new MessagingException("MIME part of type \"" + type + "\" contains object of type " + o.getClass().getName() + " instead of MimeMultipart"); } else if (cType.match("message/rfc822")) { composite = true; // XXX - call MimeMessage.updateHeaders()? } // Content-Transfer-Encoding, but only if we don't // already have one if (!composite) { // not allowed on composite parts if (part.getHeader("Content-Transfer-Encoding") == null) setEncoding(part, MimeUtility.getEncoding(dh)); if (needCTHeader && setDefaultTextCharset && cType.match("text/*") && cType.getParameter("charset") == null) { /* * Set a default charset for text parts. * We really should examine the data to determine * whether or not it's all ASCII, but that's too * expensive so we make an assumption: If we * chose 7bit encoding for this data, it's probably * ASCII. (MimeUtility.getEncoding will choose * 7bit only in this case, but someone might've * set the Content-Transfer-Encoding header manually.) */ String charset; String enc = part.getEncoding(); if (enc != null && enc.equalsIgnoreCase("7bit")) charset = "us-ascii"; else charset = MimeUtility.getDefaultMIMECharset(); cType.setParameter("charset", charset); type = cType.toString(); } } // Now, let's update our own headers ... // Content-type, but only if we don't already have one if (needCTHeader) { /* * Pull out "filename" from Content-Disposition, and * use that to set the "name" parameter. This is to * satisfy older MUAs (DtMail, Roam and probably * a bunch of others). */ String s = part.getHeader("Content-Disposition", null); if (s != null) { // Parse the header .. ContentDisposition cd = new ContentDisposition(s); String filename = cd.getParameter("filename"); if (filename != null) { cType.setParameter("name", filename); type = cType.toString(); } } part.setHeader("Content-Type", type); } } catch (IOException ex) { throw new MessagingException("IOException updating headers", ex); } } static void invalidateContentHeaders(MimePart part) throws MessagingException { part.removeHeader("Content-Type"); part.removeHeader("Content-Transfer-Encoding"); } static void writeTo(MimePart part, OutputStream os, String[] ignoreList) throws IOException, MessagingException { // see if we already have a LOS LineOutputStream los = null; if (os instanceof LineOutputStream) { los = (LineOutputStream) os; } else { los = new LineOutputStream(os); } // First, write out the header Enumeration hdrLines = part.getNonMatchingHeaderLines(ignoreList); while (hdrLines.hasMoreElements()) los.writeln((String)hdrLines.nextElement()); // The CRLF separator between header and content los.writeln(); // Finally, the content. Encode if required. // XXX: May need to account for ESMTP ? os = MimeUtility.encode(os, part.getEncoding()); part.getDataHandler().writeTo(os); os.flush(); // Needed to complete encoding }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -