(geronimo-mail) branch main updated: GERONIMO-6895 - Folder.fetch() fails on RFC-strict servers - header names emitted outside the BODY.PEEK[HEADER.FIELDS] section brackets

[email protected] Sat, 18 Jul 2026 18:32:44 +0000
Newsgroups gmane.comp.java.geronimo.cvs
Message-ID <178439956489.2920806.7472805485716849194@gitbox3-he-fi.apache.org>
This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/geronimo-mail.git


The following commit(s) were added to refs/heads/main by this push:
     new dfe8613  GERONIMO-6895 - Folder.fetch() fails on RFC-strict servers - header names emitted outside the BODY.PEEK[HEADER.FIELDS] section brackets
dfe8613 is described below

commit dfe861307dc03c0aec10c5535ee22bb74bda6587
Author: Richard Zowalla <[email protected]>
AuthorDate: Sat Jul 18 20:32:22 2026 +0200

    GERONIMO-6895 - Folder.fetch() fails on RFC-strict servers - header names emitted outside the BODY.PEEK[HEADER.FIELDS] section brackets
---
 .../mail/store/imap/connection/IMAPCommand.java    |  13 ++-
 .../mail/store/imap/IMAPFetchHeaderFieldsTest.java | 105 +++++++++++++++++++++
 geronimo-mail_2.1_tck/src/tck/geronimo.jtx         |   3 -
 3 files changed, 114 insertions(+), 7 deletions(-)

diff --git a/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/store/imap/connection/IMAPCommand.java b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/store/imap/connection/IMAPCommand.java
index 00a0e4a..5848aaa 100644
--- a/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/store/imap/connection/IMAPCommand.java
+++ b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/store/imap/connection/IMAPCommand.java
@@ -1407,12 +1407,17 @@ public class IMAPCommand {
             // have an actual list to retrieve?  need to craft this as a sublist
             // of identified fields.
             if (headers.length > 0) {
-                appendAtom("BODY.PEEK[HEADER.FIELDS]");
-                startList();
+                // per RFC 3501, the list of header fields belongs inside the
+                // section brackets:  BODY.PEEK[HEADER.FIELDS (h1 h2 ...)]
+                StringBuilder buff = new StringBuilder("BODY.PEEK[HEADER.FIELDS (");
                 for (int i = 0; i < headers.length; i++) {
-                    appendAtom(headers[i]);
+                    if (i > 0) {
+                        buff.append(' ');
+                    }
+                    buff.append(headers[i]);
                 }
-                endList();
+                buff.append(")]");
+                appendAtom(buff.toString());
             }
         }
         // end the list.
diff --git a/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/store/imap/IMAPFetchHeaderFieldsTest.java b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/store/imap/IMAPFetchHeaderFieldsTest.java
new file mode 100644
index 0000000..f04ef01
--- /dev/null
+++ b/geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/test/java/org/apache/geronimo/mail/store/imap/IMAPFetchHeaderFieldsTest.java
@@ -0,0 +1,105 @@
+/**
+ *  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.geronimo.mail.store.imap;
+
+import java.util.Date;
+import java.util.Properties;
+
+import jakarta.mail.FetchProfile;
+import jakarta.mail.Folder;
+import jakarta.mail.Message;
+import jakarta.mail.Session;
+import jakarta.mail.Store;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeMessage;
+
+import org.apache.geronimo.mail.testserver.AbstractProtocolTest;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Regression test for the FETCH command syntax generated for a FetchProfile
+ * that names explicit headers.
+ *
+ * IMAPCommand.appendFetchProfile used to emit
+ * "BODY.PEEK[HEADER.FIELDS] (Subject From)" - but per RFC 3501 the header
+ * name list belongs INSIDE the section brackets:
+ * "BODY.PEEK[HEADER.FIELDS (Subject From)]".  RFC-strict servers such as
+ * Apache James answer BAD, which surfaced as an InvalidCommandException
+ * from Folder.fetch().
+ */
+public class IMAPFetchHeaderFieldsTest extends AbstractProtocolTest {
+
+    private Store connect() throws Exception {
+        final Properties props = new Properties();
+        props.setProperty("mail.imap.port", String.valueOf(imapConf.getListenerPort()));
+        props.setProperty("mail.debug", "true");
+        final Session session = Session.getInstance(props);
+        final Store store = session.getStore("imap");
+        store.connect("127.0.0.1", "serveruser", "serverpass");
+        return store;
+    }
+
+    @Test
+    @Timeout(60)
+    public void testFetchWithExplicitHeaderNames() throws Exception {
+        start();
+
+        final MimeMessage msg = new MimeMessage(Session.getInstance(new Properties()));
+        msg.setFrom(new InternetAddress("serveruser@localhost"));
+        msg.setRecipient(Message.RecipientType.TO, new InternetAddress("serveruser@localhost"));
+        msg.setSubject("fetch profile test");
+        msg.setSentDate(new Date());
+        msg.setText("fetch profile test body");
+
+        final Store store = connect();
+        try {
+            final Folder inbox = store.getFolder("INBOX");
+            inbox.open(Folder.READ_WRITE);
+            inbox.appendMessages(new Message[] { msg });
+            assertEquals(1, inbox.getMessageCount());
+
+            final Message[] messages = inbox.getMessages();
+
+            // a profile with explicitly named headers plus regular items -
+            // this used to make the server answer BAD and fetch() throw.
+            final FetchProfile profile = new FetchProfile();
+            profile.add(FetchProfile.Item.ENVELOPE);
+            profile.add(FetchProfile.Item.FLAGS);
+            profile.add("Subject");
+            profile.add("From");
+            inbox.fetch(messages, profile);
+
+            // the prefetched headers must be usable afterwards
+            final Message fetched = messages[0];
+            assertEquals("fetch profile test", fetched.getSubject());
+            final String[] from = fetched.getHeader("From");
+            assertNotNull(from);
+            assertTrue(from.length > 0);
+            assertEquals("serveruser@localhost", from[0]);
+
+            inbox.close(false);
+        } finally {
+            store.close();
+        }
+    }
+
+}
diff --git a/geronimo-mail_2.1_tck/src/tck/geronimo.jtx b/geronimo-mail_2.1_tck/src/tck/geronimo.jtx
index deef334..844f991 100644
--- a/geronimo-mail_2.1_tck/src/tck/geronimo.jtx
+++ b/geronimo-mail_2.1_tck/src/tck/geronimo.jtx
@@ -43,11 +43,8 @@ javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#appendMessages_Test
 javasoft/sqe/tests/jakarta/mail/event/FolderEvent/testlist.html#addMsgChangeList_Test
 javasoft/sqe/tests/jakarta/mail/exception/testlist.html#folderNotFoundExp_Test
 javasoft/sqe/tests/jakarta/mail/exception/testlist.html#msgRemoveExp_Test
-javasoft/sqe/tests/jakarta/mail/FetchProfile/testlist.html#add_Test
-javasoft/sqe/tests/jakarta/mail/FetchProfile/testlist.html#contains_Test
 javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#create_Test
 javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#delete_Test
-javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#fetch_Test
 javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#getPermanentFlags_Test
 javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#getType_Test
 javasoft/sqe/tests/jakarta/mail/Folder/testlist.html#list_Test