📄 simplemessageattributes.java
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation. * * All rights reserved. * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You * * may obtain a copy of the License at: * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * * implied. See the License for the specific language governing * * permissions and limitations under the License. * ***********************************************************************/package org.apache.james.imapserver.store;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.james.util.RFC822DateFormat;import org.apache.mailet.MailAddress;import javax.mail.BodyPart;import javax.mail.MessagingException;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimePart;import javax.mail.internet.ParseException;import java.util.ArrayList;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;/** * Attributes of a Message in IMAP4rev1 style. Message * Attributes should be set when a message enters a mailbox. * <p> Note that the message in a mailbox have the same order using either * Message Sequence Numbers or UIDs. * <p> reinitialize() must be called on deserialization to reset Logger * * Reference: RFC 2060 - para 2.3 * @version 0.2 on 04 Aug 2002 */public class SimpleMessageAttributes extends AbstractLogEnabled implements ImapMessageAttributes{ private final static String SP = " "; private final static String NIL = "NIL"; private final static String Q = "\""; private final static String LB = "("; private final static String RB = ")"; private final static boolean DEBUG = false; private final static String MULTIPART = "MULTIPART"; private final static String MESSAGE = "MESSAGE"; private int uid; private int messageSequenceNumber; private Date internalDate; private String internalDateString; private String bodyStructure; private String envelope; private int size; private int lineCount; public ImapMessageAttributes[] parts; private List headers; //rfc822 or MIME header fields //arrays only if multiple values allowed under rfc822 private String subject; private String[] from; private String[] sender; private String[] replyTo; private String[] to; private String[] cc; private String[] bcc; private String[] inReplyTo; private String[] date; private String[] messageID; private String contentType; private String primaryType; // parsed from contentType private String secondaryType; // parsed from contentType private Set parameters; // parsed from contentType private String contentID; private String contentDesc; private String contentEncoding; SimpleMessageAttributes() { } void setAttributesFor(MimeMessage msg) throws MessagingException { size = msg.getSize(); try { internalDate = msg.getSentDate(); } catch (MessagingException me) { internalDate = new Date(); } internalDateString = RFC822DateFormat.toString(internalDate); // not right format parseMimePart(msg); envelope = null; bodyStructure = null; } void setUID(int thisUID) { uid = thisUID; } /** * Parses key data items from a MimeMessage for seperate storage. * TODO this is a mess, and should be completely revamped. */ void parseMimePart(MimePart part) { // Section 1 - Message Headers if (part instanceof MimeMessage) { try { subject = ((MimeMessage)part).getSubject(); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getSubject: " + me); } } try { from = part.getHeader("From"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(From): " + me); } try { sender = part.getHeader("Sender"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Sender): " + me); } try { replyTo = part.getHeader("Reply To"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Reply To): " + me); } try { to = part.getHeader("To"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { cc = part.getHeader("Cc"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { bcc = part.getHeader("Bcc"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { inReplyTo = part.getHeader("In Reply To"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(In Reply To): " + me); } try { date = part.getHeader("Date"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Date): " + me); } try { messageID = part.getHeader("Message-ID"); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(messageID): " + me); } String contentTypeLine = null; try { contentTypeLine = part.getContentType(); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getContentType(): " + me); } if (contentTypeLine !=null ) { decodeContentType(contentTypeLine); } try { contentID = part.getContentID(); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getContentUD(): " + me); } try { contentDesc = part.getDescription(); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getDescription(): " + me); } try { contentEncoding = part.getEncoding(); // default value. if ( contentEncoding == null ) { contentEncoding = "7BIT"; } } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getEncoding(): " + me); } if (DEBUG) { try { String contentDisposition = part.getDisposition(); } catch (MessagingException me) { getLogger().debug("Messaging Exception for getEncoding(): " + me); } } try { // TODO this doesn't work lineCount = part.getLineCount(); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getLineCount(): " + me); } catch (Exception e) { if (DEBUG) getLogger().debug("Exception for getLineCount(): " + e); } // Recurse through any embedded parts if (primaryType.equalsIgnoreCase(MULTIPART)) { MimeMultipart container; try { container =(MimeMultipart) part.getContent(); int count = container.getCount(); parts = new SimpleMessageAttributes[count]; for (int i = 0; i < count ; i ++) { BodyPart nextPart = container.getBodyPart(i); if (nextPart instanceof MimePart) { SimpleMessageAttributes partAttrs = new SimpleMessageAttributes(); setupLogger(partAttrs); // reset transient logger partAttrs.parseMimePart((MimePart)nextPart); parts[i] = partAttrs; } else { getLogger().info("Found a non-Mime bodyPart"); } } } catch (Exception e) { getLogger().debug("Messaging Exception for getContent(): " + e); e.printStackTrace(); } } else if (primaryType.equalsIgnoreCase("message")) { getLogger().info("This part contains an embedded message of subtype: " + secondaryType); getLogger().info("Uses java class: " + part.getClass().getName()); if (secondaryType.equalsIgnoreCase("RFC822")) { //try { /* MimeMessageWrapper message = new MimeMessageWrapper(part.getInputStream()); SimpleMessageAttributes msgAttrs = new SimpleMessageAttributes(); msgAttrs.setAttributesFor(message); if (part instanceof MimeMessage) { Comments out because I don't know what it should do here MimeMessage msg1 = (MimeMessage) part; MimeMessageWrapper message2 = new MimeMessageWrapper(msg1); SimpleMessageAttributes msgAttrs2 = new SimpleMessageAttributes(); msgAttrs.setAttributesFor(message2); } parts = new SimpleMessageAttributes[1]; parts[0] = msgAttrs; */ //} catch (Exception e) { //getLogger().error("Error interpreting a message/rfc822: " + e); //e.printStackTrace(); //} } else { getLogger().info("Unknown subtype of message encountered."); System.out.println("Unknown subtype of message encountered."); } } else { System.out.println("parseMimePart: its just a plain message"); } } /** * Builds IMAP envelope String from pre-parsed data. */ String parseEnvelope() { List response = new ArrayList(); response.add( LB + Q + internalDateString + Q + SP); if (subject != null && (!subject.equals(""))) { response.add( Q + subject + Q + SP ); } else { response.add( NIL + SP ); } if (from != null && from.length > 0) { response.add(LB); for (int i=0; i<from.length; i++) { response.add(parseAddress( from[i]) ); } response.add(RB); } else { response.add( NIL); } response.add(SP); if (sender != null && sender.length >0) { if (DEBUG) getLogger().debug("parsingEnvelope - sender[0] is: " + sender[0]); //Check for Netscape feature - sender is local part only if (sender[0].indexOf("@") == -1) { response.add(LB + (String)response.get(3) + RB); //first From address } else { response.add(LB); for (int i=0; i<sender.length; i++) { response.add( parseAddress(sender[i])); } response.add(RB); } } else { if (from != null && from.length > 0) { response.add(LB + (String)response.get(3) + RB); //first From address } else { response.add( NIL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -