james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From da...@apache.org
Subject svn commit: r469558 [2/2] - in /james/server/sandbox/mailet-refactorings/src: java/org/apache/james/ java/org/apache/james/core/ java/org/apache/james/mailrepository/ java/org/apache/james/transport/ java/org/apache/james/transport/mailets/ java/org/ap...
Date Tue, 31 Oct 2006 17:02:08 GMT
Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UnwrapText.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UnwrapText.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UnwrapText.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UnwrapText.java
Tue Oct 31 09:02:05 2006
@@ -19,16 +19,14 @@
 
 package org.apache.james.transport.mailets;
 
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.mail.MessagingException;
 import org.apache.james.util.mailet.FlowedMessageUtils;
 import org.apache.mailet.GenericMailet;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailetException;
-
-import javax.mail.MessagingException;
-
-import java.io.IOException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * Remove (best effort to) the hardcoded wrapping from a message.

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UseHeaderRecipients.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UseHeaderRecipients.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UseHeaderRecipients.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UseHeaderRecipients.java
Tue Oct 31 09:02:05 2006
@@ -21,16 +21,15 @@
 
 package org.apache.james.transport.mailets;
 
-import org.apache.mailet.GenericMailet;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
-
-import javax.mail.MessagingException;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeMessage;
 import java.util.Collection;
 import java.util.StringTokenizer;
 import java.util.Vector;
+import javax.mail.MessagingException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import org.apache.mailet.GenericMailet;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
 
 /**
  * <p>Mailet designed to process the recipients from the mail headers rather

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java
Tue Oct 31 09:02:05 2006
@@ -21,6 +21,13 @@
 
 package org.apache.james.transport.mailets;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Vector;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.james.Constants;
@@ -32,15 +39,6 @@
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.RFC2822Headers;
 import org.apache.mailet.UsersRepository;
-
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Vector;
 
 /**
  * Receives a Mail from JamesSpoolManager and takes care of delivery of the

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WhiteListManager.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WhiteListManager.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WhiteListManager.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WhiteListManager.java
Tue Oct 31 09:02:05 2006
@@ -21,28 +21,6 @@
 
 package org.apache.james.transport.mailets;
 
-import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
-import org.apache.avalon.excalibur.datasource.DataSourceComponent;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.james.Constants;
-import org.apache.james.util.JDBCUtil;
-import org.apache.james.util.SqlResources;
-import org.apache.mailet.AliasedUser;
-import org.apache.mailet.GenericMailet;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
-import org.apache.mailet.RFC2822Headers;
-import org.apache.mailet.UsersRepository;
-import org.apache.mailet.dates.RFC822DateFormat;
-
-import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -60,6 +38,26 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.james.Constants;
+import org.apache.james.util.JDBCUtil;
+import org.apache.james.util.SqlResources;
+import org.apache.mailet.AliasedUser;
+import org.apache.mailet.GenericMailet;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.RFC2822Headers;
+import org.apache.mailet.UsersRepository;
+import org.apache.mailet.dates.RFC822DateFormat;
 
 /** <P>Manages for each local user a "white list" of remote addresses whose messages
  * should never be blocked as spam.</P>

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WrapText.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WrapText.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WrapText.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/WrapText.java
Tue Oct 31 09:02:05 2006
@@ -19,14 +19,12 @@
 
 package org.apache.james.transport.mailets;
 
+import java.io.IOException;
+import javax.mail.MessagingException;
 import org.apache.james.util.mailet.FlowedMessageUtils;
 import org.apache.mailet.GenericMailet;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailetException;
-
-import javax.mail.MessagingException;
-
-import java.io.IOException;
 
 /**
  * Convert a message to format=flowed

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/XMLVirtualUserTable.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/XMLVirtualUserTable.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/XMLVirtualUserTable.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/james/transport/mailets/XMLVirtualUserTable.java
Tue Oct 31 09:02:05 2006
@@ -25,9 +25,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-
 import javax.mail.MessagingException;
-
 import org.apache.james.util.VirtualUserTableUtil;
 import org.apache.mailet.MailAddress;
 

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/Mail.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/Mail.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/Mail.java (original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/Mail.java Tue Oct
31 09:02:05 2006
@@ -203,4 +203,20 @@
      * @since Mailet API v2.3
      */
     void setLastUpdated(Date lastUpdated);
