Author: bago
Date: Sat Sep 27 06:39:56 2008
New Revision: 699642

URL: http://svn.apache.org/viewvc?rev=699642&view=rev
Log:
Fix formatting for JAMES conventions. Remove non-ascii chars.

Modified:
    
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java
    
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/StripAttachment.java
    
james/mailet/standard/trunk/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java

Modified: 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java
URL: 
http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java?rev=699642&r1=699641&r2=699642&view=diff
==============================================================================
--- 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java
 (original)
+++ 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java
 Sat Sep 27 06:39:56 2008
@@ -18,12 +18,13 @@
 
 /**
  * <p>
- * This mailet takes an attachment stored in an attribute and
- * attach it back to the message
+ * This mailet takes an attachment stored in an attribute and attach it back to
+ * the message
  * </p>
  * <p>
- * This may be used to place back attachment stripped by StripAttachment and 
stored in the
- * attribute 
<code>org.apache.james.transport.mailets.StripAttachment.saved</code>
+ * This may be used to place back attachment stripped by StripAttachment and
+ * stored in the attribute
+ * <code>org.apache.james.transport.mailets.StripAttachment.saved</code>
  * </p>
  * <p>
  * 
@@ -37,82 +38,88 @@
  */
 public class RecoverAttachment extends GenericMailet {
 
-       public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
-       private String attributeName = null;
+    public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
 
-       /**
-        * Checks if the mandatory parameters are present
-        * 
-        * @throws MailetException
-        */
-       public void init() throws MailetException {
-               attributeName = getInitParameter(ATTRIBUTE_PARAMETER_NAME);
-               
-               if (attributeName == null) {
-                       throw new MailetException(ATTRIBUTE_PARAMETER_NAME+" is 
a mandatory parameter");
-               }
-               
-               log("RecoverAttachment is initialised with attribute 
["+attributeName+"]");
-       }
-
-       /**
-        * Service the mail: check for the attribute and attach the attachment
-        * to the mail.
-        * 
-        * @param mail
-        *          The mail to service
-        * @throws MailetException
-        *           Thrown when an error situation is encountered.
-        */
-       public void service(Mail mail) throws MailetException {
-               Map attachments = (Map) mail.getAttribute(attributeName);
-               if (attachments != null) {
-
-                       MimeMessage message = null;
-                       try {
-                               message = mail.getMessage();
-                       } catch (MessagingException e) {
-                               throw new MailetException("Could not retrieve 
message from Mail object",
-                                               e);
-                       }
-
-                       Iterator i = attachments.values().iterator();
-                       try {
-                               while (i.hasNext()) {
-                                       byte[] bytes = (byte[]) i.next();
-                                       InputStream is = new 
BufferedInputStream(new ByteArrayInputStream(bytes));
-                                       MimeBodyPart p = new MimeBodyPart(is);
-                                       if (!(message.isMimeType("multipart/*") 
&& (message.getContent() instanceof MimeMultipart))) {
-                                               Object content = 
message.getContent();
-                                               String contentType = 
message.getContentType();
-                                               MimeMultipart mimeMultipart = 
new MimeMultipart();
-                                               
message.setContent(mimeMultipart);
-                                               // This saveChanges is required 
when the MimeMessage has been created from
-                                               // an InputStream, otherwise it 
is not saved correctly.
-                                               message.saveChanges();
-                                               
mimeMultipart.setParent(message);
-                                               MimeBodyPart bodyPart = new 
MimeBodyPart();
-                                               
mimeMultipart.addBodyPart(bodyPart);
-                                               bodyPart.setContent(content, 
contentType);
-                                       }
-                                       ((MimeMultipart) 
message.getContent()).addBodyPart(p);
-                               }
-                               message.saveChanges();
-                       } catch (MessagingException e) {
-                               log("MessagingException in recoverAttachment", 
e);
-                       } catch (IOException e) {
-                               log("IOException in recoverAttachment", e);
-                       }
-               }
-       }
-
-       /**
-        * returns a String describing this mailet.
-        * 
-        * @return A desciption of this mailet
-        */
-       public String getMailetInfo() {
-               return "RecoverAttachment Mailet";
-       }
+    private String attributeName = null;
+
+    /**
+     * Checks if the mandatory parameters are present
+     * 
+     * @throws MailetException
+     */
+    public void init() throws MailetException {
+        attributeName = getInitParameter(ATTRIBUTE_PARAMETER_NAME);
+
+        if (attributeName == null) {
+            throw new MailetException(ATTRIBUTE_PARAMETER_NAME
+                    + " is a mandatory parameter");
+        }
+
+        log("RecoverAttachment is initialised with attribute [" + attributeName
+                + "]");
+    }
+
+    /**
+     * Service the mail: check for the attribute and attach the attachment to
+     * the mail.
+     * 
+     * @param mail
+     *            The mail to service
+     * @throws MailetException
+     *             Thrown when an error situation is encountered.
+     */
+    public void service(Mail mail) throws MailetException {
+        Map attachments = (Map) mail.getAttribute(attributeName);
+        if (attachments != null) {
+
+            MimeMessage message = null;
+            try {
+                message = mail.getMessage();
+            } catch (MessagingException e) {
+                throw new MailetException(
+                        "Could not retrieve message from Mail object", e);
+            }
+
+            Iterator i = attachments.values().iterator();
+            try {
+                while (i.hasNext()) {
+                    byte[] bytes = (byte[]) i.next();
+                    InputStream is = new BufferedInputStream(
+                            new ByteArrayInputStream(bytes));
+                    MimeBodyPart p = new MimeBodyPart(is);
+                    if (!(message.isMimeType("multipart/*") && (message
+                            .getContent() instanceof MimeMultipart))) {
+                        Object content = message.getContent();
+                        String contentType = message.getContentType();
+                        MimeMultipart mimeMultipart = new MimeMultipart();
+                        message.setContent(mimeMultipart);
+                        // This saveChanges is required when the MimeMessage 
has
+                        // been created from
+                        // an InputStream, otherwise it is not saved correctly.
+                        message.saveChanges();
+                        mimeMultipart.setParent(message);
+                        MimeBodyPart bodyPart = new MimeBodyPart();
+                        mimeMultipart.addBodyPart(bodyPart);
+                        bodyPart.setContent(content, contentType);
+                    }
+                    ((MimeMultipart) message.getContent()).addBodyPart(p);
+                }
+                message.saveChanges();
+            } catch (MessagingException e) {
+                log("MessagingException in recoverAttachment", e);
+            } catch (IOException e) {
+                log("IOException in recoverAttachment", e);
+            }
+        }
+    }
+
+    /**
+     * returns a String describing this mailet.
+     * 
+     * @return A desciption of this mailet
+     */
+    public String getMailetInfo() {
+        return "RecoverAttachment Mailet";
+    }
 
 }

Modified: 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/StripAttachment.java
URL: 
http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/StripAttachment.java?rev=699642&r1=699641&r2=699642&view=diff
==============================================================================
--- 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/StripAttachment.java
 (original)
+++ 
james/mailet/standard/trunk/src/main/java/org/apache/james/transport/mailets/StripAttachment.java
 Sat Sep 27 06:39:56 2008
@@ -27,8 +27,8 @@
 
 /**
  * <p>
- * Remove attachments from a Message.
- * Supports simple removal, storing to file, or storing to mail attributes. 
+ * Remove attachments from a Message. Supports simple removal, storing to file,
+ * or storing to mail attributes.
  * </p>
  * <p>
  * Configuration:
@@ -49,318 +49,365 @@
  */
 public class StripAttachment extends GenericMailet {
 
-       public static final String PATTERN_PARAMETER_NAME = "pattern";
-       public static final String NOTPATTERN_PARAMETER_NAME = "notpattern";
-       public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
-       public static final String DIRECTORY_PARAMETER_NAME = "directory";
-       public static final String REMOVE_ATTACHMENT_PARAMETER_NAME = "remove"; 
// either "no", "matched", "all"
-       public static final String DECODE_FILENAME_PARAMETER_NAME = 
"decodeFilename"; // either "true", "false"
-       public static final String REPLACE_FILENAME_PATTERN_PARAMETER_NAME = 
"replaceFilenamePattern"; 
-       public static final String REMOVE_NONE = "no";
-       public static final String REMOVE_ALL = "all";
-       public static final String REMOVE_MATCHED = "matched";
-       public static final String REMOVED_ATTACHMENTS_ATTRIBUTE_KEY = 
"org.apache.james.transport.mailets.StripAttachment.removed";
-       public static final String SAVED_ATTACHMENTS_ATTRIBUTE_KEY = 
"org.apache.james.transport.mailets.StripAttachment.saved";
-
-       private String patternString = null;
-       private String notpatternString = null;
-       private String removeAttachments = null;
-       private String directoryName = null;
-       private String attributeName = null;
-       private Pattern regExPattern = null;
-       private Pattern notregExPattern = null;
-       
-       private boolean decodeFilename = false;
-       private Pattern[] replaceFilenamePatterns = null;
-       private String[] replaceFilenameSubstitutions = null;
-       private Integer[] replaceFilenameFlags = null;
-
-       private static boolean getBooleanParameter(String v, boolean def) {
-               return def ? 
-                               !(v != null && (v.equalsIgnoreCase("false") || 
v.equalsIgnoreCase("no"))) : 
-                                       v != null && 
(v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes"))  ;
-       }
-       
-       /**
-        * Checks if the mandatory parameters are present, creates the 
directory to
-        * save the files ni (if not present).
-        * 
-        * @throws MailetException
-        */
-       public void init() throws MailetException {
-               patternString = getInitParameter(PATTERN_PARAMETER_NAME);
-               notpatternString = getInitParameter(NOTPATTERN_PARAMETER_NAME);
-               if (patternString == null && notpatternString == null) {
-                       throw new MailetException("No value for " + 
PATTERN_PARAMETER_NAME
-                                       + " parameter was provided.");
-               }
-
-               directoryName = getInitParameter(DIRECTORY_PARAMETER_NAME);
-               attributeName = getInitParameter(ATTRIBUTE_PARAMETER_NAME);
-
-               removeAttachments = 
getInitParameter(REMOVE_ATTACHMENT_PARAMETER_NAME,
-                               REMOVE_NONE).toLowerCase();
-               if (!REMOVE_MATCHED.equals(removeAttachments)
-                               && !REMOVE_ALL.equals(removeAttachments)) {
-                       removeAttachments = REMOVE_NONE;
-               }
-               
-               try {
-                       //if (patternString != null) regExPattern = new 
Perl5Compiler().compile(patternString);
-                       if (patternString != null) regExPattern = 
Pattern.compile(patternString);
-               } catch (Exception e) {
-                       throw new MailetException("Could not compile regex [" + 
patternString + "].");
-               }
-               try {
-                       //if (notpatternString != null) notregExPattern = new 
Perl5Compiler().compile(notpatternString);
-                       if (notpatternString != null) notregExPattern = 
Pattern.compile(notpatternString);
-               } catch (Exception e) {
-                       throw new MailetException("Could not compile regex [" + 
notpatternString + "].");
-               }
-
-               if (directoryName != null) {
-                       try {
-                               File saveDirectory = null;
-                               saveDirectory = new File(directoryName);
-                               if (!saveDirectory.exists()) {
-                                       saveDirectory.mkdirs();
-                               }
-                       } catch (Exception e) {
-                               throw new MailetException("Could not create 
directory ["
-                                               + directoryName + "].", e);
-                       }
-               }
-               
-               decodeFilename = 
getBooleanParameter(getInitParameter(DECODE_FILENAME_PARAMETER_NAME), 
decodeFilename);
-               if (getInitParameter(REPLACE_FILENAME_PATTERN_PARAMETER_NAME) 
!= null) {
-                       List[] pl = 
ReplaceContent.getPatternsFromString(getInitParameter(REPLACE_FILENAME_PATTERN_PARAMETER_NAME));
-                       replaceFilenamePatterns = (Pattern[]) pl[0].toArray(new 
Pattern[0]);
-                       replaceFilenameSubstitutions = (String[]) 
pl[1].toArray(new String[0]);
-                       replaceFilenameFlags = (Integer[]) pl[2].toArray(new 
Integer[0]);
-               }
-               
-               String toLog = "StripAttachment is initialised with regex 
pattern ["
-                               + patternString + " / " + notpatternString + 
"]";
-               if (directoryName != null) {
-                       toLog += " and will save to directory [" + 
directoryName + "]";
-               }
-               if (attributeName != null) {
-                       toLog += " and will store attachments to attribute [" + 
attributeName + "]";
-               }
-               log(toLog);
-       }
-
-       /**
-        * Service the mail: scan it for attchemnts matching the pattern, store 
the
-        * content of a matchin attachment in the given directory.
-        * 
-        * @param mail
-        *          The mail to service
-        * @throws MailetException
-        *           Thrown when an error situation is encountered.
-        */
-       public void service(Mail mail) throws MailetException {
-               MimeMessage message = null;
-               try {
-                       message = mail.getMessage();
-               } catch (MessagingException e) {
-                       throw new MailetException("Could not retrieve message 
from Mail object",
-                                       e);
-               }
-               // All MIME messages with an attachment are multipart, so we do 
nothing if
-               // it is not mutlipart
-               try {
-                       if (message.isMimeType("multipart/*")) {
-                               analyseMultipartPartMessage(message, mail);
-                       }
-               } catch (MessagingException e) {
-                       throw new MailetException("Could not retrieve 
contenttype of message.", e);
-               } catch (Exception e) {
-                       throw new MailetException("Could not analyse message.", 
e);
-               }
-       }
-
-       /**
-        * returns a String describing this mailet.
-        * 
-        * @return A desciption of this mailet
-        */
-       public String getMailetInfo() {
-               return "StripAttachment";
-       }
-
-       /**
-        * Checks every part in this part (if it is a Multipart) for having a 
filename
-        * that matches the pattern. If the name matches, the content of the 
part is
-        * stored (using its name) in te given diretcory.
-        * 
-        * Note: this method is recursive.
-        * 
-        * @param part
-        *          The part to analyse.
-        * @param mail
-        * @return
-        * @throws Exception
-        */
-       private boolean analyseMultipartPartMessage(Part part, Mail mail)
-                       throws Exception {
-               if (part.isMimeType("multipart/*")) {
-                       try {
-                               Multipart multipart = (Multipart) 
part.getContent();
-                               boolean atLeastOneRemoved = false;
-                               int numParts = multipart.getCount();
-                               for (int i = 0; i < numParts; i++) {
-                                       Part p = multipart.getBodyPart(i);
-                                       if (p.isMimeType("multipart/*")) {
-                                               atLeastOneRemoved |= 
analyseMultipartPartMessage(p, mail);
-                                       } else {
-                                               boolean removed = 
checkMessageRemoved(p, mail);
-                                               if (removed) {
-                                                       
multipart.removeBodyPart(i);
-                                                       atLeastOneRemoved = 
true;
-                                                       i--;
-                                                       numParts--;
-                                               }
-                                       }
-                               }
-                               if (atLeastOneRemoved) {
-                                       part.setContent(multipart);
-                                       if (part instanceof Message) {
-                                               ((Message) part).saveChanges();
-                                       }
-                               }
-                               return atLeastOneRemoved;
-                       } catch (Exception e) {
-                               log("Could not analyse part.", e);
-                       }
-               }
-               return false;
-       }
-
-       private boolean checkMessageRemoved(Part part, Mail mail)
-                       throws MessagingException, Exception {
-               String fileName = null;
-               fileName = part.getFileName();
-               
-               // filename or name of part can be null, so we have to be 
careful
-
-               boolean ret = false;
-
-               if (fileName != null) {
-                       if (decodeFilename) fileName = 
MimeUtility.decodeText(fileName);
-                       
-                       if (replaceFilenamePatterns != null)
-                               fileName = 
ReplaceContent.applyPatterns(replaceFilenamePatterns, 
replaceFilenameSubstitutions, replaceFilenameFlags, fileName, 0, this);
-
-                       if (fileNameMatches(fileName)) {
-                               if (directoryName != null) {
-                                       String filename = 
saveAttachmentToFile(part, fileName);
-                                       if (filename != null) {
-                                               Collection c = (Collection) mail
-                                                               
.getAttribute(SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
-                                               if (c == null) {
-                                                       c = new ArrayList();
-                                                       
mail.setAttribute(SAVED_ATTACHMENTS_ATTRIBUTE_KEY,(ArrayList) c);
-                                               }
-                                               c.add(filename);
-                                       }
-                               }
-                               if (attributeName != null) {
-                                       Map m = (Map) 
mail.getAttribute(attributeName);
-                                       if (m == null) {
-                                               m = new LinkedHashMap();
-                                               
mail.setAttribute(attributeName, (LinkedHashMap) m);
-                                       }
-                                       ByteArrayOutputStream 
byteArrayOutputStream = new ByteArrayOutputStream();
-                                       OutputStream os = new 
BufferedOutputStream(byteArrayOutputStream);
-                                       part.writeTo(os);
-                                       m.put(fileName, 
byteArrayOutputStream.toByteArray());
-                               }
-                               if (removeAttachments.equals(REMOVE_MATCHED)) {
-                                       ret = true;
-                               }
-                       }
-                       if (!ret) {
-                               ret = removeAttachments.equals(REMOVE_ALL);
-                       }
-                       if (ret) {
-                               Collection c = (Collection) mail
-                                               
.getAttribute(REMOVED_ATTACHMENTS_ATTRIBUTE_KEY);
-                               if (c == null) {
-                                       c = new ArrayList();
-                                       
mail.setAttribute(REMOVED_ATTACHMENTS_ATTRIBUTE_KEY,(ArrayList) c);
-                               }
-                               c.add(fileName);
-                       }
-               }
-               return ret;
-       }
-
-       /**
-        * Checks if the given name matches the pattern.
-        * 
-        * @param name
-        *          The name to check for a match.
-        * @return True if a match is found, false otherwise.
-        */
-       private boolean fileNameMatches(String name) {
-               boolean result = true;
-               if (regExPattern != null)
-                       result = regExPattern.matcher(name).matches();
-               if (result && notregExPattern != null)
-                       result = !notregExPattern.matcher(name).matches();
-               
-               String log = "attachment "+name+" ";
-               if (!result) log += "does not match";
-               else log += "matches";
-               log(log);
-               return result;
-       }
-
-       /**
-        * Saves the content of the part to a file in the given directoy, using 
the
-        * name of the part. If a file with that name already exists, it will
-        * 
-        * @param part
-        *          The MIME part to save.
-        * @return
-        * @throws Exception
-        */
-       private String saveAttachmentToFile(Part part, String fileName) throws 
Exception {
-               BufferedOutputStream os = null;
-               InputStream is = null;
-               File f = null;
-               try {
-                       if (fileName == null) fileName = part.getFileName();
-                       int pos = -1;
-                       if (fileName != null) {
-                               pos = fileName.lastIndexOf(".");
-                       }
-                       String prefix = pos > 0 ? (fileName.substring(0, pos)) 
: fileName;
-                       String suffix = pos > 0 ? (fileName.substring(pos)) : 
".bin";
-                       while (prefix.length() < 3) prefix += "_";
-                       if (suffix.length() == 0) suffix = ".bin";
-                       f = File.createTempFile(prefix,suffix,new 
File(directoryName));
-                       log("saving content of " + f.getName() + "...");
-                       os = new BufferedOutputStream(new FileOutputStream(f));
-                       is = part.getInputStream();
-                       if (!(is instanceof BufferedInputStream)) {
-                               is = new BufferedInputStream(is);
-                       }
-                       int c;
-                       while ((c = is.read()) != -1) {
-                               os.write(c);
-                       }
-
-                       return f.getName();
-               } catch (Exception e) {
-                       log("Error while saving contents of ["
-                                       + (f != null ? f.getName() : (part != 
null ? part.getFileName() : "NULL")) + "].", e);
-                       throw e;
-               } finally {
-                       is.close();
-                       os.close();
-               }
-       }
+    public static final String PATTERN_PARAMETER_NAME = "pattern";
+
+    public static final String NOTPATTERN_PARAMETER_NAME = "notpattern";
+
+    public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
+
+    public static final String DIRECTORY_PARAMETER_NAME = "directory";
+
+    // Either "no", "matched", "all"
+    public static final String REMOVE_ATTACHMENT_PARAMETER_NAME = "remove"; 
+
+    // Either "true", "false"
+    public static final String DECODE_FILENAME_PARAMETER_NAME = 
"decodeFilename";
+
+    public static final String REPLACE_FILENAME_PATTERN_PARAMETER_NAME = 
"replaceFilenamePattern";
+
+    public static final String REMOVE_NONE = "no";
+
+    public static final String REMOVE_ALL = "all";
+
+    public static final String REMOVE_MATCHED = "matched";
+
+    public static final String REMOVED_ATTACHMENTS_ATTRIBUTE_KEY = 
"org.apache.james.transport.mailets.StripAttachment.removed";
+
+    public static final String SAVED_ATTACHMENTS_ATTRIBUTE_KEY = 
"org.apache.james.transport.mailets.StripAttachment.saved";
+
+    private String patternString = null;
+
+    private String notpatternString = null;
+
+    private String removeAttachments = null;
+
+    private String directoryName = null;
+
+    private String attributeName = null;
+
+    private Pattern regExPattern = null;
+
+    private Pattern notregExPattern = null;
+
+    private boolean decodeFilename = false;
+
+    private Pattern[] replaceFilenamePatterns = null;
+
+    private String[] replaceFilenameSubstitutions = null;
+
+    private Integer[] replaceFilenameFlags = null;
+
+    private static boolean getBooleanParameter(String v, boolean def) {
+        return def ? !(v != null && (v.equalsIgnoreCase("false") || v
+                .equalsIgnoreCase("no"))) : v != null
+                && (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes"));
+    }
+
+    /**
+     * Checks if the mandatory parameters are present, creates the directory to
+     * save the files ni (if not present).
+     * 
+     * @throws MailetException
+     */
+    public void init() throws MailetException {
+        patternString = getInitParameter(PATTERN_PARAMETER_NAME);
+        notpatternString = getInitParameter(NOTPATTERN_PARAMETER_NAME);
+        if (patternString == null && notpatternString == null) {
+            throw new MailetException("No value for " + PATTERN_PARAMETER_NAME
+                    + " parameter was provided.");
+        }
+
+        directoryName = getInitParameter(DIRECTORY_PARAMETER_NAME);
+        attributeName = getInitParameter(ATTRIBUTE_PARAMETER_NAME);
+
+        removeAttachments = getInitParameter(REMOVE_ATTACHMENT_PARAMETER_NAME,
+                REMOVE_NONE).toLowerCase();
+        if (!REMOVE_MATCHED.equals(removeAttachments)
+                && !REMOVE_ALL.equals(removeAttachments)) {
+            removeAttachments = REMOVE_NONE;
+        }
+
+        try {
+            // if (patternString != null) regExPattern = new
+            // Perl5Compiler().compile(patternString);
+            if (patternString != null)
+                regExPattern = Pattern.compile(patternString);
+        } catch (Exception e) {
+            throw new MailetException("Could not compile regex ["
+                    + patternString + "].");
+        }
+        try {
+            // if (notpatternString != null) notregExPattern = new
+            // Perl5Compiler().compile(notpatternString);
+            if (notpatternString != null)
+                notregExPattern = Pattern.compile(notpatternString);
+        } catch (Exception e) {
+            throw new MailetException("Could not compile regex ["
+                    + notpatternString + "].");
+        }
+
+        if (directoryName != null) {
+            try {
+                File saveDirectory = null;
+                saveDirectory = new File(directoryName);
+                if (!saveDirectory.exists()) {
+                    saveDirectory.mkdirs();
+                }
+            } catch (Exception e) {
+                throw new MailetException("Could not create directory ["
+                        + directoryName + "].", e);
+            }
+        }
+
+        decodeFilename = getBooleanParameter(
+                getInitParameter(DECODE_FILENAME_PARAMETER_NAME),
+                decodeFilename);
+        if (getInitParameter(REPLACE_FILENAME_PATTERN_PARAMETER_NAME) != null) 
{
+            List[] pl = ReplaceContent
+                    
.getPatternsFromString(getInitParameter(REPLACE_FILENAME_PATTERN_PARAMETER_NAME));
+            replaceFilenamePatterns = (Pattern[]) pl[0].toArray(new 
Pattern[0]);
+            replaceFilenameSubstitutions = (String[]) pl[1]
+                    .toArray(new String[0]);
+            replaceFilenameFlags = (Integer[]) pl[2].toArray(new Integer[0]);
+        }
+
+        String toLog = "StripAttachment is initialised with regex pattern ["
+                + patternString + " / " + notpatternString + "]";
+        if (directoryName != null) {
+            toLog += " and will save to directory [" + directoryName + "]";
+        }
+        if (attributeName != null) {
+            toLog += " and will store attachments to attribute ["
+                    + attributeName + "]";
+        }
+        log(toLog);
+    }
+
+    /**
+     * Service the mail: scan it for attchemnts matching the pattern, store the
+     * content of a matchin attachment in the given directory.
+     * 
+     * @param mail
+     *            The mail to service
+     * @throws MailetException
+     *             Thrown when an error situation is encountered.
+     */
+    public void service(Mail mail) throws MailetException {
+        MimeMessage message = null;
+        try {
+            message = mail.getMessage();
+        } catch (MessagingException e) {
+            throw new MailetException(
+                    "Could not retrieve message from Mail object", e);
+        }
+        // All MIME messages with an attachment are multipart, so we do nothing
+        // if it is not mutlipart
+        try {
+            if (message.isMimeType("multipart/*")) {
+                analyseMultipartPartMessage(message, mail);
+            }
+        } catch (MessagingException e) {
+            throw new MailetException(
+                    "Could not retrieve contenttype of message.", e);
+        } catch (Exception e) {
+            throw new MailetException("Could not analyse message.", e);
+        }
+    }
+
+    /**
+     * returns a String describing this mailet.
+     * 
+     * @return A desciption of this mailet
+     */
+    public String getMailetInfo() {
+        return "StripAttachment";
+    }
+
+    /**
+     * Checks every part in this part (if it is a Multipart) for having a
+     * filename that matches the pattern. If the name matches, the content of
+     * the part is stored (using its name) in te given diretcory.
+     * 
+     * Note: this method is recursive.
+     * 
+     * @param part
+     *            The part to analyse.
+     * @param mail
+     * @return
+     * @throws Exception
+     */
+    private boolean analyseMultipartPartMessage(Part part, Mail mail)
+            throws Exception {
+        if (part.isMimeType("multipart/*")) {
+            try {
+                Multipart multipart = (Multipart) part.getContent();
+                boolean atLeastOneRemoved = false;
+                int numParts = multipart.getCount();
+                for (int i = 0; i < numParts; i++) {
+                    Part p = multipart.getBodyPart(i);
+                    if (p.isMimeType("multipart/*")) {
+                        atLeastOneRemoved |= analyseMultipartPartMessage(p,
+                                mail);
+                    } else {
+                        boolean removed = checkMessageRemoved(p, mail);
+                        if (removed) {
+                            multipart.removeBodyPart(i);
+                            atLeastOneRemoved = true;
+                            i--;
+                            numParts--;
+                        }
+                    }
+                }
+                if (atLeastOneRemoved) {
+                    part.setContent(multipart);
+                    if (part instanceof Message) {
+                        ((Message) part).saveChanges();
+                    }
+                }
+                return atLeastOneRemoved;
+            } catch (Exception e) {
+                log("Could not analyse part.", e);
+            }
+        }
+        return false;
+    }
+
+    private boolean checkMessageRemoved(Part part, Mail mail)
+            throws MessagingException, Exception {
+        String fileName = null;
+        fileName = part.getFileName();
+
+        // filename or name of part can be null, so we have to be careful
+        boolean ret = false;
+
+        if (fileName != null) {
+            if (decodeFilename)
+                fileName = MimeUtility.decodeText(fileName);
+
+            if (replaceFilenamePatterns != null)
+                fileName = ReplaceContent.applyPatterns(
+                        replaceFilenamePatterns, replaceFilenameSubstitutions,
+                        replaceFilenameFlags, fileName, 0, this);
+
+            if (fileNameMatches(fileName)) {
+                if (directoryName != null) {
+                    String filename = saveAttachmentToFile(part, fileName);
+                    if (filename != null) {
+                        Collection c = (Collection) mail
+                                .getAttribute(SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
+                        if (c == null) {
+                            c = new ArrayList();
+                            mail.setAttribute(SAVED_ATTACHMENTS_ATTRIBUTE_KEY,
+                                    (ArrayList) c);
+                        }
+                        c.add(filename);
+                    }
+                }
+                if (attributeName != null) {
+                    Map m = (Map) mail.getAttribute(attributeName);
+                    if (m == null) {
+                        m = new LinkedHashMap();
+                        mail.setAttribute(attributeName, (LinkedHashMap) m);
+                    }
+                    ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();
+                    OutputStream os = new BufferedOutputStream(
+                            byteArrayOutputStream);
+                    part.writeTo(os);
+                    m.put(fileName, byteArrayOutputStream.toByteArray());
+                }
+                if (removeAttachments.equals(REMOVE_MATCHED)) {
+                    ret = true;
+                }
+            }
+            if (!ret) {
+                ret = removeAttachments.equals(REMOVE_ALL);
+            }
+            if (ret) {
+                Collection c = (Collection) mail
+                        .getAttribute(REMOVED_ATTACHMENTS_ATTRIBUTE_KEY);
+                if (c == null) {
+                    c = new ArrayList();
+                    mail.setAttribute(REMOVED_ATTACHMENTS_ATTRIBUTE_KEY,
+                            (ArrayList) c);
+                }
+                c.add(fileName);
+            }
+        }
+        return ret;
+    }
+
+    /**
+     * Checks if the given name matches the pattern.
+     * 
+     * @param name
+     *            The name to check for a match.
+     * @return True if a match is found, false otherwise.
+     */
+    private boolean fileNameMatches(String name) {
+        boolean result = true;
+        if (regExPattern != null)
+            result = regExPattern.matcher(name).matches();
+        if (result && notregExPattern != null)
+            result = !notregExPattern.matcher(name).matches();
+
+        String log = "attachment " + name + " ";
+        if (!result)
+            log += "does not match";
+        else
+            log += "matches";
+        log(log);
+        return result;
+    }
+
+    /**
+     * Saves the content of the part to a file in the given directoy, using the
+     * name of the part. If a file with that name already exists, it will
+     * 
+     * @param part
+     *            The MIME part to save.
+     * @return
+     * @throws Exception
+     */
+    private String saveAttachmentToFile(Part part, String fileName)
+            throws Exception {
+        BufferedOutputStream os = null;
+        InputStream is = null;
+        File f = null;
+        try {
+            if (fileName == null)
+                fileName = part.getFileName();
+            int pos = -1;
+            if (fileName != null) {
+                pos = fileName.lastIndexOf(".");
+            }
+            String prefix = pos > 0 ? (fileName.substring(0, pos)) : fileName;
+            String suffix = pos > 0 ? (fileName.substring(pos)) : ".bin";
+            while (prefix.length() < 3)
+                prefix += "_";
+            if (suffix.length() == 0)
+                suffix = ".bin";
+            f = File.createTempFile(prefix, suffix, new File(directoryName));
+            log("saving content of " + f.getName() + "...");
+            os = new BufferedOutputStream(new FileOutputStream(f));
+            is = part.getInputStream();
+            if (!(is instanceof BufferedInputStream)) {
+                is = new BufferedInputStream(is);
+            }
+            int c;
+            while ((c = is.read()) != -1) {
+                os.write(c);
+            }
+
+            return f.getName();
+        } catch (Exception e) {
+            log("Error while saving contents of ["
+                    + (f != null ? f.getName() : (part != null ? part
+                            .getFileName() : "NULL")) + "].", e);
+            throw e;
+        } finally {
+            is.close();
+            os.close();
+        }
+    }
 
 }

Modified: 
james/mailet/standard/trunk/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
URL: 
http://svn.apache.org/viewvc/james/mailet/standard/trunk/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java?rev=699642&r1=699641&r2=699642&view=diff
==============================================================================
--- 
james/mailet/standard/trunk/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
 (original)
+++ 
james/mailet/standard/trunk/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
 Sat Sep 27 06:39:56 2008
@@ -26,289 +26,311 @@
 
 public class StripAttachmentTest extends TestCase {
 
-       public void testNoAttachment() {
+    public void testNoAttachment() {
 
-       }
+    }
+
+    public void testSimpleAttachment() throws MessagingException, IOException {
+        Mailet mailet = initMailet();
+
+        MimeMessage message = new MimeMessage(Session
+                .getDefaultInstance(new Properties()));
+
+        MimeMultipart mm = new MimeMultipart();
+        MimeBodyPart mp = new MimeBodyPart();
+        mp.setText("simple text");
+        mm.addBodyPart(mp);
+        String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
+        MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body).getBytes()));
+        mp2.setDisposition("attachment");
+        mp2.setFileName("10.tmp");
+        mm.addBodyPart(mp2);
+        String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4";
+        MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body2).getBytes()));
+        mp3.setDisposition("attachment");
+        mp3.setFileName("temp.zip");
+        mm.addBodyPart(mp3);
+        message.setSubject("test");
+        message.setContent(mm);
+        message.saveChanges();
+
+        Mail mail = new MockMail();
+        mail.setMessage(message);
+
+        mailet.service(mail);
+
+        ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+        mail.getMessage().writeTo(rawMessage,
+                new String[] { "Bcc", "Content-Length", "Message-ID" });
+        String res = rawMessage.toString();
+
+        System.out.println(res);
+
+        Collection c = (Collection) mail
+                .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
+        assertNotNull(c);
+
+        assertEquals(1, c.size());
+
+        String name = (String) c.iterator().next();
+
+        File f = new File("c:/" + name);
+        InputStream is = new FileInputStream(f);
+        String savedFile = toString(is);
+        is.close();
+        assertEquals(body, savedFile);
+
+        f.delete();
+
+    }
+
+    public String toString(final InputStream is) throws IOException {
+        final ByteArrayOutputStream sw = new ByteArrayOutputStream();
+        final byte[] buffer = new byte[1024];
+        int n = 0;
+        while (-1 != (n = is.read(buffer))) {
+            sw.write(buffer, 0, n);
+        }
+        return sw.toString();
+    }
+
+    public void testSimpleAttachment2() throws MessagingException, IOException 
{
+        Mailet mailet = new StripAttachment();
+
+        MockMailetConfig mci = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci.setProperty("directory", "c:/");
+        mci.setProperty("remove", "all");
+        mci.setProperty("notpattern", "^(winmail\\.dat$)");
+        mailet.init(mci);
+
+        MimeMessage message = new MimeMessage(Session
+                .getDefaultInstance(new Properties()));
+
+        MimeMultipart mm = new MimeMultipart();
+        MimeBodyPart mp = new MimeBodyPart();
+        mp.setText("simple text");
+        mm.addBodyPart(mp);
+        String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
+        MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body).getBytes()));
+        mp2.setDisposition("attachment");
+        mp2.setFileName("temp.tmp");
+        mm.addBodyPart(mp2);
+        String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4";
+        MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body2).getBytes()));
+        mp3.setDisposition("attachment");
+        mp3.setFileName("winmail.dat");
+        mm.addBodyPart(mp3);
+        message.setSubject("test");
+        message.setContent(mm);
+        message.saveChanges();
+
+        Mail mail = new MockMail();
+        mail.setMessage(message);
+
+        mailet.service(mail);
+
+        ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+        mail.getMessage().writeTo(rawMessage,
+                new String[] { "Bcc", "Content-Length", "Message-ID" });
+        String res = rawMessage.toString();
+
+        System.out.println(res);
+
+        Collection c = (Collection) mail
+                .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
+        assertNotNull(c);
+
+        assertEquals(1, c.size());
+
+        String name = (String) c.iterator().next();
+
+        File f = new File("c:/" + name);
+        InputStream is = new FileInputStream(f);
+        String savedFile = IOUtil.toString(is);
+        is.close();
+        assertEquals(body, savedFile);
+
+        f.delete();
 
