Author: rdonkin
Date: Sun Nov 25 08:48:31 2007
New Revision: 598007

URL: http://svn.apache.org/viewvc?rev=598007&view=rev
Log:
Removed deprecated LegacyFetchResponse. Moved (some) encoding from the 
processor into the encoder (where it belongs).

Removed:
    
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/LegacyFetchResponse.java
Modified:
    
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/FetchResponse.java
    
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/ImapResponseComposer.java
    
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/base/ImapResponseComposerImpl.java
    
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoder.java
    
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/ImapResponseTest.java
    
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoderTest.java
    
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/base/SelectedMailboxSessionImpl.java
    
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/FetchProcessor.java
    
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/StoreProcessor.java

Modified: 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/FetchResponse.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/FetchResponse.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/FetchResponse.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imap/message/response/imap4rev1/FetchResponse.java
 Sun Nov 25 08:48:31 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.james.imap.message.response.imap4rev1;
 
+import java.util.Date;
+
 import javax.mail.Flags;
 
 import org.apache.james.api.imap.message.response.ImapResponseMessage;
@@ -27,12 +29,21 @@
         private final int messageNumber;
         private final Flags flags;
         private final Long uid;
+        private final Date internalDate;
+        private final Integer size;
+        private final StringBuffer misc;
+        private final StringBuffer elements;
         
-        public FetchResponse(final int messageNumber, final Flags flags, final 
Long uid) {
+        public FetchResponse(final int messageNumber, final Flags flags, final 
Long uid,
+                final Date internalDate, final Integer size, StringBuffer 
misc, StringBuffer elements) {
             super();
             this.messageNumber = messageNumber;
             this.flags = flags;
             this.uid = uid;
+            this.internalDate = internalDate;
+            this.size = size;
+            this.misc = misc;
+            this.elements = elements;
         }
         
         /**
@@ -61,4 +72,40 @@
         public Long getUid() {
             return uid;
         }
+
+        /**
+         * Gets the internal date for the fetched message.
+         * @return the internalDate,
+         * or null if the <code>FETCH</code> did not include 
<code>INTERNALDATE</code>
+         */
+        public final Date getInternalDate() {
+            return internalDate;
+        }
+
+        /**
+         * Gets the size for the fetched message.
+         * @return the size,
+         * or null if the <code>FETCH</code> did not include <code>SIZE</code>
+         */
+        public final Integer getSize() {
+            return size;
+        }
+
+        /**
+         * TODO: replace
+         * @return the elements
+         */
+        public final StringBuffer getElements() {
+            return elements;
+        }
+
+        /**
+         * TODO: replace
+         * @return the misc
+         */
+        public final StringBuffer getMisc() {
+            return misc;
+        }
+        
+        
 }

Modified: 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/ImapResponseComposer.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/ImapResponseComposer.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/ImapResponseComposer.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/ImapResponseComposer.java
 Sun Nov 25 08:48:31 2007
@@ -174,12 +174,6 @@
      */
     public abstract void closeFetchResponse() throws IOException;
     
-    /**
-     * @deprecated
-     * @see #openFetchResponse(long)
-     */
-    public abstract void legacyFetchResponse(int msn, String msgData) throws 
IOException;
-
     public abstract void commandResponse(ImapCommand command, String message) 
throws IOException;
 
     /**
@@ -231,5 +225,7 @@
 
     public abstract void statusResponse(String tag, ImapCommand command,
             String type, String responseCode, String text) throws IOException;
+    
+    public void quote(String message) throws IOException;
 
 }

Modified: 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/base/ImapResponseComposerImpl.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/base/ImapResponseComposerImpl.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/base/ImapResponseComposerImpl.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/base/ImapResponseComposerImpl.java
 Sun Nov 25 08:48:31 2007
@@ -222,18 +222,6 @@
 
     /**
      * @throws IOException 
-     * @see 
org.apache.james.imapserver.codec.encode.ImapResponseComposer#legacyFetchResponse(int,
 java.lang.String)
-     */
-    public void legacyFetchResponse(int msn, String msgData) throws 
IOException {
-        untagged();
-        message(msn);
-        message(FETCH);
-        message("(" + msgData + ")");
-        end();
-    }
-
-    /**
-     * @throws IOException 
      * @see 
org.apache.james.imapserver.codec.encode.ImapResponseComposer#commandResponse(org.apache.james.api.imap.ImapCommand,
 java.lang.String)
      */
     public void commandResponse(ImapCommand command, String message) throws 