+    /**
+     * @param reversePath
+     */
+    void setSender(MailAddress reversePath);
+    /**
+     * 
+     */
+    void dispose();
+    /**
+     * @param hostAddress
+     */
+    void setRemoteAddr(String hostAddress);
+    /**
+     * @param hostName
+     */
+    void setRemoteHost(String hostName);
 }

Added: james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailFactory.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailFactory.java?view=auto&rev=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailFactory.java (added)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailFactory.java Tue
Oct 31 09:02:05 2006
@@ -0,0 +1,52 @@
+/*
+ * Created on Oct 31, 2006
+ *
+ * PVCS Workfile Details:
+ * $Workfile$
+ * $Revision$
+ * $Author$
+ * $Date$
+ * $Modtime$
+ */
+
+package org.apache.mailet;
+
+import java.util.Collection;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+
+/**
+ * @author angusd 
+ * @author $Author$ 
+ * @version $Revision$
+ */
+public interface MailFactory {
+
+    /**
+     * @param id
+     * @param sender
+     * @param recipients
+     * @param message
+     * @return
+     * @throws MessagingException 
+     */
+    Mail newMail(String id, MailAddress sender, Collection recipients, MimeMessage message)
throws MessagingException;
+
+    /**
+     * @param originalMail
+     * @return
+     * @throws MessagingException 
+     */
+    Mail newMail(Mail originalMail) throws MessagingException;
+    
+    
+}
+
+
+/* 
+ *
+ * PVCS Log History:
+ * $Log$
+ *
+ */

Modified: james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetContext.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetContext.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetContext.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetContext.java
Tue Oct 31 09:02:05 2006
@@ -286,4 +286,20 @@
      * @return an Iterator over HostAddress instances, sorted by priority
      */
     Iterator getSMTPHostAddresses(String domainName);
+    
+    
+    /**
+     * 
+     * Returns the MailRepository associated with the URL
+     * 
+     * @param repoURL
+     * @return 
+     * @throws MailetException
+     */
+    public MailRepository getMailRepository(String repoURL) throws MailetException;
+    
+    /**
+     * @return a MailFactory implementation
+     */
+    public MailFactory getMailFactory();
 }

Added: james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetUtil.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetUtil.java?view=auto&rev=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetUtil.java (added)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MailetUtil.java Tue
Oct 31 09:02:05 2006
@@ -0,0 +1,87 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+
+
+package org.apache.mailet;
+
+
+/**
+ * The Util class provide some static methods which can be usefull in mailets
+ */
+public class MailetUtil {
+    
+    /**
+     * <p>This takes the subject string and reduces (normailzes) it.
+     * Multiple "Re:" entries are reduced to one, and capitalized.  The
+     * prefix is always moved/placed at the beginning of the line, and
+     * extra blanks are reduced, so that the output is always of the
+     * form:</p>
+     * <code>
+     * &lt;prefix&gt; + &lt;one-optional-"Re:"*gt; + &lt;remaining subject&gt;
+     * </code>
+     * <p>I have done extensive testing of this routine with a standalone
+     * driver, and am leaving the commented out debug messages so that
+     * when someone decides to enhance this method, it can be yanked it
+     * from this file, embedded it with a test driver, and the comments
+     * enabled.</p>
+     */
+    public static String normalizeSubject(String subj, String prefix) {
+        StringBuffer subject = new StringBuffer(subj);
+        int prefixLength = prefix.length();
+
+        // If the "prefix" is not at the beginning the subject line, remove it
+        int index = subject.indexOf(prefix);
+        if (index != 0) {
+
+            if (index > 0) {
+                subject.delete(index, index + prefixLength);
+            }
+            subject.insert(0, prefix); // insert prefix at the front
+        }
+
+        // Replace Re: with RE:
+        String match = "Re:";
+        index = subject.indexOf(match, prefixLength);
+
+        while(index > -1) {
+            subject.replace(index, index + match.length(), "RE:");
+            index = subject.indexOf(match, prefixLength);
+        }
+
+        // Reduce them to one at the beginning
+        match ="RE:";
+        int indexRE = subject.indexOf(match, prefixLength) + match.length();
+        index = subject.indexOf(match, indexRE);
+        while(index > 0) {    
+            subject.delete(index, index + match.length());
+            index = subject.indexOf(match, indexRE);
+        }
+
+        // Reduce blanks
+        match = "  ";
+        index = subject.indexOf(match, prefixLength);
+        while(index > -1) {
+            subject.replace(index, index + match.length(), " ");
+            index = subject.indexOf(match, prefixLength);
+        }
+        return subject.toString();
+    }
+
+}