-       public void testSimpleAttachment() throws MessagingException, 
IOException {
-               Mailet mailet = initMailet();
+    }
+
+    public void testSimpleAttachment3() throws MessagingException, IOException 
{
+        Mailet mailet = initMailet();
+
+        // System.setProperty("mail.mime.decodefilename", "true");
+
+        MimeMessage message = new MimeMessage(Session
+                .getDefaultInstance(new Properties()));
+
+        MimeMultipart mm = new MimeMultipart();
+        MimeBodyPart mp = new MimeBodyPart();
+        mp.setText("simple text");
+        mm.addBodyPart(mp);
+        String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
+        MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body).getBytes()));
+        mp2.setDisposition("attachment");
+        mp2
+                
.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=");
+        mm.addBodyPart(mp2);
+        String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4";
+        MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body2).getBytes()));
+        mp3.setDisposition("attachment");
+        mp3.setFileName("temp.zip");
+        mm.addBodyPart(mp3);
+        message.setSubject("test");
+        message.setContent(mm);
+        message.saveChanges();
+
+        // message.writeTo(System.out);
+        // System.out.println("--------------------------\n\n\n");
+
+        Mail mail = new MockMail();
+        mail.setMessage(message);
+
+        mailet.service(mail);
+
+        ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+        mail.getMessage().writeTo(rawMessage,
+                new String[] { "Bcc", "Content-Length", "Message-ID" });
+        String res = rawMessage.toString();
+
+        System.out.println(res);
+
+        Collection c = (Collection) mail
+                .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
+        assertNotNull(c);
+
+        assertEquals(1, c.size());
+
+        String name = (String) c.iterator().next();
+        // System.out.println("--------------------------\n\n\n");
+        // System.out.println(name);
+
+        assertTrue(name.startsWith("e_Pubblicita_e_vietata_Milano9052"));
+
+        File f = new File("c:/" + name);
+        InputStream is = new FileInputStream(f);
+        String savedFile = IOUtil.toString(is);
+        is.close();
+        assertEquals(body, savedFile);
+
+        f.delete();
 
