KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > AttachmentFileNameIs


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17  
18 package org.apache.james.transport.matchers;
19
20 import org.apache.mailet.GenericMatcher;
21 import org.apache.mailet.Mail;
22
23 import javax.mail.MessagingException JavaDoc;
24 import javax.mail.Multipart JavaDoc;
25 import javax.mail.Part JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Locale JavaDoc;
32
33
34 /**
35  * <P>Checks if at least one attachment has a file name which matches any
36  * element of a comma-separated list of file name masks.
37  * The match is case insensitive.</P>
38  * <P>File name masks may start with a wildcard '*'.</P>
39  * <P>Multiple file name masks can be specified, e.g.: '*.scr,*.bat'.</P>
40  *
41  * @version CVS $Revision: 1.1.2.4 $ $Date: 2004/03/15 03:54:21 $
42  * @since 2.2.0
43  */

44 public class AttachmentFileNameIs extends GenericMatcher {
45     /**
46      * represents a single parsed file name mask
47      */

48     private static class Mask {
49         /** true if the mask starts with a wildcard asterisk */
50         public boolean suffixMatch;
51         
52         /** file name mask not including the wildcard asterisk */
53         public String JavaDoc matchString;
54     }
55     
56     /** contains ParsedMask instances, setup by init */
57     private Mask[] masks = null;
58     
59     /** parses the condition */
60     public void init() throws MessagingException JavaDoc {
61         /** sets up fileNameMasks variable by parsing the condition */
62         
63         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getCondition(), ", ", false);
64         ArrayList JavaDoc theMasks = new ArrayList JavaDoc(20);
65         while (st.hasMoreTokens()) {
66             Mask mask = new Mask();
67             String JavaDoc fileName = st.nextToken();
68             if (fileName.startsWith("*")) {
69                 mask.suffixMatch = true;
70                 mask.matchString = fileName.substring(1);
71             } else {
72                 mask.suffixMatch = false;
73                 mask.matchString = fileName;
74             }
75             mask.matchString = mask.matchString.toLowerCase(Locale.US);
76             mask.matchString = mask.matchString.trim();
77             theMasks.add(mask);
78         }
79         masks = (Mask[])theMasks.toArray(new Mask[0]);
80     }
81
82     /**
83      * Either every recipient is matching or neither of them.
84      * @throws MessagingException if no matching attachment is found and at least one exception was thrown
85      */

86     public Collection JavaDoc match(Mail mail) throws MessagingException JavaDoc {
87         
88         Exception JavaDoc anException = null;
89         
90         try {
91             MimeMessage JavaDoc message = mail.getMessage();
92             Object JavaDoc content;
93             
94             /**
95              * if there is an attachment and no inline text,
96              * the content type can be anything
97              */

98             if (message.getContentType() == null) {
99                 return null;
100             }
101             
102             content = message.getContent();
103             if (content instanceof Multipart JavaDoc) {
104                 Multipart JavaDoc multipart = (Multipart JavaDoc) content;
105                 for (int i = 0; i < multipart.getCount(); i++) {
106                     try {
107                         Part JavaDoc part = multipart.getBodyPart(i);
108                         String JavaDoc fileName = part.getFileName();
109                         if (fileName != null && matchFound(fileName)) {
110                             return mail.getRecipients(); // matching file found
111
}
112                     } catch (MessagingException JavaDoc e) {
113                         anException = e;
114                     } // ignore any messaging exception and process next bodypart
115
}
116             } else {
117                 String JavaDoc fileName = message.getFileName();
118                 if (fileName != null && matchFound(fileName)) {
119                     return mail.getRecipients(); // matching file found
120
}
121             }
122         } catch (Exception JavaDoc e) {
123             anException = e;
124         }
125         
126         // if no matching attachment was found and at least one exception was catched rethrow it up
127
if (anException != null) {
128             throw new MessagingException JavaDoc("Malformed message", anException);
129         }
130         
131         return null; // no matching attachment found
132
}
133     
134     /*
135      * Checks if <I>fileName</I> matches with at least one of the <CODE>masks</CODE>.
136      */

137     private boolean matchFound(String JavaDoc fileName) {
138         fileName = fileName.toLowerCase(Locale.US);
139         fileName = fileName.trim();
140             
141         for (int j = 0; j < masks.length; j++) {
142             boolean fMatch;
143             Mask mask = masks[j];
144             
145             //XXX: file names in mail may contain directory - theoretically
146
if (mask.suffixMatch) {
147                 fMatch = fileName.endsWith(mask.matchString);
148             } else {
149                 fMatch = fileName.equals(mask.matchString);
150             }
151             if (fMatch) return true; // matching file found
152
}
153         return false;
154     }
155 }
156
157
Popular Tags