Added: james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MimeMessageUtil.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MimeMessageUtil.java?view=auto&rev=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MimeMessageUtil.java
(added)
+++ james/server/sandbox/mailet-refactorings/src/java/org/apache/mailet/MimeMessageUtil.java
Tue Oct 31 09:02:05 2006
@@ -0,0 +1,339 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+
+package org.apache.mailet;
+
+import org.apache.james.core.MimeMessageCopyOnWriteProxy;
+import org.apache.james.core.MimeMessageWrapper;
+import org.apache.james.util.InternetPrintWriter;
+import org.apache.james.util.io.IOUtil;
+
+import javax.activation.UnsupportedDataTypeException;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.Enumeration;
+
+/**
+ * Utility class to provide optimized write methods for the various MimeMessage
+ * implementations.
+ */
+public class MimeMessageUtil {
+
+    /**
+     * Convenience method to take any MimeMessage and write the headers and body to two
+     * different output streams
+     * 
+     * @param message the MimeMessage reading from
+     * @param headerOs the OutputStream writting the headers to
+     * @param bodyOs the OutputStream writting the body to
+     * @throws IOException get thrown if an IO Error detected while writing to the streams
+     * @throws MessagingException get thrown if an error detected while reading the message
+     */
+    public static void writeTo(MimeMessage message, OutputStream headerOs, OutputStream bodyOs)
throws IOException, MessagingException {
+        writeTo(message, headerOs, bodyOs, null);
+    }
+
+    /**
+     * Convenience method to take any MimeMessage and write the headers and body to two
+     * different output streams, with an ignore list
+     * 
+     * @param message the MimeMessage reading from
+     * @param headerOs the OutputStream writting the headers to
+     * @param bodyOs the OutputStream writting the body to
+     * @param ignoreList the String[] which contains headers which should be ignored
+     * @throws IOException get thrown if an IO Error detected while writing to the streams
+     * @throws MessagingException get thrown if an error detected while reading the message
+     */
+    public static void writeTo(MimeMessage message, OutputStream headerOs, OutputStream bodyOs,
String[] ignoreList) throws IOException, MessagingException {
+        MimeMessage testMessage = message;
+        if (message instanceof MimeMessageCopyOnWriteProxy) {
+            MimeMessageCopyOnWriteProxy wr = (MimeMessageCopyOnWriteProxy) message;
+            testMessage = wr.getWrappedMessage();
+        }
+        if (testMessage instanceof MimeMessageWrapper) {
+            MimeMessageWrapper wrapper = (MimeMessageWrapper)testMessage;
+            if (!wrapper.isModified()) {
+                wrapper.writeTo(headerOs, bodyOs, ignoreList);
+                return;
+            }
+        }
+        writeToInternal(message, headerOs, bodyOs, ignoreList);
+    }
+
+    /**
+     * 
+     * @param message
+     * @param headerOs
+     * @param bodyOs
+     * @param ignoreList
+     * @throws MessagingException
+     * @throws IOException
+     * @throws UnsupportedDataTypeException
+     */
+    public static void writeToInternal(MimeMessage message, OutputStream headerOs, OutputStream
bodyOs, String[] ignoreList) throws MessagingException, IOException, UnsupportedDataTypeException
{
+        if(message.getMessageID() == null) {
+            message.saveChanges();
+        }
+
+        writeHeadersTo(message, headerOs, ignoreList);
+
+        // Write the body to the output stream
+        writeMessageBodyTo(message, bodyOs);
+    }
+
+    /**
+     * Write message body of given mimeessage to the given outputStream
+     * 
+     * @param message the MimeMessage used as input 
+     * @param bodyOs the OutputStream to write the message body to
+     * @throws IOException 
+     * @throws UnsupportedDataTypeException
+     * @throws MessagingException
+     */
+    public static void writeMessageBodyTo(MimeMessage message, OutputStream bodyOs) throws
IOException, UnsupportedDataTypeException, MessagingException {
+        OutputStream bos;
+        InputStream bis;
+
+        try {
+            // Get the message as a stream.  This will encode
+            // objects as necessary, and we have some input from
+            // decoding an re-encoding the stream.  I'd prefer the
+            // raw stream, but see
+            bos = MimeUtility.encode(bodyOs, message.getEncoding());
+            bis = message.getInputStream();
+        } catch(UnsupportedDataTypeException udte) {
+            /* If we get an UnsupportedDataTypeException try using
+             * the raw input stream as a "best attempt" at rendering
+             * a message.
+             *
+             * WARNING: JavaMail v1.3 getRawInputStream() returns
+             * INVALID (unchanged) content for a changed message.
+             * getInputStream() works properly, but in this case
+             * has failed due to a missing DataHandler.
+             *
+             * MimeMessage.getRawInputStream() may throw a "no
+             * content" MessagingException.  In JavaMail v1.3, when
+             * you initially create a message using MimeMessage
+             * APIs, there is no raw content available.
+             * getInputStream() works, but getRawInputStream()
+             * throws an exception.  If we catch that exception,
+             * throw the UDTE.  It should mean that someone has
+             * locally constructed a message part for which JavaMail
+             * doesn't have a DataHandler.
+            */
+
+            try {
+                bis = message.getRawInputStream();
+                bos = bodyOs;
+            } catch(javax.mail.MessagingException _) {
+                throw udte;
+            }
+        }
+        catch(javax.mail.MessagingException me) {
+            /* This could be another kind of MessagingException
+             * thrown by MimeMessage.getInputStream(), such as a
+             * javax.mail.internet.ParseException.
+             *
+             * The ParseException is precisely one of the reasons
+             * why the getRawInputStream() method exists, so that we
+             * can continue to stream the content, even if we cannot
+             * handle it.  Again, if we get an exception, we throw
+             * the one that caused us to call getRawInputStream().
+             */
+            try {
+                bis = message.getRawInputStream();
+                bos = bodyOs;
+            } catch(javax.mail.MessagingException _) {
+                throw me;
+            }
+        }
+
+        try {
+            copyStream(bis, bos);
+        }
+        finally {
+            IOUtil.shutdownStream(bis);
+        }
+    }
+
+    /**
+     * Convenience method to copy streams
+     *
+     * @param in the InputStream used as copy source 
+     * @param out the OutputStram used as copy destination
+     * @throws IOException
+     */
+    public static void copyStream(InputStream in, OutputStream out) throws IOException {
+        // TODO: This is really a bad way to do this sort of thing.  A shared buffer to
+        //       allow simultaneous read/writes would be a substantial improvement
+        byte[] block = new byte[1024];
+        int read = 0;
+        while ((read = in.read(block)) > -1) {
+            out.write(block, 0, read);
+        }
+        out.flush();
+    }
+
+
+    /**
+     * Write the message headers to the given outputstream
+     * 
+     * @param message the MimeMessage to read from
+     * @param headerOs the OutputStream to which the headers get written
+     * @param ignoreList the String[] which holds headers which should be ignored 
+     * @throws MessagingException
+     */
+    private static void writeHeadersTo(MimeMessage message, OutputStream headerOs, String[]
ignoreList) throws MessagingException {
+        //Write the headers (minus ignored ones)
+        Enumeration headers = message.getNonMatchingHeaderLines(ignoreList);
+        writeHeadersTo(headers, headerOs);
+    }
+
+    /**
+     * Write the message headers to the given outputstream
+     * 
+     * @param headers the Enumeration which holds the headers
+     * @param headerOs the OutputStream to which the headers get written
+     * @throws MessagingException
+     */
+    public static void writeHeadersTo(Enumeration headers, OutputStream headerOs) throws
MessagingException {
+        PrintWriter hos = new InternetPrintWriter(new BufferedWriter(new OutputStreamWriter(headerOs),
512), true);
+        while (headers.hasMoreElements()) {
+            hos.println((String)headers.nextElement());
+        }
+        // Print header/data separator
+        hos.println();
+        hos.flush();
+    }
+    
+    /**
+     * Get an InputStream which holds all headers of the given MimeMessage
+     * 
+     * @param message the MimeMessage used as source
+     * @param ignoreList the String[] which holds headers which should be ignored 
+     * @return stream the InputStream which holds the headers
+     * @throws MessagingException
+     */
+    public static InputStream getHeadersInputStream(MimeMessage message, String[] ignoreList)
throws MessagingException {
+        ByteArrayOutputStream bo = new ByteArrayOutputStream();
+        writeHeadersTo(message,bo,ignoreList);
+        return new ByteArrayInputStream(bo.toByteArray());
+    }
+
+
+    /**
+     * Slow method to calculate the exact size of a message!
+     */
+    private static final class SizeCalculatorOutputStream extends OutputStream {
+        long size = 0;
+
+        public void write(int arg0) throws IOException {
+            size++;
+        }
+
+        public long getSize() {
+            return size;
+        }
+
+        public void write(byte[] arg0, int arg1, int arg2) throws IOException {
+            size += arg2;
+        }
+
+        public void write(byte[] arg0) throws IOException {
+            size += arg0.length;
+        }
+    }
+
+    /**
+     * Return the full site of an mimeMessage 
+     * 
+     * @return size of full message including headers
+     * @throws MessagingException if a problem occours while computing the message size
+     */
+    public static long getMessageSize(MimeMessage message) throws MessagingException {
+        //If we have a MimeMessageWrapper, then we can ask it for just the
+        //  message size and skip calculating it
+        long size = -1;
+        
+        if (message instanceof MimeMessageWrapper) {
+            MimeMessageWrapper wrapper = (MimeMessageWrapper) message;
+            size = wrapper.getMessageSize();
+        } else if (message instanceof MimeMessageCopyOnWriteProxy) {
+            MimeMessageCopyOnWriteProxy wrapper = (MimeMessageCopyOnWriteProxy) message;
+            size = wrapper.getMessageSize();
+        }
+        
+        if (size == -1) {
+            size = calculateMessageSize(message);
+        }
+        
+        return size;
+    }
+
+    /**
+     * Calculate the size of the give mimeMessage
+     * 
+     * @param message the MimeMessage 
+     * @return size the calculated size
+     * @throws MessagingException if a problem occours while calculate the message size
+     */
+    public static long calculateMessageSize(MimeMessage message) throws MessagingException
{
+        long size;
+        //SK: Should probably eventually store this as a locally
+        //  maintained value (so we don't have to load and reparse
+        //  messages each time).
+        size = message.getSize();
+        if (size != -1) {
+            Enumeration e = message.getAllHeaderLines();
+            if (e.hasMoreElements()) {
+                size += 2;
+            }
+            while (e.hasMoreElements()) {
+                // add 2 bytes for the CRLF
+                size += ((String) e.nextElement()).length()+2;
+            }
+        }
+        
+        
+        if (size == -1) {
+            SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
+            try {
+                message.writeTo(out);
+            } catch (IOException e) {
+                // should never happen as SizeCalculator does not actually throw IOExceptions.
+                throw new MessagingException("IOException wrapped by getMessageSize",e);
+            }
+            size = out.getSize();
+        }
+        return size;
+    }
+    
+
+}

Modified: james/server/sandbox/mailet-refactorings/src/test/org/apache/james/core/MimeMessageUtilTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/mailet-refactorings/src/test/org/apache/james/core/MimeMessageUtilTest.java?view=diff&rev=469558&r1=469557&r2=469558
==============================================================================
--- james/server/sandbox/mailet-refactorings/src/test/org/apache/james/core/MimeMessageUtilTest.java
(original)
+++ james/server/sandbox/mailet-refactorings/src/test/org/apache/james/core/MimeMessageUtilTest.java
Tue Oct 31 09:02:05 2006
@@ -24,6 +24,7 @@
 import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.internet.MimeMessage;
+import org.apache.mailet.MimeMessageUtil;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;



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


Mime
View raw message