IOException {

Modified: 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoder.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoder.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoder.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/main/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoder.java
 Sun Nov 25 08:48:31 2007
@@ -20,13 +20,14 @@
 package org.apache.james.imapserver.codec.encode.imap4rev1;
 
 import java.io.IOException;
+import java.util.Date;
 
 import javax.mail.Flags;
 
 import org.apache.james.api.imap.ImapConstants;
 import org.apache.james.api.imap.ImapMessage;
 import org.apache.james.imap.message.response.imap4rev1.FetchResponse;
-import org.apache.james.imap.message.response.imap4rev1.LegacyFetchResponse;
+import org.apache.james.imapserver.codec.encode.EncoderUtils;
 import org.apache.james.imapserver.codec.encode.ImapEncoder;
 import org.apache.james.imapserver.codec.encode.ImapResponseComposer;
 import 
org.apache.james.imapserver.codec.encode.base.AbstractChainedImapEncoder;
@@ -38,8 +39,7 @@
     }
 
     public boolean isAcceptable(final ImapMessage message) {
-        return (message instanceof LegacyFetchResponse) 
-            || (message instanceof FetchResponse);
+        return (message instanceof FetchResponse);
     }
 
     protected void doEncode(ImapMessage acceptableMessage, 
ImapResponseComposer composer) throws IOException {
@@ -47,27 +47,53 @@
             final FetchResponse fetchResponse = (FetchResponse) 
acceptableMessage;
             final long messageNumber = fetchResponse.getMessageNumber();
             composer.openFetchResponse(messageNumber);
-            final Flags flags = fetchResponse.getFlags();
-            if (flags != null) {
-                composer.flags(flags);
-            }
-            final Long uid = fetchResponse.getUid();
-            if (uid != null) {
-                composer.message(ImapConstants.UID);
-                composer.message(uid.longValue());
-            }
+            encodeFlags(composer, fetchResponse);
+            encodeInternalDate(composer, fetchResponse);
+            encodeSize(composer, fetchResponse);
+            encode(composer, fetchResponse.getMisc());
+            encodeUid(composer, fetchResponse);
+            encode(composer, fetchResponse.getElements());
             composer.closeFetchResponse();
-        } else {
-            final LegacyFetchResponse fetchResponse = (LegacyFetchResponse) 
acceptableMessage;
-            encodeLegacy(composer, fetchResponse);
+        }
+    }
+    
+    private void encode(ImapResponseComposer composer, StringBuffer buffer) 
throws IOException {
+        if (buffer != null && buffer.length() > 0) {
+            composer.message(buffer.substring(1));
+        }
+    }
+
+    private void encodeSize(ImapResponseComposer composer, final FetchResponse 
fetchResponse) throws IOException {
+        final Integer size = fetchResponse.getSize();
+        if (size != null) {
+            // TODO: add method to composer
+            composer.message("RFC822.SIZE");
+            composer.message(size.intValue());
+        }
+    }
+
+    private void encodeInternalDate(ImapResponseComposer composer, final 
FetchResponse fetchResponse) throws IOException {
+        final Date internalDate = fetchResponse.getInternalDate();
+        if (internalDate != null) {
+            // TODO: add method to composer
+            composer.message("INTERNALDATE");
+            composer.quote(EncoderUtils.encodeDateTime(internalDate));
+        }
+    }
+
+    private void encodeUid(ImapResponseComposer composer, final FetchResponse 
fetchResponse) throws IOException {
+        final Long uid = fetchResponse.getUid();
+        if (uid != null) {
+            composer.message(ImapConstants.UID);
+            composer.message(uid.longValue());
         }
     }
 