-               MimeMessage message = new MimeMessage(Session
-                               .getDefaultInstance(new Properties()));
-               
-               MimeMultipart mm = new MimeMultipart();
-               MimeBodyPart mp = new MimeBodyPart();
-               mp.setText("simple text");
-               mm.addBodyPart(mp);
-               String body = "ùàòòùàòùàòùàòùàòù";
-               MimeBodyPart mp2 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body).getBytes()));
-               mp2.setDisposition("attachment");
-               mp2.setFileName("10.tmp");
-               mm.addBodyPart(mp2);
-               String body2 = "dglkjhdfòlgkj";
-               MimeBodyPart mp3 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body2).getBytes()));
-               mp3.setDisposition("attachment");
-               mp3.setFileName("temp.zip");
-               mm.addBodyPart(mp3);
-               message.setSubject("test");
-               message.setContent(mm);
-               message.saveChanges();
-               
-               Mail mail = new MockMail();
-               mail.setMessage(message);
-
-               mailet.service(mail);
-
-               ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
-               mail.getMessage().writeTo(rawMessage,
-                               new String[] { "Bcc", "Content-Length", 
"Message-ID" });
-               String res = rawMessage.toString();
-               
-               System.out.println(res);
-               
-               Collection c = (Collection) 
mail.getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
-               assertNotNull(c);
-               
-               assertEquals(1,c.size());
-
-               String name = (String) c.iterator().next();
-
-               File f = new File("c:/"+name);
-               InputStream is = new FileInputStream(f);
-               String savedFile = toString(is);
-               is.close();
-               assertEquals(body,savedFile);
-               
-               f.delete();
-               
-       }
-       
-       public String toString( final InputStream is ) throws IOException {
-               final ByteArrayOutputStream sw = new ByteArrayOutputStream();
-    final byte[] buffer = new byte[1024];
-    int n = 0;
-    while (-1 != (n = is.read(buffer))) {
-        sw.write( buffer, 0, n );
     }
-    return sw.toString();
-       }
 
-       public void testSimpleAttachment2() throws MessagingException, 
IOException {
-               Mailet mailet = new StripAttachment();
+    public void testToAndFromAttributes() throws MessagingException,
+            IOException {
+        Mailet strip = new StripAttachment();
+        MockMailetConfig mci = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci.setProperty("attribute", "my.attribute");
+        mci.setProperty("remove", "all");
+        mci.setProperty("notpattern", ".*\\.tmp.*");
+        strip.init(mci);
+
+        Mailet recover = new RecoverAttachment();
+        MockMailetConfig mci2 = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci2.setProperty("attribute", "my.attribute");
+        recover.init(mci2);
+
+        Mailet onlyText = new OnlyText();
+        onlyText.init(new MockMailetConfig("Test", new MockMailContext()));
+
+        MimeMessage message = new MimeMessage(Session
+                .getDefaultInstance(new Properties()));
+
+        MimeMultipart mm = new MimeMultipart();
+        MimeBodyPart mp = new MimeBodyPart();
+        mp.setText("simple text");
+        mm.addBodyPart(mp);
+        String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
+        MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body).getBytes()));
+        mp2.setDisposition("attachment");
+        mp2
+                
.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=");
+        mm.addBodyPart(mp2);
+        String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4";
+        MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream(
+                ("Content-Transfer-Encoding: 8bit\r\n\r\n" + 
body2).getBytes()));
+        mp3.setDisposition("attachment");
+        mp3.setFileName("temp.zip");
+        mm.addBodyPart(mp3);
+        message.setSubject("test");
+        message.setContent(mm);
+        message.saveChanges();
+        Mail mail = new MockMail();
+        mail.setMessage(message);
+
+        assertTrue(mail.getMessage().getContent() instanceof MimeMultipart);
+        assertEquals(3, ((MimeMultipart) mail.getMessage().getContent())
+                .getCount());
+
+        strip.service(mail);
+
+        assertTrue(mail.getMessage().getContent() instanceof MimeMultipart);
+        assertEquals(1, ((MimeMultipart) mail.getMessage().getContent())
+                .getCount());
+
+        onlyText.service(mail);
+
+        assertFalse(mail.getMessage().getContent() instanceof MimeMultipart);
+
+        // prova per caricare il mime message da input stream che altrimenti
+        // javamail si comporta differentemente.
+        String mimeSource = "Message-ID: <[EMAIL PROTECTED]>\r\nSubject: 
test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; 
charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\n\r\nsimple text";
+
+        MimeMessage mmNew = new MimeMessage(Session
+                .getDefaultInstance(new Properties()),
+                new ByteArrayInputStream(mimeSource.getBytes()));
+
+        mmNew.writeTo(System.out);
+
+        recover.service(mail);
+
+        assertTrue(mail.getMessage().getContent() instanceof MimeMultipart);
+        assertEquals(2, ((MimeMultipart) mail.getMessage().getContent())
+                .getCount());
 
