📄 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;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.james.util.RFC822DateFormat;import org.apache.log.Logger;import org.apache.mailet.MailAddress;import javax.mail.BodyPart;import javax.mail.MessagingException;import javax.mail.internet.*;import java.io.Serializable;import java.util.*;/** * 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 MessageAttributes, Serializable { 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 = true; private final static String MULTIPART = "MULTIPART"; private final static String MESSAGE = "MESSAGE"; //Only available in first incarnation of object private transient Logger logger; 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 MessageAttributes[] 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() { System.out.println("SimpleMessageAttributes()"); // new Throwable().printStackTrace(); } void setAttributesFor(MimeMessage msg) throws MessagingException { System.out.println("setAttributesFor msg.class: " + msg.getClass().getName()); size = msg.getSize(); System.out.println("setAttributesFor - size: " + size); try { internalDate = msg.getSentDate(); if (DEBUG) getLogger().debug("setAttributesFor - getSentDate: " + internalDate); System.out.println("setAttributesFor - getSentDate: " + internalDate); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getSentDate: " + me); System.out.println("Messaging Exception for getSentDate: " + me); internalDate = new Date(); } //if (DEBUG) { getLogger().debug("HeaderLines received were: "); System.out.println("HeaderLines received were: "); Enumeration enum = msg.getAllHeaderLines(); while(enum.hasMoreElements()) { getLogger().debug((String)enum.nextElement()); } enum = msg.getAllHeaderLines(); while(enum.hasMoreElements()) { System.out.println((String)enum.nextElement()); } // getLogger().debug("Header objects available are:"); // Enumeration e = msg.getAllHeaders(); // while(e.hasMoreElements()) { //Header h = (Header) e.nextElement(); //getLogger().debug("Name: " + h.getName()); //getLogger().debug("Value: " + h.getValue()); // } // } internalDateString = RFC822DateFormat.toString(internalDate); // not right format System.out.println("setting msg: "+msg); parseMimePart(msg); envelope = null; bodyStructure = null; } void setUID(int thisUID) { uid = thisUID; } /** * Parses key data items from a MimeMessage for seperate storage. */ void parseMimePart(MimePart part) { // Section 1 - Message Headers System.out.println("parseMimePart("+part+")"); if (part instanceof MimeMessage) { try { subject = ((MimeMessage)part).getSubject(); if (DEBUG) getLogger().debug("parseMessage - subject: " + subject); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getSubject: " + me); } } try { from = part.getHeader("From"); if (DEBUG) getLogger().debug("parseMessage - from: " + from); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(From): " + me); } try { sender = part.getHeader("Sender"); if (DEBUG) getLogger().debug("parseMessage - sender: " + sender); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Sender): " + me); } try { replyTo = part.getHeader("Reply To"); if (DEBUG) getLogger().debug("parseMessage - ReplyTo: " + replyTo); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Reply To): " + me); } try { to = part.getHeader("To"); if (DEBUG) getLogger().debug("parseMessage - To: " + to); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { cc = part.getHeader("Cc"); if (DEBUG) getLogger().debug("parseMessage - cc: " + cc); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { bcc = part.getHeader("Bcc"); if (DEBUG) getLogger().debug("parseMessage - bcc: " + bcc); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me); } try { inReplyTo = part.getHeader("In Reply To"); if (DEBUG) getLogger().debug("parseMessage - In Reply To: " + inReplyTo); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(In Reply To): " + me); } try { date = part.getHeader("Date"); if (DEBUG) getLogger().debug("parseMessage - date: " + date); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Date): " + me); } try { messageID = part.getHeader("Message-ID"); if (DEBUG) getLogger().debug("parseMessage - messageID: " + messageID); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getHeader(messageID): " + me); } String contentTypeLine = null; try { contentTypeLine = part.getContentType(); if (DEBUG) getLogger().debug("parseMessage - contentType: " + contentTypeLine); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getContentType(): " + me); } if (contentTypeLine !=null ) { decodeContentType(contentTypeLine); } try { contentID = part.getContentID(); if (DEBUG) getLogger().debug("parseMessage - contentID: " + contentID); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getContentUD(): " + me); } try { contentDesc = part.getDescription(); if (DEBUG) getLogger().debug("parseMessage - contentDesc: " + contentDesc); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getDescription(): " + me); } try { contentEncoding = part.getEncoding(); if (DEBUG) getLogger().debug("parseMessage - contentEncoding: " + contentEncoding); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getEncoding(): " + me); } if (DEBUG) { try { String contentDisposition = part.getDisposition(); getLogger().debug("parseMessage - contentDisposition: " + contentEncoding); } catch (MessagingException me) { getLogger().debug("Messaging Exception for getEncoding(): " + me); } } try { lineCount = part.getLineCount(); if (DEBUG) getLogger().debug("parseMessage - Line Count: " + lineCount); System.out.println("parseMessage - Line Count: " + lineCount); } catch (MessagingException me) { if (DEBUG) getLogger().debug("Messaging Exception for getLineCount(): " + me); if (DEBUG) getLogger().debug(me.getMessage()); System.out.println("Messaging Exception for getLineCount(): " + me); System.out.println(me.getMessage()); } catch (Exception e) { if (DEBUG) getLogger().debug("Exception for getLineCount(): " + e); if (DEBUG) getLogger().debug("Exception message was: " + e.getMessage()); System.out.println("Exception for getLineCount(): " + e); System.out.println("Exception message was: " + e.getMessage()); e.printStackTrace(); } // Recurse through any embedded parts if (primaryType.equalsIgnoreCase(MULTIPART)) { MimeMultipart container; System.out.println("parseMimePart: its a MULTIPART"); try { container =(MimeMultipart) part.getContent(); int count = container.getCount(); getLogger().info("This part contains " + count + " parts."); System.out.println("This part contains " + count + " parts."); parts = new SimpleMessageAttributes[count]; for (int i = 0; i < count ; i ++) { getLogger().info("Getting embedded part: " + i); System.out.println("Getting embedded part: " + 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"); System.out.println("Found a non-Mime bodyPart"); } getLogger().info("Finished with embedded part: " + i); System.out.println("Finished with embedded part: " + i); } } catch (Exception e) { getLogger().debug("Messaging Exception for getContent(): " + e); System.out.println("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()); System.out.println("This part contains an embedded message of subtype: " + secondaryType); System.out.println("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."); } getLogger().info("Finished with embedded message. " ); System.out.println("Finished with embedded message. " ); } 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]); System.out.println("parsingEnvelope - sender[0] is: " + sender[0]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -