You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/08/23 20:14:42 UTC

svn commit: r1160830 - in /james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store: LazyMimeDescriptor.java MessageResultImpl.java MimeDescriptorImpl.java ResultUtils.java

Author: norman
Date: Tue Aug 23 18:14:42 2011
New Revision: 1160830

URL: http://svn.apache.org/viewvc?rev=1160830&view=rev
Log:
Lazy load and populate the MimeDescriptor. See MAILBOX-116

Added:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java   (with props)
Modified:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MimeDescriptorImpl.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java?rev=1160830&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java Tue Aug 23 18:14:42 2011
@@ -0,0 +1,171 @@
+/****************************************************************
+ * 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.james.mailbox.store;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MessageResult;
+import org.apache.james.mailbox.MessageResult.Header;
+import org.apache.james.mailbox.MimeDescriptor;
+import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+
+/**
+ * A {@link MimeDescriptor} implementation which tries to optimize the way the data
+ * is loading by using it in a lazy fashion whenever possible.
+ * 
+ *
+ */
+public class LazyMimeDescriptor implements MimeDescriptor{
+
+    private final Message<?> message;
+    private final MessageResult result;
+    private PropertyBuilder pbuilder;
+    
+    public LazyMimeDescriptor(final MessageResult result, final Message<?> message) {
+        this.message = message;
+        this.result = result;
+    }
+    
+    
+    @Override
+    public Iterator<Header> headers() throws MailboxException {
+        return result.getHeaders().headers();
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        try {
+            return result.getHeaders().getInputStream();
+        } catch (MailboxException e) {
+            throw new IOException("Unable to retrieve content", e);
+        }
+    }
+
+    @Override
+    public long size() throws MailboxException {
+        return result.getHeaders().size();
+    }
+
+    @Override
+    public String getMimeType() {
+        return message.getMediaType();
+    }
+
+    @Override
+    public String getMimeSubType() {
+        return message.getSubType();
+    }
+
+    @Override
+    public String getContentID() {
+        return getPropertyBuilder().getContentID();
+    }
+
+    @Override
+    public String getContentDescription() {
+        return getPropertyBuilder().getContentDescription();
+    }
+
+    @Override
+    public String getContentLocation() {
+        return getPropertyBuilder().getContentLocation();
+    }
+
+    @Override
+    public String getContentMD5() {
+        return getPropertyBuilder().getContentMD5();
+    }
+
+    @Override
+    public String getTransferContentEncoding() {
+        return getPropertyBuilder().getContentTransferEncoding();
+    }
+
+    @Override
+    public List<String> getLanguages() {
+        return getPropertyBuilder().getContentLanguage();
+    }
+
+    @Override
+    public String getDisposition() {
+        return getPropertyBuilder().getContentDispositionType();
+    }
+
+    @Override
+    public Map<String, String> getDispositionParams() {
+        return getPropertyBuilder().getContentDispositionParameters();
+
+    }
+
+    @Override
+    public long getLines() {
+        Long count =  message.getTextualLineCount();
+        if (count == null) {
+            return -1;
+        } else {
+            return count;
+        }
+    }
+
+    @Override
+    public long getBodyOctets() {
+        return message.getBodyOctets();
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public Iterator<MimeDescriptor> parts() {
+        return Collections.EMPTY_LIST.iterator();
+    }
+
+    /**
+     * Return null
+     */
+    @Override
+    public MimeDescriptor embeddedMessage() {
+        return null;
+    }
+
+    @Override
+    public Map<String, String> contentTypeParameters() {
+        return getPropertyBuilder().getContentTypeParameters();
+    }
+
+    /**
+     * Return a {@link PropertyBuilder} which is created in a lazy fashion if it not exist yet.
+     * This is done as it may be expensive to retrieve the properties of the message.
+     * 
+     * @return pbuilder
+     */
+    private PropertyBuilder getPropertyBuilder() {
+        if (pbuilder == null) {
+            pbuilder = new PropertyBuilder(message.getProperties());
+        }
+        return pbuilder;
+    }
+    
+}

Propchange: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/LazyMimeDescriptor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java?rev=1160830&r1=1160829&r2=1160830&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MessageResultImpl.java Tue Aug 23 18:14:42 2011
@@ -37,6 +37,7 @@ import org.apache.james.mailbox.MimeDesc
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mailbox.store.streaming.InputStreamContent;
 import org.apache.james.mailbox.store.streaming.InputStreamContent.Type;
+import org.apache.james.mime4j.MimeException;
 
 /**
  * Bean based implementation.
@@ -337,16 +338,28 @@ public class MessageResultImpl implement
         }
     }
 
-    public void setMimeDescriptor(final MimeDescriptor mimeDescriptor) {
-        this.mimeDescriptor = mimeDescriptor;
-    }
-
     /*
      * (non-Javadoc)
      * 
      * @see org.apache.james.mailbox.MessageResult#getMimeDescriptor()
      */
-    public MimeDescriptor getMimeDescriptor() {
+    public MimeDescriptor getMimeDescriptor() throws MailboxException {
+        
+        // check if we need to create the MimeDescriptor which is done in a lazy fashion because
+        // it can be relative expensive on big messages and slow mailbox implementations
+        if (mimeDescriptor == null) {
+            try {
+                if (MimeDescriptorImpl.isComposite(message.getMediaType())) {
+                    mimeDescriptor = MimeDescriptorImpl.build(getFullContent().getInputStream());
+                } else {
+                    mimeDescriptor = new LazyMimeDescriptor(this, message);
+                }
+            } catch (IOException e) {
+                throw new MailboxException("Unable to create the MimeDescriptor", e);
+            } catch (MimeException e) {
+                throw new MailboxException("Unable to create the MimeDescriptor", e);
+            }
+        }
         return mimeDescriptor;
     }
 

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MimeDescriptorImpl.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MimeDescriptorImpl.java?rev=1160830&r1=1160829&r2=1160830&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MimeDescriptorImpl.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/MimeDescriptorImpl.java Tue Aug 23 18:14:42 2011
@@ -33,9 +33,6 @@ import java.util.TreeMap;
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MessageResult;
 import org.apache.james.mailbox.MimeDescriptor;
-import org.apache.james.mailbox.store.mail.model.Message;
-import org.apache.james.mailbox.store.mail.model.Property;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 import org.apache.james.mailbox.store.streaming.CountingInputStream;
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.message.DefaultBodyDescriptorBuilder;
@@ -48,37 +45,7 @@ import org.apache.james.mime4j.stream.Re
 public class MimeDescriptorImpl implements MimeDescriptor {
 
     private final static Charset US_ASCII = Charset.forName("US-ASCII");
-    
-    public static MimeDescriptorImpl build(final Message<?> document) throws IOException, MimeException {
-        final MimeDescriptorImpl result;
-        final String mediaType = document.getMediaType();
-        if (isComposite(mediaType)) {
-            result = build(ResultUtils.toInput(document));
-        } else {
-            final List<MessageResult.Header> headers = ResultUtils.createHeaders(document);
-            final List<Property> properties = document.getProperties();
-            final PropertyBuilder builder = new PropertyBuilder(properties);
-            final Long textualLineCount = document.getTextualLineCount();
-            result = new MimeDescriptorImpl(
-                    document.getBodyOctets(), 
-                    builder.getContentDescription(), 
-                    builder.getContentID(), 
-                    textualLineCount == null? -1: textualLineCount.longValue(), 
-                    document.getSubType(),
-                    mediaType, 
-                    builder.getContentTransferEncoding(), 
-                    headers, 
-                    builder.getContentTypeParameters(),
-                    builder.getContentLanguage(), 
-                    builder.getContentDispositionType(), 
-                    builder.getContentDispositionParameters(), 
-                    null,
-                    new ArrayList<MimeDescriptor>(), 
-                    builder.getContentLocation(), 
-                    builder.getContentMD5());
-        }
-        return result;
-    }
+
     
     /**
      * Is this a composite media type (as per RFC2045)?

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java?rev=1160830&r1=1160829&r2=1160830&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultUtils.java Tue Aug 23 18:14:42 2011
@@ -35,10 +35,7 @@ import org.apache.james.mailbox.MailboxE
 import org.apache.james.mailbox.MessageResult;
 import org.apache.james.mailbox.MessageResult.FetchGroup;
 import org.apache.james.mailbox.MessageResult.MimePath;
-import org.apache.james.mailbox.MimeDescriptor;
 import org.apache.james.mailbox.store.mail.model.Message;
-import org.apache.james.mailbox.store.streaming.InputStreamContent;
-import org.apache.james.mailbox.store.streaming.InputStreamContent.Type;
 import org.apache.james.mailbox.store.streaming.PartContentBuilder;
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.parser.AbstractContentHandler;
@@ -105,30 +102,8 @@ public class ResultUtils {
         return results;
     }
 
-    /**
-     * Return the {@link Content} which holds only the Body for the given {@link MailboxMembership}
-     * 
-     * @param membership
-     * @return bodyContent
-     * @throws IOException 
-     */
-    public static Content createBodyContent(Message<?> membership) throws IOException {
-        final InputStreamContent result = new InputStreamContent(membership, Type.Body);
-        return result;
-    }
-
-    /**
-     * Return the {@link Content} which holds the full data for the given {@link MailboxMembership}
-     * 
-     * @param membership
-     * @return content
-     * @throws IOException 
-     */
-    public static Content createFullContent(final Message<?> membership) throws IOException {
-        final InputStreamContent result = new InputStreamContent(membership, Type.Full);
-        return result;
-    }
-
+  
+   
     /**
      * Return an {@link InputStream} which holds the full content of the message
      * @param message
@@ -168,7 +143,6 @@ public class ResultUtils {
                     content -= FetchGroup.FULL_CONTENT;
                 }
                 if ((content & FetchGroup.MIME_DESCRIPTOR) > 0) {
-                    addMimeDescriptor(message, messageResult);
                     content -= FetchGroup.MIME_DESCRIPTOR;
                 }
                 if (content != 0) {
@@ -187,12 +161,6 @@ public class ResultUtils {
 
     }
 
-    private static void addMimeDescriptor(Message<?> message, MessageResultImpl messageResult) throws IOException, MimeException {
-            MimeDescriptor descriptor = MimeDescriptorImpl.build(message);
-            messageResult.setMimeDescriptor(descriptor);
-    }
-
-
     private static void addPartContent(final FetchGroup fetchGroup,
             Message<?> message, MessageResultImpl messageResult)
             throws MailboxException, IOException,



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