-               MockMailetConfig mci = new MockMailetConfig("Test",new 
MockMailContext());
-               mci.setProperty("directory","c:/");
-               mci.setProperty("remove","all");
-               mci.setProperty("notpattern","^(winmail\\.dat$)");
-               mailet.init(mci);
-
-               MimeMessage message = new MimeMessage(Session
-                               .getDefaultInstance(new Properties()));
-               
-               MimeMultipart mm = new MimeMultipart();
-               MimeBodyPart mp = new MimeBodyPart();
-               mp.setText("simple text");
-               mm.addBodyPart(mp);
-               String body = "ùàòòùàòùàòùàòùàòù";
-               MimeBodyPart mp2 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body).getBytes()));
-               mp2.setDisposition("attachment");
-               mp2.setFileName("temp.tmp");
-               mm.addBodyPart(mp2);
-               String body2 = "dglkjhdfòlgkj";
-               MimeBodyPart mp3 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body2).getBytes()));
-               mp3.setDisposition("attachment");
-               mp3.setFileName("winmail.dat");
-               mm.addBodyPart(mp3);
-               message.setSubject("test");
-               message.setContent(mm);
-               message.saveChanges();
-               
-               Mail mail = new MockMail();
-               mail.setMessage(message);
-
-               mailet.service(mail);
-
-               ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
-               mail.getMessage().writeTo(rawMessage,
-                               new String[] { "Bcc", "Content-Length", 
"Message-ID" });
-               String res = rawMessage.toString();
-               
-               System.out.println(res);
-               
-               Collection c = (Collection) 
mail.getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
-               assertNotNull(c);
-               
-               assertEquals(1,c.size());
-
-               String name = (String) c.iterator().next();
-
-               File f = new File("c:/"+name);
-               InputStream is = new FileInputStream(f);
-               String savedFile = IOUtil.toString(is);
-               is.close();
-               assertEquals(body,savedFile);
-               
-               f.delete();
-               
-       }
-
-       public void testSimpleAttachment3() throws MessagingException, 
IOException {
-               Mailet mailet = initMailet();
-               
-               //System.setProperty("mail.mime.decodefilename", "true");
-
-               MimeMessage message = new MimeMessage(Session
-                               .getDefaultInstance(new Properties()));
-               
-               MimeMultipart mm = new MimeMultipart();
-               MimeBodyPart mp = new MimeBodyPart();
-               mp.setText("simple text");
-               mm.addBodyPart(mp);
-               String body = "ùàòòùàòùàòùàòùàòù";
-               MimeBodyPart mp2 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body).getBytes()));
-               mp2.setDisposition("attachment");
-               
mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=");
-               mm.addBodyPart(mp2);
-               String body2 = "dglkjhdfòlgkj";
-               MimeBodyPart mp3 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body2).getBytes()));
-               mp3.setDisposition("attachment");
-               mp3.setFileName("temp.zip");
-               mm.addBodyPart(mp3);
-               message.setSubject("test");
-               message.setContent(mm);
-               message.saveChanges();
-               
-               //message.writeTo(System.out);
-               //System.out.println("--------------------------\n\n\n");
-               
-               Mail mail = new MockMail();
-               mail.setMessage(message);
-
-               mailet.service(mail);
-
-               ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
-               mail.getMessage().writeTo(rawMessage,
-                               new String[] { "Bcc", "Content-Length", 
"Message-ID" });
-               String res = rawMessage.toString();
-               
-               System.out.println(res);
-               
-               Collection c = (Collection) 
mail.getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY);
-               assertNotNull(c);
-               
-               assertEquals(1,c.size());
-
-               String name = (String) c.iterator().next();
-               //System.out.println("--------------------------\n\n\n");
-               //System.out.println(name);
-               
-               
assertTrue(name.startsWith("e_Pubblicita_e_vietata_Milano9052"));
-
-               File f = new File("c:/"+name);
-               InputStream is = new FileInputStream(f);
-               String savedFile = IOUtil.toString(is);
-               is.close();
-               assertEquals(body,savedFile);
-               
-               f.delete();
-               
-       }
-       
-       public void testToAndFromAttributes() throws MessagingException, 
IOException {
-               Mailet strip = new StripAttachment();
-               MockMailetConfig mci = new MockMailetConfig("Test",new 
MockMailContext());
-               mci.setProperty("attribute","my.attribute");
-               mci.setProperty("remove","all");
-               mci.setProperty("notpattern",".*\\.tmp.*");
-               strip.init(mci);
-
-               Mailet recover = new RecoverAttachment();
-               MockMailetConfig mci2 = new MockMailetConfig("Test",new 
MockMailContext());
-               mci2.setProperty("attribute","my.attribute");
-               recover.init(mci2);
-               
-               Mailet onlyText = new OnlyText();
-               onlyText.init(new MockMailetConfig("Test", new 
MockMailContext()));
-               
-               MimeMessage message = new MimeMessage(Session
-                               .getDefaultInstance(new Properties()));
-               
-               MimeMultipart mm = new MimeMultipart();
-               MimeBodyPart mp = new MimeBodyPart();
-               mp.setText("simple text");
-               mm.addBodyPart(mp);
-               String body = "\u0023\u00A4\u00E3\u00E0\u00E9";
-               MimeBodyPart mp2 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body).getBytes()));
-               mp2.setDisposition("attachment");
-               
mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=");
-               mm.addBodyPart(mp2);
-               String body2 = "dglkjhdfòlgkj";
-               MimeBodyPart mp3 = new MimeBodyPart(new 
ByteArrayInputStream(("Content-Transfer-Encoding: 
8bit\r\n\r\n"+body2).getBytes()));
-               mp3.setDisposition("attachment");
-               mp3.setFileName("temp.zip");
-               mm.addBodyPart(mp3);
-               message.setSubject("test");
-               message.setContent(mm);
-               message.saveChanges();
-               Mail mail = new MockMail();
-               mail.setMessage(message);
-
-               assertTrue(mail.getMessage().getContent() instanceof 
MimeMultipart);
-               assertEquals(3, ((MimeMultipart) 
mail.getMessage().getContent()).getCount());
-               
-               strip.service(mail);
-
-               assertTrue(mail.getMessage().getContent() instanceof 
MimeMultipart);
-               assertEquals(1, ((MimeMultipart) 
mail.getMessage().getContent()).getCount());
-
-               onlyText.service(mail);
-
-               assertFalse(mail.getMessage().getContent() instanceof 
MimeMultipart);
-               
-               // prova per caricare il mime message da input  stream che 
altrimenti javamail si comporta differentemente.
-               String mimeSource = "Message-ID: <[EMAIL 
PROTECTED]>\r\nSubject: test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; 
charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\n\r\nsimple text";
-               
-               MimeMessage mmNew = new MimeMessage(Session
-                               .getDefaultInstance(new Properties()), new 
ByteArrayInputStream(mimeSource.getBytes()));
-               
-               mmNew.writeTo(System.out);
-
-               recover.service(mail);
-
-               assertTrue(mail.getMessage().getContent() instanceof 
MimeMultipart);
-               assertEquals(2, ((MimeMultipart) 
mail.getMessage().getContent()).getCount());
-               
-               assertEquals(body2, (((MimeMultipart) 
mail.getMessage().getContent()).getBodyPart(1).getContent()));
-               
-       }
-       
-
-       private Mailet initMailet() throws MessagingException {
-               Mailet mailet = new StripAttachment();
-
-               MockMailetConfig mci = new MockMailetConfig("Test",new 
MockMailContext());
-               mci.setProperty("directory","c:/");
-               mci.setProperty("remove","all");
-               mci.setProperty("pattern",".*\\.tmp");
-               mci.setProperty("decodeFilename", "true");
-               mci.setProperty("replaceFilenamePattern",
-                               "/[\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5]/A//,"+
-                               "/[\u00C6]/AE//,"+
-                               "/[\u00C8\u00C9\u00CA\u00CB]/E//,"+
-                               "/[\u00CC\u00CD\u00CE\u00CF]/I//,"+
-                               "/[\u00D2\u00D3\u00D4\u00D5\u00D6]/O//,"+
-                               "/[\u00D7]/x//,"+
-                               "/[\u00D9\u00DA\u00DB\u00DC]/U//,"+
-                               "/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5]/a//,"+
-                               "/[\u00E6]/ae//,"+
-                               "/[\u00E8\u00E9\u00EA\u00EB]/e//,"+
-                               "/[\u00EC\u00ED\u00EE\u00EF]/i//,"+
-                               "/[\u00F2\u00F3\u00F4\u00F5\u00F6]/o//,"+
-                               "/[\u00F9\u00FA\u00FB\u00FC]/u//,"+
-                               "/[^A-Za-z0-9._-]+/_//");
-
-               mailet.init(mci);
-               return mailet;
-       }
+        assertEquals(body2, (((MimeMultipart) mail.getMessage().getContent())
+                .getBodyPart(1).getContent()));
+
+    }
+
+    private Mailet initMailet() throws MessagingException {
+        Mailet mailet = new StripAttachment();
+
+        MockMailetConfig mci = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci.setProperty("directory", "c:/");
+        mci.setProperty("remove", "all");
+        mci.setProperty("pattern", ".*\\.tmp");
+        mci.setProperty("decodeFilename", "true");
+        mci.setProperty("replaceFilenamePattern",
+                "/[\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5]/A//,"
+                        + "/[\u00C6]/AE//,"
+                        + "/[\u00C8\u00C9\u00CA\u00CB]/E//,"
+                        + "/[\u00CC\u00CD\u00CE\u00CF]/I//,"
+                        + "/[\u00D2\u00D3\u00D4\u00D5\u00D6]/O//,"
+                        + "/[\u00D7]/x//," + "/[\u00D9\u00DA\u00DB\u00DC]/U//,"
+                        + "/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5]/a//,"
+                        + "/[\u00E6]/ae//,"
+                        + "/[\u00E8\u00E9\u00EA\u00EB]/e//,"
+                        + "/[\u00EC\u00ED\u00EE\u00EF]/i//,"
+                        + "/[\u00F2\u00F3\u00F4\u00F5\u00F6]/o//,"
+                        + "/[\u00F9\u00FA\u00FB\u00FC]/u//,"
+                        + "/[^A-Za-z0-9._-]+/_//");
+
+        mailet.init(mci);
+        return mailet;
+    }
 
 }



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

Reply via email to