javamailgateway.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,639 行 · 第 1/5 页
JAVA
1,639 行
* * @param filter the filter to apply the hack to. */ protected void shortTermQueryCacheHack(ResultFilter filter) { // workaround for shortTerm Query cache caching resultFilter queries // when we can't have it doing that if (DbForumFactory.getInstance().cacheManager.isShortTermQueryCacheEnabled()) { filter.setCreationDateRangeMax(new Date()); } } /** * Utility method to check to see if a Message object contains * attachments or not. * * @return True if the message has attachments, false otherwise. * @throws MessagingException If the error occurs parsing the * message object. */ protected boolean isMessageWithAttachments(Message message) throws MessagingException { try { // plain text message // search message content for uuencoded attachments if (message.isMimeType("text/plain") || message.isMimeType("text")) { BufferedReader reader = getTextReader(message); String line; try { while ((line = reader.readLine()) != null) { if (line.indexOf("begin 666 ") == 0 || line.indexOf("begin 664 ") == 0 || line.indexOf("begin 660 ") == 0 || line.indexOf("begin 644 ") == 0 || line.indexOf("begin 640 ") == 0 || line.indexOf("begin 600 ") == 0) { return true; } } } catch (sun.io.MalformedInputException mie) { System.err.println("Reading message body threw sun.io.MalformedInputException!!"); } finally { try { reader.close(); } catch (IOException ioe) { /* ignore */ } } } // not text, assume multipart or html, etc else { return true; } } catch (IOException e) { throw new MessagingException("IOException thrown", e); } return false; } /** * Adds attachments from a JavaMail Part object to a forum message * object.<p> * * This implementation doesn't attempt to figure out beforehand * if an attachment should be added by checking with an attachment * manager, rather it just catches any AttachmentException thrown * and reports it. * * @throws MessagingException if the error occurs getting an attachment * out of the message. * @throws UnauthorizedException if the error occurs adding an attachment * to the forumMessage. */ protected void addAttachments(ForumMessage forumMessage, Part part) throws MessagingException, UnauthorizedException { try { // html is always added as attachments if (part.isMimeType("text/html")) { String filename = part.getFileName(); if (filename == null) { filename = "att1.html"; } forumMessage.createAttachment(filename, "text/html", part.getInputStream()); } // if text with disposition of attachment, create attachment // otherwise, search text message content for uuencoded attachments // and add those as attachments else if (part.isMimeType("text/plain") || part.isMimeType("text")) { if (part.getDisposition() == null) { BufferedReader reader = getTextReader(part); addUUencodedAttachments(forumMessage, reader); } else if (part.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) { String filename = part.getFileName(); String ct = part.getContentType(); if (ct != null && ct.indexOf(';') != -1) { ct = ct.substring(0, ct.indexOf(';')).trim().toLowerCase(); } forumMessage.createAttachment(filename, ct, part.getInputStream()); } else { BufferedReader reader = getTextReader(part); addUUencodedAttachments(forumMessage, reader); } } // multipart - we loop through each part and recursively // determine what to do with it else if (part.isMimeType("multipart/*")) { try { Multipart mp = (Multipart) part.getContent(); int cnt = mp.getCount(); for (int j = 0; j < cnt; j++) { Part p = mp.getBodyPart(j); addAttachments(forumMessage, p); } } catch (IllegalArgumentException iae) { throw new MessagingException("Character set not recognized", iae); } } // not html, not text, not multipart, likely binary // determine from it's content-disposition what to do with it else { String ct = part.getContentType(); String disposition = part.getDisposition(); String filename = part.getFileName(); // first, clean up the content type if (ct != null && ct.indexOf(';') != -1) { ct = ct.substring(0, ct.indexOf(';')).trim().toLowerCase(); } if (disposition == null) { if (filename == null) { filename = "att1.dat"; } forumMessage.createAttachment(filename, ct, part.getInputStream()); } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) { if (filename == null) { filename = "att1.dat"; } forumMessage.createAttachment(filename, ct, part.getInputStream()); } else { // if the part has a filename, attach it no matter what the // disposition says if (filename != null) { forumMessage.createAttachment(filename, ct, part.getInputStream()); } else { BufferedReader reader = getTextReader(part); addUUencodedAttachments(forumMessage, reader); } } } } catch (IOException e) { throw new MessagingException("IOException thrown", e); } catch (IllegalStateException e) { // should never happen!! e.printStackTrace(); } catch (AttachmentException e) { if (e.getErrorType() == AttachmentException.TOO_LARGE) { System.err.println("Attachment rejected because it was too large"); } else if (e.getErrorType() == AttachmentException.TOO_MANY_ATTACHMENTS) { System.err.println("Attachment rejected because too many attachments " + "are already attached to the message"); } else if (e.getErrorType() == AttachmentException.BAD_CONTENT_TYPE) { System.err.println("Attachment rejected because the content type was " + "disallowed or unknown"); } else { System.err.println("General Attachment error " + e.getMessage()); } } } /** * Search for uuencoded attachments from a reader and attach them to a message. * * @param forumMessage the message to attach the uuencoded attachments to. * @param reader the reader to search for uuencoded attachments in. * @throws IOException * @throws IllegalStateException * @throws AttachmentException * @throws UnauthorizedException */ protected void addUUencodedAttachments(ForumMessage forumMessage, BufferedReader reader) throws IOException, IllegalStateException, AttachmentException, UnauthorizedException { String line = "", bufline = "", filename = ""; boolean found = false; StringBuffer sb = new StringBuffer(4096); try { while ((line = reader.readLine()) != null) { if (line.indexOf("begin 666 ") == 0 || line.indexOf("begin 664 ") == 0 || line.indexOf("begin 660 ") == 0 || line.indexOf("begin 644 ") == 0 || line.indexOf("begin 640 ") == 0 || line.indexOf("begin 600 ") == 0) { bufline = line; filename = line.substring(9).trim(); if (filename.charAt(0) == '"') { filename = filename.substring(1); } if (filename.charAt(filename.length()-1) == '"') { filename = filename.substring(0, filename.length()-1); } found = true; } else if (found && line.indexOf("end") == 0) { // workaround for bug in Sun's UUDecoderStream class // at least, as best as I can tell // ignore line just prior to the end sb.append(" \nend\n"); found = false; // get mimetype from filename MimetypesFileTypeMap typeMap = new MimetypesFileTypeMap(); String mimeType = typeMap.getContentType(filename); byte[] b = sb.toString().getBytes("ASCII"); ByteArrayInputStream bis = new ByteArrayInputStream(b); InputStream is = MimeUtility.decode(bis, "uuencode"); try { forumMessage.createAttachment(filename, mimeType, is); } finally { try { is.close(); } catch (IOException e) { /* ignore */ } try { bis.close(); } catch (IOException e) { /* ignore */ } sb = new StringBuffer(4096); } } else if (found) { sb.append(bufline).append("\n"); bufline = line; } } } catch (MessagingException e) { System.err.println("Adding uuencoded attachments threw MessagingException"); throw new IOException(); } catch (sun.io.MalformedInputException mie) { System.err.println("Adding uuencoded attachments threw sun.io.MalformedInputException"); throw new IOException(); } finally { try { reader.close(); } catch (IOException ioe) { /* ignore */ } } } /** * Search for uuencoded attachments from a reader and remove them appending * the remaining lines into the provided stringbuffer * * @param sb the StringBuffer to append text lines to. * @param reader the reader to search for uuencoded attachments in. * @throws IOException */ protected void removeUUencodedAttachments(StringBuffer sb, BufferedReader reader) throws IOException { boolean ignore = false; String line; try { while ((line = reader.readLine()) != null) { if (line.indexOf("begin 666 ") == 0 || line.indexOf("begin 664 ") == 0 || line.indexOf("begin 660 ") == 0 || line.indexOf("begin 644 ") == 0 || line.indexOf("begin 640 ") == 0 || line.indexOf("begin 600 ") == 0) { if (!attachmentsEnabled) { String filename = line.substring(9).trim(); if (filename.charAt(0) == '"') { filename = filename.substring(1); } if (filename.charAt(filename.length()-1) == '"') { filename = filename.substring(0, filename.length()-1); } sb.append("[").append(filename).append("]\n"); } ignore = true; } else if (ignore && line.indexOf("end") == 0) { ignore = false; } else if (!ignore) { sb.append(line).append("\n"); } } } catch (sun.io.MalformedInputException mie) { System.err.println("Removing uuencoded attachments threw sun.io.MalformedInputException"); throw new IOException(); } finally { try { reader.close(); } catch (IOException ioe) { /* ignore */ } } } /** * Returns the subject of a JavaMail message. *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?