-    private void encodeLegacy(ImapResponseComposer composer, final 
LegacyFetchResponse fetchResponse) throws IOException {
-        // TODO: this is inefficient
-        final String data = fetchResponse.getData();
-        final int number = fetchResponse.getNumber();
-        composer.legacyFetchResponse(number, data);
+    private void encodeFlags(ImapResponseComposer composer, final 
FetchResponse fetchResponse) throws IOException {
+        final Flags flags = fetchResponse.getFlags();
+        if (flags != null) {
+            composer.flags(flags);
+        }
     }
 
 }

Modified: 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/ImapResponseTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/ImapResponseTest.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/ImapResponseTest.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/ImapResponseTest.java
 Sun Nov 25 08:48:31 2007
@@ -141,23 +141,6 @@
                 writer.operations.get(3));
     }
 
-    public void testFetchResponse() throws Exception {
-        int count = 7;
-        String data = "Some data";
-        response.legacyFetchResponse(count, data);
-        assertEquals(5, writer.operations.size());
-        assertEquals(new MockImapResponseWriter.UntaggedOperation(), 
writer.operations.get(0));
-        assertEquals(new 
MockImapResponseWriter.NumericMessageOperation(count), 
-                writer.operations.get(1));
-        assertEquals(new 
MockImapResponseWriter.TextMessageOperation(ImapResponseComposerImpl.FETCH),
-                writer.operations.get(2));
-        assertEquals(new MockImapResponseWriter.TextMessageOperation("(" + 
data + ")"),
-                writer.operations.get(3));
-        assertEquals(new MockImapResponseWriter.EndOperation(), 
-                writer.operations.get(4));
-    }
-
-
     public void testTaggedResponse() throws Exception {
         String message = "A message";
         response.taggedResponse(message, TAG);

Modified: 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoderTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoderTest.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoderTest.java
 (original)
+++ 
james/server/trunk/imap-codec-library/src/test/java/org/apache/james/imapserver/codec/encode/imap4rev1/FetchResponseEncoderTest.java
 Sun Nov 25 08:48:31 2007
@@ -57,11 +57,11 @@
     }
     
     public void testShouldAcceptFetchResponse() throws Exception {
-        assertTrue(encoder.isAcceptable(new FetchResponse(11, null, null)));
+        assertTrue(encoder.isAcceptable(new FetchResponse(11, null, null, 
null, null, null, null)));
     }
     
     public void testShouldEncodeFlagsResponse() throws Exception {
-        FetchResponse message = new FetchResponse(100, flags, null);
+        FetchResponse message = new FetchResponse(100, flags, null, null, 
null, null, null);
         
mockComposer.expects(once()).method("openFetchResponse").with(eq(100L));
         mockComposer.expects(once()).method("flags").with(eq(flags));
         mockComposer.expects(once()).method("closeFetchResponse");
@@ -69,7 +69,7 @@
     }
     
     public void testShouldEncodeUidResponse() throws Exception {
-        FetchResponse message = new FetchResponse(100, null, new Long(72));
+        FetchResponse message = new FetchResponse(100, null, new Long(72), 
null, null, null, null);
         
mockComposer.expects(once()).method("openFetchResponse").with(eq(100L));
         mockComposer.expects(once()).method("message").with(eq("UID"));
         mockComposer.expects(once()).method("message").with(eq(72L));
@@ -78,7 +78,7 @@
     }
     
     public void testShouldEncodeAllResponse() throws Exception {
-        FetchResponse message = new FetchResponse(100, flags, new Long(72));
+        FetchResponse message = new FetchResponse(100, flags, new Long(72), 
null, null, null, null);
         
mockComposer.expects(once()).method("openFetchResponse").with(eq(100L));
         mockComposer.expects(once()).method("flags").with(eq(flags));
         mockComposer.expects(once()).method("message").with(eq("UID"));

Modified: 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/base/SelectedMailboxSessionImpl.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/base/SelectedMailboxSessionImpl.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/base/SelectedMailboxSessionImpl.java
 (original)
+++ 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/base/SelectedMailboxSessionImpl.java
 Sun Nov 25 08:48:31 2007
@@ -141,7 +141,7 @@
                     } else {
                         uidOut = null;
                     }
-                    FetchResponse response = new FetchResponse(msn, flags, 
uidOut);
+                    FetchResponse response = new FetchResponse(msn, flags, 
uidOut, null, null, null, null);
                     responses.add(response);
                 }
             }

