/** * Returns the content-type of this DataSource. <p> * * This implementation just invokes the <code>getContentType</code> * method on the MimePart. */ public String getContentType() { try { return part.getContentType(); } catch (MessagingException mex) { // would like to be able to reflect the exception to the // application, but since we can't do that we return a // generic "unknown" value here and hope for another // exception later. return "application/octet-stream"; } }
/** * Creates a MimePart from HTML part. * * @return mimePart from HTML part * @throws MessagingException */ private MimePart createHtmlMimePart() throws MessagingException { MimePart bodyPart = new MimeBodyPart(); bodyPart.setContent(htmlPart, "text/html; charset=utf-8"); return bodyPart; }
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); s = MimeUtil.cleanContentType(part, s); 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; }
is = ((MimeBodyPart)part).getContentStream(); else if (part instanceof MimeMessage) is = ((MimeMessage)part).getContentStream(); else throw new MessagingException("Unknown part"); MimeBodyPart.restrictEncoding(part, part.getEncoding()); if (encoding != null) return MimeUtility.decode(is, encoding); else return is; fex.getMessage()); } catch (MessagingException mex) { IOException ioex = new IOException(mex.getMessage()); ioex.initCause(mex); throw ioex;
static void updateHeaders(MimePart part) throws MessagingException { DataHandler dh = part.getDataHandler(); if (dh == null) // Huh ? return; String type = dh.getContentType(); boolean composite = false; boolean needCTHeader = part.getHeader("Content-Type") == null; if (mpart != part) { if (needCTHeader) part.setHeader("Content-Type", mpart.getContentType()); String enc = mpart.getEncoding(); if (enc != null) { setEncoding(part, enc); return; if (part.getHeader("Content-Transfer-Encoding") == null) setEncoding(part, MimeUtility.getEncoding(dh)); String enc = part.getEncoding(); if (enc != null && enc.equalsIgnoreCase("7bit")) charset = "us-ascii"; else charset = MimeUtility.getDefaultMIMECharset(); String s = part.getHeader("Content-Disposition", null); if (s != null) {
if (attachments != null && attachments.length > 0) { Multipart multipart = new MimeMultipart(); bodyPart = new MimeBodyPart(); bodyPart.setContent(body, type); for (DataSource attachment : attachments) { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setFileName(attachment.getName()); multipart.addBodyPart(attachmentPart); bodyPart.setContent(body, type); } else { bodyPart.setText(body.toString());
String s = part.getHeader("Content-Disposition", null); ContentDisposition cd = new ContentDisposition(s == null ? Part.ATTACHMENT : s); else p.set("filename", name, charset); part.setHeader("Content-Disposition", cd.toString()); s = part.getHeader("Content-Type", null); s = MimeUtil.cleanContentType(part, s); if (s != null) { try { ContentType cType = new ContentType(s); p = cType.getParameterList(); if (p == null) { p = new ParameterList(); cType.setParameterList(p); else p.set("name", name, charset); part.setHeader("Content-Type", cType.toString()); } catch (ParseException pex) { } // ignore it
/** * Creates a MimePart from text part. * * @return mimePart from HTML string * @throws MessagingException */ private MimePart createTextMimePart() throws MessagingException { MimePart bodyPart = new MimeBodyPart(); bodyPart.setText(textPart); return bodyPart; }
boolean changed = false; try { if (part.isMimeType("text/*")) { String enc = part.getEncoding(); if (enc != null && (enc.equalsIgnoreCase("quoted-printable") || enc.equalsIgnoreCase("base64"))) { InputStream is = null; try { is = part.getInputStream(); if (is8Bit(is)) { part.setContent(part.getContent(), part.getContentType()); part.setHeader("Content-Transfer-Encoding", "8bit"); changed = true; } else if (part.isMimeType("multipart/*")) { MimeMultipart mp = (MimeMultipart)part.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) {
static boolean isMimeType(MimePart part, String mimeType) throws MessagingException { // XXX - lots of room for optimization here! String type = part.getContentType(); try { return new ContentType(type).match(mimeType); } catch (ParseException ex) { // we only need the type and subtype so throw away the rest try { int i = type.indexOf(';'); if (i > 0) return new ContentType(type.substring(0, i)).match(mimeType); } catch (ParseException pex2) { } return type.equalsIgnoreCase(mimeType); } }
/** * Checks whether the MimePart contains an object of the given mime type. * * @param part the current MimePart * @param mimeType the mime type to check * @return {@code true} if the MimePart matches the given mime type, {@code false} otherwise * @throws MessagingException parsing the MimeMessage failed * @throws IOException parsing the MimeMessage failed */ private boolean isMimeType(final MimePart part, final String mimeType) throws MessagingException, IOException { // Do not use part.isMimeType(String) as it is broken for MimeBodyPart // and does not really check the actual content type. try { final ContentType ct = new ContentType(part.getDataHandler().getContentType()); return ct.match(mimeType); } catch (final ParseException ex) { return part.getContentType().equalsIgnoreCase(mimeType); } }
String lang = LogManagerProperties.toLanguageTag(l); if (lang.length() != 0) { String header = p.getHeader("Content-Language", null); if (isEmpty(header)) { p.setHeader("Content-Language", lang); } else if (!header.equalsIgnoreCase(lang)) { lang = ",".concat(lang); header = header.concat(lang); p.setHeader("Content-Language", header); reportError(ME.getMessage(), ME, ErrorManager.FORMAT_FAILURE);
public MimePart convertTo7Bit(MimePart part) throws MessagingException, IOException { if (part.isMimeType("multipart/*")) { List<BodyPart> bodyParts = MultipartUtil.retrieveBodyParts((MimeMultipart) part.getContent()); for (BodyPart bodyPart : bodyParts) { convertTo7Bit((MimePart) bodyPart); } } else if ("8bit".equals(part.getEncoding())) { // The content may already be in encoded the form (likely with mail // created from a // stream). In that case, just changing the encoding to // quoted-printable will mangle // the result when this is transmitted. We must first convert the // content into its // native format, set it back, and only THEN set the transfer // encoding to force the // content to be encoded appropriately. // if the part doesn't contain text it will be base64 encoded. String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" : "base64"; part.setContent(part.getContent(), part.getContentType()); part.setHeader("Content-Transfer-Encoding", contentTransferEncoding); part.addHeader("X-MIME-Autoconverted", "from 8bit to " + contentTransferEncoding + " by " + mailetContext.getServerInfo()); } return part; }
= part.getNonMatchingHeaderLines(ignoreList); while (hdrLines.hasMoreElements()) los.writeln(hdrLines.nextElement()); DataHandler dh = part.getDataHandler(); if (dh instanceof MimePartDataHandler) { MimePartDataHandler mpdh = (MimePartDataHandler)dh; MimePart mpart = mpdh.getPart(); if (mpart.getEncoding() != null) is = mpdh.getContentStream(); os.write(buf, 0, len); } else { os = MimeUtility.encode(os, restrictEncoding(part, part.getEncoding())); part.getDataHandler().writeTo(os);
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)); }
/** * Prepends the content of the MimePart as text to the existing footer * * @param part the MimePart to attach * * @throws MessagingException * @throws IOException */ protected void addToText(MimePart part) throws MessagingException, IOException { // log("Trying to add footer to " + part.getContent().toString()); String contentType = part.getContentType(); String content = (String) part.getContent(); if (!content.endsWith("\n")) { content += "\r\n"; } content += getFooterText(); part.setContent(content, contentType); part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType); // log("After adding footer: " + part.getContent().toString()); }
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; } }
LOGGER.debug("mime type is: \"{}\"", part.getContentType()); if (part.isMimeType("text/plain") || part.isMimeType("text/html")) { LOGGER.debug("scanning: \"{}\"", part.getContent()); HashSet<String> newDom = URIScanner.scanContentForDomains(domains, part.getContent().toString()); domains.addAll(newDom); } else if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.getContent(); int count = multipart.getCount(); LOGGER.debug("multipart count is: {}", count);