private Message replyInternal()

in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/MimeMessage.java [1045:1142]


    private Message replyInternal(final boolean replyToAll, final boolean setOriginalAnswered) throws MessagingException {
        // create a new message in this session.
        final MimeMessage reply = createMimeMessage(session);

        // get the header and add the "Re:" bit, if necessary.
        String newSubject = getSubject();
        if (newSubject != null) {
            // check to see if it already begins with "Re: " (in any case).
            // Add one on if we don't have it yet.
            if (!newSubject.regionMatches(true, 0, "Re: ", 0, 4)) {
                newSubject = "Re: " + newSubject;
            }
            reply.setSubject(newSubject);
        }
        
        // if this message has a message ID, then add a In-Reply-To and References
        // header to the reply message 
        final String messageID = getSingleHeader("Message-ID"); 
        if (messageID != null) {
            // this one is just set unconditionally 
            reply.setHeader("In-Reply-To", messageID); 
            // we might already have a references header.  If so, then add our message id 
            // on the the end
            String references = getSingleHeader("References"); 
            if (references == null) {
                references = messageID; 
            }
            else {
                references = references + " " + messageID; 
            }
            // and this is a replacement for whatever might be there.              
            reply.setHeader("References", MimeUtility.fold("References: ".length(), references)); 
        }

        // set the target recipients the replyTo value
        reply.setRecipients(Message.RecipientType.TO, getReplyTo());

        // need to reply to everybody?  More things to add.
        if (replyToAll) {
            // when replying, we want to remove "duplicates" in the final list.

            final HashMap masterList = new HashMap();

            // reply to all implies add the local sender.  Add this to the list if resolveable.
            final InternetAddress localMail = InternetAddress.getLocalAddress(session);
            if (localMail != null) {
                masterList.put(localMail.getAddress(), localMail);
            }
            // see if we have some local aliases to deal with.
            final String alternates = session.getProperty(MAIL_ALTERNATES);
            if (alternates != null) {
                // parse this string list and merge with our set.
                final Address[] alternateList = InternetAddress.parse(alternates, false);
                mergeAddressList(masterList, alternateList);
            }

            // the master list now contains an a list of addresses we will exclude from
            // the addresses.  From this point on, we're going to prune any additional addresses
            // against this list, AND add any new addresses to the list

            // now merge in the main recipients, and merge in the other recipents as well
            Address[] toList = pruneAddresses(masterList, getRecipients(Message.RecipientType.TO));
            if (toList.length != 0) {
                // now check to see what sort of reply we've been asked to send.
                // if replying to all as a CC, then we need to add to the CC list, otherwise they are
                // TO recipients.
                if (SessionUtil.getBooleanProperty(session, MAIL_REPLYALLCC, false)) {
                    reply.addRecipients(Message.RecipientType.CC, toList);
                }
                else {
                    reply.addRecipients(Message.RecipientType.TO, toList);
                }
            }
            // and repeat for the CC list.
            toList = pruneAddresses(masterList, getRecipients(Message.RecipientType.CC));
            if (toList.length != 0) {
                reply.addRecipients(Message.RecipientType.CC, toList);
            }

            // a news group list is separate from the normal addresses.  We just take these recepients
            // asis without trying to prune duplicates.
            toList = getRecipients(RecipientType.NEWSGROUPS);
            if (toList != null && toList.length != 0) {
                reply.addRecipients(RecipientType.NEWSGROUPS, toList);
            }
        }

        // this is a bit of a pain.  We can't set the flags here by specifying the system flag, we need to
        // construct a flag item instance inorder to set it.

        // this is an answered email.
        if(setOriginalAnswered) {
            setFlags(new Flags(Flags.Flag.ANSWERED), true);
        }
        
        // all done, return the constructed Message object.
        return reply;
    }