View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.transport.matchers;
23  
24  import org.apache.mailet.base.GenericMatcher;
25  import org.apache.mailet.Mail;
26  
27  import javax.mail.MessagingException;
28  import javax.mail.Multipart;
29  import javax.mail.Part;
30  import javax.mail.internet.MimeMessage;
31  import java.util.Collection;
32  
33  /**
34   * Checks whether this message has an attachment
35   *
36   * @version CVS $Revision: 713949 $ $Date: 2008-11-14 07:40:21 +0000 (Fri, 14 Nov 2008) $
37   */
38  public class HasAttachment extends GenericMatcher {
39  
40      /** 
41       * Either every recipient is matching or neither of them.
42       * @throws MessagingException if no attachment is found and at least one exception was thrown
43       */
44      public Collection match(Mail mail) throws MessagingException {
45          
46          Exception anException = null;
47          
48          try {
49              MimeMessage message = mail.getMessage();
50              Object content;
51              
52              /**
53               * if there is an attachment and no inline text,
54               * the content type can be anything
55               */
56              if (message.getContentType() == null) {
57                  return null;
58              }
59              
60              content = message.getContent();
61              if (content instanceof Multipart) {
62                  Multipart multipart = (Multipart) content;
63                  for (int i = 0; i < multipart.getCount(); i++) {
64                      try {
65                          Part part = multipart.getBodyPart(i);
66                          String fileName = part.getFileName();
67                          if (fileName != null) {
68                              return mail.getRecipients(); // file found
69                          }
70                      } catch (MessagingException e) {
71                          anException = e;
72                      } // ignore any messaging exception and process next bodypart
73                  }
74              } else {
75                  String fileName = message.getFileName();
76                  if (fileName != null) {
77                      return mail.getRecipients(); // file found
78                  }
79              }
80          } catch (Exception e) {
81              anException = e;
82          }
83          
84          // if no attachment was found and at least one exception was catched rethrow it up
85          if (anException != null) {
86              throw new MessagingException("Malformed message", anException);
87          }
88          
89          return null; // no attachment found
90      }
91  }