noel        2003/06/02 20:38:07

  Modified:    src/java/org/apache/james/transport/mailets Tag:
                        branch_2_1_fcs AbstractRedirect.java
  Log:
  New utility methods to get Headers and Body from MimeMessage.  Support inlining 
complex MimeMessages (fixes bug 18028).
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.3   +95 -11    
jakarta-james/src/java/org/apache/james/transport/mailets/Attic/AbstractRedirect.java
  
  Index: AbstractRedirect.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/Attic/AbstractRedirect.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- AbstractRedirect.java     3 Jun 2003 00:42:36 -0000       1.1.2.2
  +++ AbstractRedirect.java     3 Jun 2003 03:38:06 -0000       1.1.2.3
  @@ -937,6 +937,96 @@
       }
   
       /**
  +     * Utility method for obtaining a string representation of a
  +     * Message's headers
  +     */
  +    private String getMessageHeaders(MimeMessage message) throws MessagingException 
{
  +        Enumeration heads = message.getAllHeaderLines();
  +        StringBuffer headBuffer = new StringBuffer(1024);
  +        while(heads.hasMoreElements()) {
  +            headBuffer.append(heads.nextElement().toString()).append("\r\n");
  +        }
  +        return headBuffer.toString();
  +    }
  +
  +    /**
  +     * Utility method for obtaining a string representation of a
  +     * Message's body
  +     */
  +    private String getMessageBody(MimeMessage message) throws Exception {
  +        java.io.InputStream bis = null;
  +        java.io.OutputStream bos = null;
  +        java.io.ByteArrayOutputStream bodyOs = new java.io.ByteArrayOutputStream();
  +
  +        try {
  +            // Get the message as a stream.  This will encode
  +            // objects as necessary, and we have some overhead from
  +            // decoding and re-encoding the stream.  I'd prefer the
  +            // raw stream, but see the WARNING below.
  +            bos = javax.mail.internet.MimeUtility.encode(bodyOs, 
message.getEncoding());
  +            bis = message.getInputStream();
  +        } catch(javax.activation.UnsupportedDataTypeException udte) {
  +            /* If we get an UnsupportedDataTypeException try using
  +             * the raw input stream as a "best attempt" at rendering
  +             * a message.
  +             *
  +             * WARNING: JavaMail v1.3 getRawInputStream() returns
  +             * INVALID (unchanged) content for a changed message.
  +             * getInputStream() works properly, but in this case
  +             * has failed due to a missing DataHandler.
  +             *
  +             * MimeMessage.getRawInputStream() may throw a "no
  +             * content" MessagingException.  In JavaMail v1.3, when
  +             * you initially create a message using MimeMessage
  +             * APIs, there is no raw content available.
  +             * getInputStream() works, but getRawInputStream()
  +             * throws an exception.  If we catch that exception,
  +             * throw the UDTE.  It should mean that someone has
  +             * locally constructed a message part for which JavaMail
  +             * doesn't have a DataHandler.
  +             */
  +
  +            try {
  +                bis = message.getRawInputStream();
  +                bos = bodyOs;
  +            } catch(javax.mail.MessagingException _) {
  +                throw udte;
  +            }
  +        }
  +        catch(javax.mail.MessagingException me) {
  +            /* This could be another kind of MessagingException
  +             * thrown by MimeMessage.getInputStream(), such as a
  +             * javax.mail.internet.ParseException.
  +             *
  +             * The ParseException is precisely one of the reasons
  +             * why the getRawInputStream() method exists, so that we
  +             * can continue to stream the content, even if we cannot
  +             * handle it.  Again, if we get an exception, we throw
  +             * the one that caused us to call getRawInputStream().
  +             */
  +            try {
  +                bis = message.getRawInputStream();
  +                bos = bodyOs;
  +            } catch(javax.mail.MessagingException _) {
  +                throw me;
  +            }
  +        }
  +
  +        try {
  +            byte[] block = new byte[1024];
  +            int read = 0;
  +            while ((read = bis.read(block)) > -1) {
  +                bos.write(block, 0, read);
  +            }
  +            bos.flush();
  +            return bodyOs.toString();
  +        }
  +        finally {
  +            bis.close();             
  +        }
  +    }
  +
  +    /**
        * Builds the message of the newMail in case it has to be altered.
        *
        * @param originalMail the original Mail object
  @@ -948,13 +1038,7 @@
           
           StringWriter sout = new StringWriter();
           PrintWriter out   = new PrintWriter(sout, true);
  -        Enumeration heads = message.getAllHeaderLines();
  -        String head       = "";
  -        StringBuffer headBuffer = new StringBuffer(1024);
  -        while(heads.hasMoreElements()) {
  -            headBuffer.append(heads.nextElement().toString()).append("\r\n");
  -        }
  -        head = headBuffer.toString();
  +        String head = getMessageHeaders(message);
           boolean all = false;
           
           String messageText = getMessage(originalMail);
  @@ -974,7 +1058,7 @@
               case BODY: //BODY:
                   out.println("Message:");
                   try {
  -                    out.println(message.getContent().toString());
  +                    out.println(getMessageBody(message));
                   } catch(Exception e) {
                       out.println("body unavailable");
                   }
  @@ -1007,7 +1091,7 @@
                           break;
                       case BODY: //BODY:
                           try {
  -                            part.setText(message.getContent().toString());
  +                            part.setText(getMessageBody(message));
                           } catch(Exception e) {
                               part.setText("body unavailable");
                           }
  @@ -1016,8 +1100,8 @@
                           StringBuffer textBuffer =
                               new StringBuffer(1024)
                                   .append(head)
  -                                .append("\n\n")
  -                                .append(message.toString());
  +                                .append("r\n\r\n")
  +                                .append(getMessageBody(message));
                           part.setText(textBuffer.toString());
                           break;
                       case MESSAGE: //MESSAGE:
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to