FileDocCategorySizeDatePackage
AttachmentFileNameIs.javaAPI DocApache James 2.3.19452Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.matchers

AttachmentFileNameIs

public class AttachmentFileNameIs extends org.apache.mailet.GenericMatcher

Checks if at least one attachment has a file name which matches any element of a comma-separated or space-separated list of file name masks.

Syntax: match="AttachmentFileNameIs=[-d] [-z] masks"

The match is case insensitive.

File name masks may start with a wildcard '*'.

Multiple file name masks can be specified, e.g.: '*.scr,*.bat'.

If '-d' is coded, some debug info will be logged.

If '-z' is coded, the check will be non-recursively applied to the contents of any attached '*.zip' file.

version
CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
since
2.2.0

Fields Summary
protected static final String
UNZIP_REQUEST_PARAMETER
Unzip request parameter.
protected static final String
DEBUG_REQUEST_PARAMETER
Debug request parameter.
protected static final String
ZIP_SUFFIX
Match string for zip files.
protected boolean
isDebug
Controls certain log messages.
private Mask[]
masks
contains ParsedMask instances, setup by init
protected boolean
unzipIsRequested
True if unzip is requested.
Constructors Summary
Methods Summary
protected java.lang.StringcleanFileName(java.lang.String fileName)
Transforms fileName in a trimmed lowercase string usable for matching agains the masks.

        return fileName.toLowerCase(Locale.US).trim();
    
public voidinit()

    
         
        /* sets up fileNameMasks variable by parsing the condition */
        
        StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
        ArrayList theMasks = new ArrayList(20);
        while (st.hasMoreTokens()) {
            String fileName = st.nextToken();
            
            // check possible parameters at the beginning of the condition
            if (theMasks.size() == 0 && fileName.equalsIgnoreCase(UNZIP_REQUEST_PARAMETER)) {
                unzipIsRequested = true;
                log("zip file analysis requested");
                continue;
            }
            if (theMasks.size() == 0 && fileName.equalsIgnoreCase(DEBUG_REQUEST_PARAMETER)) {
                isDebug = true;
                log("debug requested");
                continue;
            }
            Mask mask = new Mask(); 
            if (fileName.startsWith("*")) {
                mask.suffixMatch = true;
                mask.matchString = fileName.substring(1);
            } else {
                mask.suffixMatch = false;
                mask.matchString = fileName;
            }
            mask.matchString = cleanFileName(mask.matchString);
            theMasks.add(mask);
        }
        masks = (Mask[])theMasks.toArray(new Mask[0]);
    
public java.util.Collectionmatch(org.apache.mailet.Mail mail)
Either every recipient is matching or neither of them.

throws
MessagingException if no matching attachment is found and at least one exception was thrown

        
        try {
            MimeMessage message = mail.getMessage();
            
            if (matchFound(message)) {
                return mail.getRecipients(); // matching file found
            } else {
                return null; // no matching attachment found
            }
            
        } catch (Exception e) {
            if (isDebug) {
                log("Malformed message", e);
            }
            throw new MessagingException("Malformed message", e);
        }
    
protected booleanmatchFound(javax.mail.Part part)
Checks if part matches with at least one of the masks.

        
        /*
         * if there is an attachment and no inline text,
         * the content type can be anything
         */
        
        if (part.getContentType() == null ||
            part.getContentType().startsWith("multipart/alternative")) {
            return false;
        }
        
        Object content;
        
        try {
            content = part.getContent();
        } catch (UnsupportedEncodingException uee) {
            // in this case it is not an attachment, so ignore it
            return false;
        }
        
        Exception anException = null;
        
        if (content instanceof Multipart) {
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                try {
                    Part bodyPart = multipart.getBodyPart(i);
                    if (matchFound(bodyPart)) {
                        return true; // matching file found
                    }
                } catch (MessagingException e) {
                    anException = e;
                } // remember any messaging exception and process next bodypart
            }
        } else {
            String fileName = part.getFileName();
            if (fileName != null) {
                fileName = cleanFileName(fileName);
                // check the file name
                if (matchFound(fileName)) {
                    if (isDebug) {
                        log("matched " + fileName);
                    }
                    return true;
                }
                if (unzipIsRequested && fileName.endsWith(ZIP_SUFFIX) && matchFoundInZip(part)){
                    return true;
                }
            }
        }
        
        // if no matching attachment was found and at least one exception was catched rethrow it up
        if (anException != null) {
            throw anException;
        }
        
        return false;
    
protected booleanmatchFound(java.lang.String fileName)
Checks if fileName matches with at least one of the masks.

        for (int j = 0; j < masks.length; j++) {
            boolean fMatch;
            Mask mask = masks[j];
            
            //XXX: file names in mail may contain directory - theoretically
            if (mask.suffixMatch) {
                fMatch = fileName.endsWith(mask.matchString);
            } else {
                fMatch = fileName.equals(mask.matchString);
            }
            if (fMatch) {
                return true; // matching file found
            }
        }
        return false;
    
protected booleanmatchFoundInZip(javax.mail.Part part)
Checks if part is a zip containing a file that matches with at least one of the masks.

        ZipInputStream zis = new ZipInputStream(part.getInputStream());
        
        try {
            while (true) {
                ZipEntry zipEntry = zis.getNextEntry();
                if (zipEntry == null) {
                    break;
                }
                String fileName = zipEntry.getName();
                if (matchFound(fileName)) {
                    if (isDebug) {
                        log("matched " + part.getFileName() + "(" + fileName + ")");
                    }
                    return true;
                }
            }
            return false;
        } finally {
            zis.close();
        }