List of usage examples for javax.mail.internet MimeUtility decodeText
public static String decodeText(String etext) throws UnsupportedEncodingException
From source file:org.elasticsearch.river.email.EmailToJson.java
public static String decodeText(String encodeText) throws UnsupportedEncodingException { if (encodeText == null || "".equals(encodeText)) { return ""; } else {//from w ww. ja v a 2 s.c o m return MimeUtility.decodeText(encodeText); } }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public Attach getAttach(Part part, String baseStorePath) throws MessagingException, IOException { MimeBodyPart mimePart = (MimeBodyPart) part; Attach attach = new Attach(); if (StringUtils.isEmpty(mimePart.getFileName())) { attach.setFileName("UNKNOWN"); } else {// w ww .ja v a 2 s.c o m String fileName = mimePart.getFileName(); String encoded = System.getProperty("mail.mime.encodefilename"); if (Boolean.parseBoolean(encoded)) { fileName = MimeUtility.decodeText(fileName); } attach.setFileName(fileName); } ContentType type = new ContentType(mimePart.getContentType()); attach.setMimeType(type.getBaseType()); InputStream inputStream = mimePart.getDataHandler().getInputStream(); attach.buildMd5Checksum(inputStream); attach.buildPath(mimePart, baseStorePath); return attach; }
From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java
protected static String getFilename(Part p, String defaultFileName) throws MessagingException { String originalFilename = p.getFileName(); if (originalFilename == null || originalFilename.trim().length() == 0) { String filename = defaultFileName; // using default filename => add extension for this type if (p.isMimeType("text/plain")) { filename += ".txt"; } else if (p.isMimeType("text/html")) { filename += ".html"; }//w w w . j a v a 2 s. c o m return filename; } else { try { return MimeUtility.decodeText(originalFilename.trim()); } catch (UnsupportedEncodingException e) { return originalFilename.trim(); } } }
From source file:org.nuxeo.ecm.platform.mail.action.TransformMessageAction.java
/** * "javax.mail.internet.MimeBodyPart" is decoding the file name (with special characters) if it has the * "mail.mime.decodefilename" sysstem property set but the "com.sun.mail.imap.IMAPBodyPart" subclass of MimeBodyPart * is overriding getFileName() and never deal with encoded file names. the filename is decoded with the utility * function: MimeUtility.decodeText(filename); so we force here a filename decode. MimeUtility.decodeText is doing * nothing if the text is not encoded// w w w.ja v a 2 s . c o m */ private static String getFileName(Part mailPart) throws MessagingException { String sysPropertyVal = System.getProperty("mail.mime.decodefilename"); boolean decodeFileName = sysPropertyVal != null && !sysPropertyVal.equalsIgnoreCase("false"); String encodedFilename = mailPart.getFileName(); if (!decodeFileName || encodedFilename == null) { return encodedFilename; } try { return MimeUtility.decodeText(encodedFilename); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Can't decode attachment filename.", ex); } }
From source file:org.nuxeo.ecm.platform.mail.action.TransformMessageAction.java
private static String safelyDecodeText(String textToDecode) { try {// w ww. j a v a2s .c om return MimeUtility.decodeText(textToDecode); } catch (UnsupportedEncodingException ex) { log.error("Can't decode text. Use undecoded!", ex); return textToDecode; } }
From source file:org.ossmeter.platform.communicationchannel.nntp.local.NntpUtil.java
protected static String decodeSubject(String subject) { if (subject.contains("=?utf-8?")) { System.err.println("D: subject:\t" + subject); String decodedSubject = ""; for (String str : subject.replace("=?utf-8?", " =?utf-8?").split("\\s+")) try { decodedSubject += MimeUtility.decodeText(str); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace();/*from w ww. ja v a 2s. co m*/ } System.err.println("D: decoded:\t" + decodedSubject); return decodedSubject; } else return subject; }
From source file:org.ossmeter.platform.communicationchannel.nntp.NntpUtil.java
protected static String decodeSubject(String subject) { if (subject.contains("=?utf-8?")) { // System.err.println("D: subject:\t" + subject); String decodedSubject = ""; for (String str : subject.replace("=?utf-8?", " =?utf-8?").split("\\s+")) try { decodedSubject += MimeUtility.decodeText(str); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace();/*ww w. ja v a2s . c o m*/ } // System.err.println("D: decoded:\t" + decodedSubject); return decodedSubject; } else return subject; }
From source file:org.pentaho.di.job.entries.getpop.MailConnection.java
private void handlePart(String foldername, Part part, Pattern pattern) throws KettleException { try {//from www . ja v a2 s . c om String disposition = part.getDisposition(); // The RFC2183 doesn't REQUIRE Content-Disposition header field so we'll create one to // fake out the code below. if (disposition == null || disposition.length() < 1) { disposition = Part.ATTACHMENT; } if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) { String MimeText = null; try { MimeText = MimeUtility.decodeText(part.getFileName()); } catch (Exception e) { // Ignore errors } if (MimeText != null) { String filename = MimeUtility.decodeText(part.getFileName()); if (isWildcardMatch(filename, pattern)) { // Save file saveFile(foldername, filename, part.getInputStream()); updateSavedAttachedFilesCounter(); if (log.isDetailed()) { log.logDetailed(BaseMessages.getString(PKG, "JobGetMailsFromPOP.AttachedFileSaved", filename, "" + getMessage().getMessageNumber(), foldername)); } } } } } catch (Exception e) { throw new KettleException(e); } }
From source file:org.pentaho.di.job.entries.getpop.MailConnection.java
public int getAttachedFilesCount(Message message, Pattern pattern) throws KettleException { Object content = null;//from ww w .j ava 2 s . c o m int retval = 0; try { content = message.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0, n = multipart.getCount(); i < n; i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); if ((disposition != null) && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) { String MimeText = null; try { MimeText = MimeUtility.decodeText(part.getFileName()); } catch (Exception e) { // Ignore errors } if (MimeText != null) { String filename = MimeUtility.decodeText(part.getFileName()); if (isWildcardMatch(filename, pattern)) { retval++; } } } } } } catch (Exception e) { throw new KettleException(BaseMessages.getString(PKG, "MailConnection.Error.CountingAttachedFiles", "" + this.message.getMessageNumber()), e); } finally { if (content != null) { content = null; } } return retval; }
From source file:org.sakaiproject.james.SakaiMailet.java
/** * Breaks email messages into parts which can be saved as files (saves as attachments) or viewed as plain text (added to body of message). * //w w w . jav a2 s . c o m * @param siteId * Site associated with attachments, if any * @param p * The message-part embedded in a message.. * @param id * The string containing the message's id. * @param bodyBuf * The string-buffers in which the plain/text and/or html/text message body is being built. * @param bodyContentType * The value of the Content-Type header for the mesage body. * @param attachments * The ReferenceVector in which references to attachments are collected. * @param embedCount * An Integer that counts embedded messages (outer message is zero). * @return Value of embedCount (updated if this call processed any embedded messages). */ protected Integer parseParts(String siteId, Part p, String id, StringBuilder bodyBuf[], StringBuilder bodyContentType, List attachments, Integer embedCount) throws MessagingException, IOException { // increment embedded message counter if (p instanceof Message) { embedCount = Integer.valueOf(embedCount.intValue() + 1); } String type = p.getContentType(); // discard if content-type is unknown if (type == null || "".equals(type)) { M_log.warn(this + " message with unknown content-type discarded"); } // add plain text to bodyBuf[0] else if (p.isMimeType("text/plain") && p.getFileName() == null) { Object o = null; // let them convert to text if possible // but if bad encaps get the stream and do it ourselves try { o = p.getContent(); } catch (java.io.UnsupportedEncodingException ignore) { o = p.getInputStream(); } String txt = null; String innerContentType = p.getContentType(); if (o instanceof String) { txt = (String) p.getContent(); if (bodyContentType != null && bodyContentType.length() == 0) bodyContentType.append(innerContentType); } else if (o instanceof InputStream) { InputStream in = (InputStream) o; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[in.available()]; for (int len = in.read(buf); len != -1; len = in.read(buf)) out.write(buf, 0, len); String charset = (new ContentType(innerContentType)).getParameter("charset"); // RFC 2045 says if no char set specified use US-ASCII. // If specified but illegal that's less clear. The common case is X-UNKNOWN. // my sense is that UTF-8 is most likely these days but the sample we got // was actually ISO 8859-1. Could also justify using US-ASCII. Duh... if (charset == null) charset = "us-ascii"; try { txt = out.toString(MimeUtility.javaCharset(charset)); } catch (java.io.UnsupportedEncodingException ignore) { txt = out.toString("UTF-8"); } if (bodyContentType != null && bodyContentType.length() == 0) bodyContentType.append(innerContentType); } // remove extra line breaks added by mac Mail, perhaps others // characterized by a space followed by a line break if (txt != null) { txt = txt.replaceAll(" \n", " "); } // make sure previous message parts ended with newline if (bodyBuf[0].length() > 0 && bodyBuf[0].charAt(bodyBuf[0].length() - 1) != '\n') bodyBuf[0].append("\n"); bodyBuf[0].append(txt); } // add html text to bodyBuf[1] else if (p.isMimeType("text/html") && p.getFileName() == null) { Object o = null; // let them convert to text if possible // but if bad encaps get the stream and do it ourselves try { o = p.getContent(); } catch (java.io.UnsupportedEncodingException ignore) { o = p.getInputStream(); } String txt = null; String innerContentType = p.getContentType(); if (o instanceof String) { txt = (String) p.getContent(); if (bodyContentType != null && bodyContentType.length() == 0) bodyContentType.append(innerContentType); } else if (o instanceof InputStream) { InputStream in = (InputStream) o; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[in.available()]; for (int len = in.read(buf); len != -1; len = in.read(buf)) out.write(buf, 0, len); String charset = (new ContentType(innerContentType)).getParameter("charset"); if (charset == null) charset = "us-ascii"; try { txt = out.toString(MimeUtility.javaCharset(charset)); } catch (java.io.UnsupportedEncodingException ignore) { txt = out.toString("UTF-8"); } if (bodyContentType != null && bodyContentType.length() == 0) bodyContentType.append(innerContentType); } // remove bad image tags and naughty javascript if (txt != null) { txt = Web.cleanHtml(txt); } bodyBuf[1].append(txt); } // process subparts of multiparts else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { embedCount = parseParts(siteId, mp.getBodyPart(i), id, bodyBuf, bodyContentType, attachments, embedCount); } } // Discard parts with mime-type application/applefile. If an e-mail message contains an attachment is sent from // a macintosh, you may get two parts, one for the data fork and one for the resource fork. The part that // corresponds to the resource fork confuses users, this has mime-type application/applefile. The best thing // is to discard it. else if (p.isMimeType("application/applefile")) { M_log.warn(this + " message with application/applefile discarded"); } // discard enriched text version of the message. // Sakai only uses the plain/text or html/text version of the message. else if (p.isMimeType("text/enriched") && p.getFileName() == null) { M_log.warn(this + " message with text/enriched discarded"); } // everything else gets treated as an attachment else { String name = p.getFileName(); // look for filenames not parsed by getFileName() if (name == null && type.indexOf(NAME_PREFIX) != -1) { name = type.substring(type.indexOf(NAME_PREFIX) + NAME_PREFIX.length()); } // ContentType can't handle filenames with spaces or UTF8 characters if (name != null) { String decodedName = MimeUtility.decodeText(name); // first decode RFC 2047 type = type.replace(name, URLEncoder.encode(decodedName, "UTF-8")); name = decodedName; } ContentType cType = new ContentType(type); String disposition = p.getDisposition(); int approxSize = p.getSize(); if (name == null) { name = "unknown"; // if file's parent is multipart/alternative, // provide a better name for the file if (p instanceof BodyPart) { Multipart parent = ((BodyPart) p).getParent(); if (parent != null) { String pType = parent.getContentType(); ContentType pcType = new ContentType(pType); if (pcType.getBaseType().equalsIgnoreCase("multipart/alternative")) { name = "message" + embedCount; } } } if (p.isMimeType("text/html")) { name += ".html"; } else if (p.isMimeType("text/richtext")) { name += ".rtx"; } else if (p.isMimeType("text/rtf")) { name += ".rtf"; } else if (p.isMimeType("text/enriched")) { name += ".etf"; } else if (p.isMimeType("text/plain")) { name += ".txt"; } else if (p.isMimeType("text/xml")) { name += ".xml"; } else if (p.isMimeType("message/rfc822")) { name += ".txt"; } } // read the attachments bytes, and create it as an attachment in content hosting byte[] bodyBytes = readBody(approxSize, p.getInputStream()); if ((bodyBytes != null) && (bodyBytes.length > 0)) { // can we ignore the attachment it it's just whitespace chars?? Reference attachment = createAttachment(siteId, attachments, cType.getBaseType(), name, bodyBytes, id); // add plain/text attachment reference (if plain/text message) if (attachment != null && bodyBuf[0].length() > 0) bodyBuf[0] .append("[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]\n\n"); // add html/text attachment reference (if html/text message) if (attachment != null && bodyBuf[1].length() > 0) bodyBuf[1].append( "<p>[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]</p>"); // add plain/text attachment reference (if no plain/text and no html/text) if (attachment != null && bodyBuf[0].length() == 0 && bodyBuf[1].length() == 0) bodyBuf[0] .append("[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]\n\n"); } } return embedCount; }