Modified: 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/FetchProcessor.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/FetchProcessor.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/FetchProcessor.java
 (original)
+++ 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/FetchProcessor.java
 Sun Nov 25 08:48:31 2007
@@ -21,12 +21,14 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
 import javax.mail.Flags;
 import javax.mail.MessagingException;
 
+import org.apache.avalon.framework.logger.Logger;
 import org.apache.james.api.imap.ImapCommand;
 import org.apache.james.api.imap.ImapConstants;
 import org.apache.james.api.imap.ImapMessage;
@@ -35,14 +37,12 @@
 import org.apache.james.api.imap.message.BodyFetchElement;
 import org.apache.james.api.imap.message.FetchData;
 import org.apache.james.api.imap.message.IdRange;
-import org.apache.james.api.imap.message.MessageFlags;
 import org.apache.james.api.imap.message.request.ImapRequest;
 import 
org.apache.james.api.imap.message.response.imap4rev1.StatusResponseFactory;
 import org.apache.james.api.imap.process.ImapProcessor;
 import org.apache.james.api.imap.process.ImapSession;
 import org.apache.james.imap.message.request.imap4rev1.FetchRequest;
-import org.apache.james.imap.message.response.imap4rev1.LegacyFetchResponse;
-import org.apache.james.imapserver.codec.encode.EncoderUtils;
+import org.apache.james.imap.message.response.imap4rev1.FetchResponse;
 import org.apache.james.imapserver.processor.base.AbstractImapRequestProcessor;
 import org.apache.james.imapserver.processor.base.AuthorizationException;
 import org.apache.james.imapserver.processor.base.ImapSessionUtils;
@@ -86,17 +86,13 @@
             int resultToFetch = getNeededMessageResult(fetch);
             ImapMailboxSession mailbox = ImapSessionUtils.getMailbox(session);
             for (int i = 0; i < idSet.length; i++) {
+                final FetchResponseBuilder builder = new 
FetchResponseBuilder(getLogger());
                 GeneralMessageSet messageSet = 
GeneralMessageSetImpl.range(idSet[i]
                         .getLowVal(), idSet[i].getHighVal(), useUids);
                 final Iterator it = mailbox.getMessages(messageSet, 
resultToFetch);
                 while (it.hasNext()) {
                     final MessageResult result = (MessageResult) it.next();
-                    String msgData = outputMessage(fetch, result, mailbox,
-                            useUids);
-                    final int msn = result.getMsn();
-                    // TODO: this is inefficient
-                    // TODO: stream output upon response
-                    LegacyFetchResponse response = new 
LegacyFetchResponse(msn, msgData);
+                    final FetchResponse response = builder.build(fetch, 
result, mailbox, useUids);
                     responder.respond(response);
                 }
             }
@@ -157,218 +153,264 @@
         return result;
     }
 
