james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject cvs commit: jakarta-james/src/java/org/apache/james/transport/mailets AddFooter.java
Date Sun, 12 Aug 2001 06:14:56 GMT
serge       01/08/11 23:14:56

  Modified:    src/java/org/apache/james/transport/mailets AddFooter.java
  Log:
  Supports multipart messages now (alternative and attachments)
  
  Revision  Changes    Path
  1.2       +68 -6     jakarta-james/src/java/org/apache/james/transport/mailets/AddFooter.java
  
  Index: AddFooter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/AddFooter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AddFooter.java	2001/08/06 03:45:40	1.1
  +++ AddFooter.java	2001/08/12 06:14:56	1.2
  @@ -38,14 +38,27 @@
               MimeMessage message = mail.getMessage();
   
               //I want to modify the right message body
  -            String contentType = message.getContentType();
  -            if (contentType == null || contentType.startsWith("text/plain")) {
  +            if (message.isMimeType("text/plain")) {
                   //This is a straight text message... just append the single part normally
  -                String content = message.getContent().toString();
  -                content += getFooterText();
  -                message.setText(content);
  +                addToText(message);
  +            } else if (message.isMimeType("multipart/mixed")) {
  +                //Find the first body part, and determine what to do then.
  +                MimeMultipart multipart = (MimeMultipart)message.getContent();
  +                MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(0);
  +                attachFooter(part);
  +                //We have to do this because of a bug in JavaMail (ref id 4404733)
  +                message.setContent(multipart);
  +            } else if (message.isMimeType("multipart/alternative")) {
  +                //Find the HTML and text message types and add to each
  +                MimeMultipart multipart = (MimeMultipart)message.getContent();
  +                for (int i = 0; i < multipart.getCount(); i++) {
  +                    MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(i);
  +                    attachFooter(part);
  +                }
  +                //We have to do this because of a bug in JavaMail (ref id 4404733)
  +                message.setContent(multipart);
               } else {
  -                //System.err.println(message.getContentType());
  +                //Give up... we won't attach the footer to this message
               }
           } catch (IOException ioe) {
               throw new MessagingException("Could not read message", ioe);
  @@ -60,7 +73,56 @@
           return text;
       }
   
  +    /**
  +     * This is exposed as a method for easy subclassing to provide alternate ways
  +     * to get the footer text.  By default, this will take the footer text,
  +     * converting the linefeeds to &lt;br&gt; tags.
  +     */
  +    public String getFooterHTML() {
  +        String text = getFooterText();
  +        StringBuffer sb = new StringBuffer();
  +        StringTokenizer st = new StringTokenizer(text, "\r\n", true);
  +        while (st.hasMoreTokens()) {
  +            String token = st.nextToken();
  +            if (token.equals("\r")) {
  +                continue;
  +            }
  +            if (token.equals("\n")) {
  +                sb.append("<br />\n");
  +            } else {
  +                sb.append(token);
  +            }
  +        }
  +        return sb.toString();
  +    }
  +
       public String getMailetInfo() {
           return "AddFooter Mailet";
  +    }
  +
  +    protected void addToText(MimePart part) throws MessagingException, IOException {
  +        String content = part.getContent().toString();
  +        if (!content.endsWith("\n")) {
  +            content += "\r\n";
  +        }
  +        content += getFooterText();
  +        part.setText(content);
  +    }
  +
  +    protected void addToHTML(MimePart part) throws MessagingException, IOException {
  +        String content = part.getContent().toString();
  +        content += "<br>" + getFooterHTML();
  +        part.setContent(content, part.getContentType());
  +    }
  +
  +    protected void attachFooter(MimePart part) throws MessagingException, IOException {
  +        if (part.isMimeType("text/plain")) {
  +            addToText(part);
  +        } else if (part.isMimeType("text/html")) {
  +            addToHTML(part);
  +        } else {
  +            System.err.println(part.getContentType());
  +        }
  +        //Give up... we won't attach the footer to this message
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org


Mime
View raw message