You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by vi...@apache.org on 2003/06/20 13:59:25 UTC

cvs commit: jakarta-james/src/java/org/apache/james/transport/matchers HasAttachment.java

vincenzo    2003/06/20 04:59:25

  Modified:    src/java/org/apache/james/transport/matchers Tag:
                        branch_2_1_fcs HasAttachment.java
  Log:
  HasAttachment was ignoring some attachments and reporting others  inexistent: if there was a message with no inline text and an attachment, was erroneously not matching; if the message had inline text with HTML format and no attachments, was erroneously matching.
  An overall catch now rethrows a MessagingException, consistently with the new "onException" control.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.2.4.3   +51 -8     jakarta-james/src/java/org/apache/james/transport/matchers/HasAttachment.java
  
  Index: HasAttachment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/matchers/HasAttachment.java,v
  retrieving revision 1.2.4.2
  retrieving revision 1.2.4.3
  diff -u -r1.2.4.2 -r1.2.4.3
  --- HasAttachment.java	8 Mar 2003 21:54:09 -0000	1.2.4.2
  +++ HasAttachment.java	20 Jun 2003 11:59:25 -0000	1.2.4.3
  @@ -62,24 +62,67 @@
   import org.apache.mailet.Mail;
   
   import javax.mail.MessagingException;
  +import javax.mail.Multipart;
  +import javax.mail.Part;
   import javax.mail.internet.MimeMessage;
   import java.util.Collection;
   
   /**
    * Checks whether this message has an attachment
    *
  - * @author  Serge Knystautas <se...@lokitech.com>
  + * @version CVS $Revision$ $Date$
    */
   public class HasAttachment extends GenericMatcher {
   
  +    /** 
  +     * Either every recipient is matching or neither of them.
  +     * @throws MessagingException if no attachment is found and at least one exception was thrown
  +     */
       public Collection match(Mail mail) throws MessagingException {
  -        MimeMessage message = mail.getMessage();
  -
  -        if (message.getContentType() != null &&
  -                message.getContentType().startsWith("multipart/mixed")) {
  -            return mail.getRecipients();
  -        } else {
  -            return null;
  +        
  +        Exception anException = null;
  +        
  +        try {
  +            MimeMessage message = mail.getMessage();
  +            Object content;
  +            
  +            /**
  +             * if there is an attachment and no inline text,
  +             * the content type can be anything
  +             */
  +            if (message.getContentType() == null) {
  +                return null;
  +            }
  +            
  +            content = message.getContent();
  +            if (content instanceof Multipart) {
  +                Multipart multipart = (Multipart) content;
  +                for (int i = 0; i < multipart.getCount(); i++) {
  +                    try {
  +                        Part part = multipart.getBodyPart(i);
  +                        String fileName = part.getFileName();
  +                        if (fileName != null) {
  +                            return mail.getRecipients(); // file found
  +                        }
  +                    } catch (MessagingException e) {
  +                        anException = e;
  +                    } // ignore any messaging exception and process next bodypart
  +                }
  +            } else {
  +                String fileName = message.getFileName();
  +                if (fileName != null) {
  +                    return mail.getRecipients(); // file found
  +                }
  +            }
  +        } catch (Exception e) {
  +            anException = e;
  +        }
  +        
  +        // if no attachment was found and at least one exception was catched rethrow it up
  +        if (anException != null) {
  +            throw new MessagingException("Malformed message", anException);
           }
  +        
  +        return null; // no attachment found
       }
   }
  
  
  

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