-    private String outputMessage(FetchData fetch, MessageResult result,
-            ImapMailboxSession mailbox, boolean useUids)
-            throws MailboxException, ProtocolException {
-        // Check if this fetch will cause the "SEEN" flag to be set on this
-        // message
-        // If so, update the flags, and ensure that a flags response is 
included
-        // in the response.
-        try {
-            boolean ensureFlagsResponse = false;
-            if (fetch.isSetSeen()
-                    && !result.getFlags().contains(Flags.Flag.SEEN)) {
-                mailbox.setFlags(new Flags(Flags.Flag.SEEN), true, false,
-                        GeneralMessageSetImpl.oneUid(result.getUid()), 
MessageResult.NOTHING);
-                result.getFlags().add(Flags.Flag.SEEN);
-                ensureFlagsResponse = true;
-            }
-
-            StringBuffer response = new StringBuffer();
+    private static final class FetchResponseBuilder {
+        private final Logger logger;
+        private int msn;
+        private Long uid;
+        private Flags flags;
+        private Date internalDate;
+        private Integer size;
+        private StringBuffer misc;
+        private StringBuffer elements;
+        
+        public FetchResponseBuilder(final Logger logger) {
+            super();
+            this.logger = logger;
+        }
+
+        public void reset(int msn) {
+            this.msn = msn;
+            uid = null;
+            flags = null;
+            internalDate = null;
+            size = null;    
+            misc = null;
+            elements = null;
+        }
+        
+        public void setUid(long uid) {
+            this.uid = new Long(uid);
+        }
+        
+        public void setFlags(Flags flags) {
+            this.flags = flags;
+        }
+        
+        public FetchResponse build() {
+            final FetchResponse result = new FetchResponse(msn, flags, uid, 
internalDate, 
+                    size, misc, elements);
+            return result;
+        }
+
+        public FetchResponse build(FetchData fetch, MessageResult result,
+                ImapMailboxSession mailbox, boolean useUids)
+                throws MailboxException, ProtocolException {
+            
+            setMsn(result);
+            
+            // Check if this fetch will cause the "SEEN" flag to be set on this
+            // message
+            // If so, update the flags, and ensure that a flags response is 
included
+            // in the response.
+            try {
+                boolean ensureFlagsResponse = false;
+                if (fetch.isSetSeen()
+                        && !result.getFlags().contains(Flags.Flag.SEEN)) {
+                    mailbox.setFlags(new Flags(Flags.Flag.SEEN), true, false,
+                            GeneralMessageSetImpl.oneUid(result.getUid()), 
MessageResult.NOTHING);
+                    result.getFlags().add(Flags.Flag.SEEN);
+                    ensureFlagsResponse = true;
+                }
 
-            // FLAGS response
-            if (fetch.isFlags() || ensureFlagsResponse) {
-                response.append(" FLAGS ");
-                response.append(MessageFlags.format(result.getFlags()));
-            }
+                
 
-            // INTERNALDATE response
-            if (fetch.isInternalDate()) {
-                response.append(" INTERNALDATE \"");
-                // TODO format properly
-                response.append(EncoderUtils.encodeDateTime(result
-                        .getInternalDate())); // not right format
-                response.append("\"");
+                // FLAGS response
+                if (fetch.isFlags() || ensureFlagsResponse) {
+                    setFlags(result.getFlags());
+                }
 
-            }
+                // INTERNALDATE response
+                if (fetch.isInternalDate()) {
+                    setInternalDate(result
+                            .getInternalDate());
+                }
 
-            // TODO: RFC822.HEADER
+                // TODO: RFC822.HEADER
 
-            // RFC822.SIZE response
-            if (fetch.isSize()) {
-                response.append(" RFC822.SIZE ");
-                response.append(result.getSize());
-            }
-
-            // Only create when needed
-            if (fetch.isEnvelope() || fetch.isBody() || 
fetch.isBodyStructure()) {
-                // TODO: replace SimpleMessageAttributes
-                final SimpleMessageAttributes attrs = new 
SimpleMessageAttributes(
-                        result.getMimeMessage(), getLogger());
-
-                // ENVELOPE response
-                if (fetch.isEnvelope()) {
-                    response.append(" ENVELOPE ");
-                    response.append(attrs.getEnvelope());
+                // RFC822.SIZE response
+                if (fetch.isSize()) {
+                    setSize(result.getSize());
                 }
 
-                // BODY response
-                if (fetch.isBody()) {
-                    response.append(" BODY ");
-                    response.append(attrs.getBodyStructure(false));
+                // Only create when needed
+                if (fetch.isEnvelope() || fetch.isBody() || 
fetch.isBodyStructure()) {
+                    misc = new StringBuffer();
+                    // TODO: replace SimpleMessageAttributes
+                    final SimpleMessageAttributes attrs = new 
SimpleMessageAttributes(
+                            result.getMimeMessage(), logger);
+
+                    // ENVELOPE response
+                    if (fetch.isEnvelope()) {
+                        misc.append(" ENVELOPE ");
+                        misc.append(attrs.getEnvelope());
+                    }
+
+                    // BODY response
+                    if (fetch.isBody()) {
+                        misc.append(" BODY ");
+                        misc.append(attrs.getBodyStructure(false));
+                    }
+
+                    // BODYSTRUCTURE response
+                    if (fetch.isBodyStructure()) {
+                        misc.append(" BODYSTRUCTURE ");
+                        misc.append(attrs.getBodyStructure(true));
+                    }
                 }
-
-                // BODYSTRUCTURE response
-                if (fetch.isBodyStructure()) {
-                    response.append(" BODYSTRUCTURE ");
-                    response.append(attrs.getBodyStructure(true));
+                // UID response
+                if (fetch.isUid()) {
+                    setUid(result.getUid());
                 }
-            }
-            // UID response
-            if (fetch.isUid()) {
-                response.append(" UID ");
-                response.append(result.getUid());
-            }
 
-            // BODY part responses.
-            Collection elements = fetch.getBodyElements();
-            for (Iterator iterator = elements.iterator(); iterator.hasNext();) 
{
-                BodyFetchElement fetchElement = (BodyFetchElement) iterator
-                        .next();
-                response.append(ImapConstants.SP);
-                response.append(fetchElement.getResponseName());
-                response.append(ImapConstants.SP);
-
-                // Various mechanisms for returning message body.
-                String sectionSpecifier = fetchElement.getParameters();
-
-                try {
-                    handleBodyFetch(result, sectionSpecifier, response);
-                } catch (MessagingException e) {
-                    throw new MailboxException(e.getMessage(), e);
+                // BODY part responses.
+                Collection elements = fetch.getBodyElements();
+                this.elements = new StringBuffer();
+                for (Iterator iterator = elements.iterator(); 
iterator.hasNext();) {
+                    BodyFetchElement fetchElement = (BodyFetchElement) iterator
+                            .next();
+                    this.elements.append(ImapConstants.SP);
+                    this.elements.append(fetchElement.getResponseName());
+                    this.elements.append(ImapConstants.SP);
+
+                    // Various mechanisms for returning message body.
+                    String sectionSpecifier = fetchElement.getParameters();
+
+                    try {
+                        handleBodyFetch(result, sectionSpecifier, 
this.elements);
+                    } catch (MessagingException e) {
+                        throw new MailboxException(e.getMessage(), e);
+                    }
                 }
+                return build();
+            } catch (MailboxManagerException mme) {
+                throw new MailboxException(mme);
+            } catch (MessagingException me) {
+                throw new MailboxException(me);
             }
+        }
+
+        private void setSize(int size) {
+            this.size = new Integer(size);
+        }
+
+        public void setInternalDate(Date internalDate) {
+            this.internalDate = internalDate;
+        }
+
+        private void setMsn(MessageResult result) {
+            final int msn = result.getMsn();
+            reset(msn);
+        }
+
+        private void handleBodyFetch(final MessageResult result,
+                String sectionSpecifier, StringBuffer response)
+                throws ProtocolException, MessagingException {
+            // TODO: section specifier should be fully parsed during parsing 
phase
+            if (sectionSpecifier.length() == 0) {
+                final MessageResult.Content fullMessage = 
result.getFullMessage();
+                addLiteralContent(fullMessage, response);
+            } else if (sectionSpecifier.equalsIgnoreCase("HEADER")) {
+                final Iterator headers = result.iterateHeaders();
+                List lines = MessageResultUtils.getAll(headers);
+                addHeaders(lines, response);
+            } else if (sectionSpecifier.startsWith("HEADER.FIELDS.NOT ")) {
+                String[] excludeNames = extractHeaderList(sectionSpecifier,
+                        "HEADER.FIELDS.NOT ".length());
+                final Iterator headers = result.iterateHeaders();
+                List lines = MessageResultUtils.getNotMatching(excludeNames, 
headers);
+                addHeaders(lines, response);
+            } else if (sectionSpecifier.startsWith("HEADER.FIELDS ")) {
+                String[] includeNames = extractHeaderList(sectionSpecifier,
+                        "HEADER.FIELDS ".length());
+                final Iterator headers = result.iterateHeaders();
+                List lines = MessageResultUtils.getMatching(includeNames, 
headers);
+                addHeaders(lines, response);
+            } else if (sectionSpecifier.equalsIgnoreCase("MIME")) {
+                // TODO implement
+                throw new ProtocolException("MIME not yet implemented.");
+
+            } else if (sectionSpecifier.equalsIgnoreCase("TEXT")) {
+                final MessageResult.Content messageBody = 
result.getMessageBody();
+                addLiteralContent(messageBody, response);
 
-            if (response.length() > 0) {
-                // Remove the leading " ".
-                return response.substring(1);
             } else {
-                return "";
+                // Should be a part specifier followed by a section specifier.
+                // See if there's a leading part specifier.
+                // If so, get the number, get the part, and call this 
recursively.
+                int dotPos = sectionSpecifier.indexOf('.');
+                if (dotPos == -1) {
+                    throw new ProtocolException("Malformed fetch attribute: "
+                            + sectionSpecifier);
+                }
+                int partNumber = Integer.parseInt(sectionSpecifier.substring(0,
+                        dotPos));
+                String partSectionSpecifier = sectionSpecifier
+                        .substring(dotPos + 1);
+
+                // TODO - get the MimePart of the mimeMessage, and call this 
method
+                // with the new partSectionSpecifier.
+                // MimeMessage part;
+                // handleBodyFetch( part, partSectionSpecifier, response );
+                throw new ProtocolException(
+                        "Mime parts not yet implemented for fetch.");
             }
-        } catch (MailboxManagerException mme) {
-            throw new MailboxException(mme);
-        } catch (MessagingException me) {
-            throw new MailboxException(me);
-        }
-    }
 
-    private void handleBodyFetch(final MessageResult result,
-            String sectionSpecifier, StringBuffer response)
-            throws ProtocolException, MessagingException {
-        // TODO: section specifier should be fully parsed during parsing phase
-        if (sectionSpecifier.length() == 0) {
-            final MessageResult.Content fullMessage = result.getFullMessage();
-            addLiteralContent(fullMessage, response);
-        } else if (sectionSpecifier.equalsIgnoreCase("HEADER")) {
-            final Iterator headers = result.iterateHeaders();
-            List lines = MessageResultUtils.getAll(headers);
-            addHeaders(lines, response);
-        } else if (sectionSpecifier.startsWith("HEADER.FIELDS.NOT ")) {
-            String[] excludeNames = extractHeaderList(sectionSpecifier,
-                    "HEADER.FIELDS.NOT ".length());
-            final Iterator headers = result.iterateHeaders();
-            List lines = MessageResultUtils.getNotMatching(excludeNames, 
headers);
-            addHeaders(lines, response);
-        } else if (sectionSpecifier.startsWith("HEADER.FIELDS ")) {
-            String[] includeNames = extractHeaderList(sectionSpecifier,
-                    "HEADER.FIELDS ".length());
-            final Iterator headers = result.iterateHeaders();
-            List lines = MessageResultUtils.getMatching(includeNames, headers);
-            addHeaders(lines, response);
-        } else if (sectionSpecifier.equalsIgnoreCase("MIME")) {
-            // TODO implement
-            throw new ProtocolException("MIME not yet implemented.");
-
-        } else if (sectionSpecifier.equalsIgnoreCase("TEXT")) {
-            final MessageResult.Content messageBody = result.getMessageBody();
-            addLiteralContent(messageBody, response);
-
-        } else {
-            // Should be a part specifier followed by a section specifier.
-            // See if there's a leading part specifier.
-            // If so, get the number, get the part, and call this recursively.
-            int dotPos = sectionSpecifier.indexOf('.');
-            if (dotPos == -1) {
-                throw new ProtocolException("Malformed fetch attribute: "
-                        + sectionSpecifier);
-            }
-            int partNumber = Integer.parseInt(sectionSpecifier.substring(0,
-                    dotPos));
-            String partSectionSpecifier = sectionSpecifier
-                    .substring(dotPos + 1);
-
-            // TODO - get the MimePart of the mimeMessage, and call this method
-            // with the new partSectionSpecifier.
-            // MimeMessage part;
-            // handleBodyFetch( part, partSectionSpecifier, response );
-            throw new ProtocolException(
-                    "Mime parts not yet implemented for fetch.");
         }
 
-    }
-
-    private void addLiteralContent(final MessageResult.Content content,
-            final StringBuffer response) throws MessagingException {
-        response.append('{');
-        final long length = content.size();
-        response.append(length); // TODO JD addLiteral: why was it
-                                    // bytes.length +1 here?
-        response.append('}');
-        response.append("\r\n");
-        content.writeTo(response);
-    }
-
-    // TODO should do this at parse time.
-    private String[] extractHeaderList(String headerList, int prefixLen) {
-        // Remove the trailing and leading ')('
-        String tmp = headerList.substring(prefixLen + 1,
-                headerList.length() - 1);
-        String[] headerNames = split(tmp, " ");
-        return headerNames;
-    }
+        private void addLiteralContent(final MessageResult.Content content,
+                final StringBuffer response) throws MessagingException {
+            response.append('{');
+            final long length = content.size();
+            response.append(length); // TODO JD addLiteral: why was it
+                                        // bytes.length +1 here?
+            response.append('}');
+            response.append("\r\n");
+            content.writeTo(response);
+        }
 
-    private String[] split(String value, String delimiter) {
-        ArrayList strings = new ArrayList();
-        int startPos = 0;
-        int delimPos;
-        while ((delimPos = value.indexOf(delimiter, startPos)) != -1) {
-            String sub = value.substring(startPos, delimPos);
+        // TODO should do this at parse time.
+        private String[] extractHeaderList(String headerList, int prefixLen) {
+            // Remove the trailing and leading ')('
+            String tmp = headerList.substring(prefixLen + 1,
+                    headerList.length() - 1);
+            String[] headerNames = split(tmp, " ");
+            return headerNames;
+        }
+
+        private String[] split(String value, String delimiter) {
+            ArrayList strings = new ArrayList();
+            int startPos = 0;
+            int delimPos;
+            while ((delimPos = value.indexOf(delimiter, startPos)) != -1) {
+                String sub = value.substring(startPos, delimPos);
+                strings.add(sub);
+                startPos = delimPos + 1;
+            }
+            String sub = value.substring(startPos);
             strings.add(sub);
-            startPos = delimPos + 1;
+
+            return (String[]) strings.toArray(new String[0]);
         }
-        String sub = value.substring(startPos);
-        strings.add(sub);
 
-        return (String[]) strings.toArray(new String[0]);
-    }
+        private void addHeaders(List headerLines, StringBuffer response)
+                throws MessagingException {
+            int count = 0;
+            for (final Iterator it = headerLines.iterator(); it.hasNext();) {
+                MessageResult.Header header = (MessageResult.Header) it.next();
+                count += header.size() + 2;
+            }
+            response.append('{');
+            response.append(count + 2);
+            response.append('}');
+            response.append("\r\n");
 
-    private void addHeaders(List headerLines, StringBuffer response)
-            throws MessagingException {
-        int count = 0;
-        for (final Iterator it = headerLines.iterator(); it.hasNext();) {
-            MessageResult.Header header = (MessageResult.Header) it.next();
-            count += header.size() + 2;
-        }
-        response.append('{');
-        response.append(count + 2);
-        response.append('}');
-        response.append("\r\n");
-
-        for (final Iterator it = headerLines.iterator(); it.hasNext();) {
-            MessageResult.Header header = (MessageResult.Header) it.next();
-            header.writeTo(response);
+            for (final Iterator it = headerLines.iterator(); it.hasNext();) {
+                MessageResult.Header header = (MessageResult.Header) it.next();
+                header.writeTo(response);
+                response.append("\r\n");
+            }
             response.append("\r\n");
         }
-        response.append("\r\n");
+        
     }
 }

Modified: 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/StoreProcessor.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/StoreProcessor.java?rev=598007&r1=598006&r2=598007&view=diff
==============================================================================
--- 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/StoreProcessor.java
 (original)
+++ 
james/server/trunk/imap-mailbox-processor-function/src/main/java/org/apache/james/imapserver/processor/imap4rev1/StoreProcessor.java
 Sun Nov 25 08:48:31 2007
@@ -96,7 +96,7 @@
                         } else {
                             resultUid = null;
                         }
-                        final FetchResponse response = new FetchResponse(msn, 
resultFlags, resultUid);
+                        final FetchResponse response = new FetchResponse(msn, 
resultFlags, resultUid, null, null, null, null);
                         responder.respond(response);
                     }
                 }



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

Reply via email to