|
|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
* #%L
|
|
|
|
|
* Alfresco Repository
|
|
|
|
|
* %%
|
|
|
|
|
* Copyright (C) 2005 - 2023 Alfresco Software Limited
|
|
|
|
|
* Copyright (C) 2005 - 2025 Alfresco Software Limited
|
|
|
|
|
* %%
|
|
|
|
|
* This file is part of the Alfresco software.
|
|
|
|
|
* If the software was purchased under a paid Alfresco license, the terms of
|
|
|
|
@@ -25,6 +25,8 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.alfresco.repo.imap;
|
|
|
|
|
|
|
|
|
|
import static org.alfresco.model.ContentModel.PROP_MODIFIED;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
@@ -37,7 +39,6 @@ import java.util.List;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import jakarta.mail.Address;
|
|
|
|
|
import jakarta.mail.Folder;
|
|
|
|
|
import jakarta.mail.Message;
|
|
|
|
@@ -51,6 +52,23 @@ import jakarta.mail.internet.MimeUtility;
|
|
|
|
|
import jakarta.transaction.UserTransaction;
|
|
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.eclipse.angus.mail.iap.ProtocolException;
|
|
|
|
|
import org.eclipse.angus.mail.iap.Response;
|
|
|
|
|
import org.eclipse.angus.mail.imap.IMAPFolder;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.BODY;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.FetchResponse;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.IMAPProtocol;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.IMAPResponse;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.RFC822DATA;
|
|
|
|
|
import org.eclipse.angus.mail.imap.protocol.UID;
|
|
|
|
|
import org.eclipse.angus.mail.util.ASCIIUtility;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.experimental.categories.Category;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
|
|
|
|
|
|
import org.alfresco.model.ContentModel;
|
|
|
|
|
import org.alfresco.model.ImapModel;
|
|
|
|
@@ -83,26 +101,6 @@ import org.alfresco.util.PropertyMap;
|
|
|
|
|
import org.alfresco.util.config.RepositoryFolderConfigBean;
|
|
|
|
|
import org.alfresco.util.testing.category.LuceneTests;
|
|
|
|
|
import org.alfresco.util.testing.category.RedundantTests;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.experimental.categories.Category;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
|
|
|
|
|
|
import com.sun.mail.iap.ProtocolException;
|
|
|
|
|
import com.sun.mail.iap.Response;
|
|
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
|
import com.sun.mail.imap.protocol.BODY;
|
|
|
|
|
import com.sun.mail.imap.protocol.FetchResponse;
|
|
|
|
|
import com.sun.mail.imap.protocol.IMAPProtocol;
|
|
|
|
|
import com.sun.mail.imap.protocol.IMAPResponse;
|
|
|
|
|
import com.sun.mail.imap.protocol.RFC822DATA;
|
|
|
|
|
import com.sun.mail.imap.protocol.UID;
|
|
|
|
|
import com.sun.mail.util.ASCIIUtility;
|
|
|
|
|
|
|
|
|
|
import static org.alfresco.model.ContentModel.PROP_MODIFIED;
|
|
|
|
|
|
|
|
|
|
@Category({OwnJVMTestsCategory.class, LuceneTests.class})
|
|
|
|
|
public class ImapMessageTest extends TestCase
|
|
|
|
@@ -159,7 +157,6 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
namespaceService = serviceRegistry.getNamespaceService();
|
|
|
|
|
fileFolderService = serviceRegistry.getFileFolderService();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start the transaction
|
|
|
|
|
UserTransaction txn = transactionService.getUserTransaction();
|
|
|
|
|
txn.begin();
|
|
|
|
@@ -200,7 +197,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
ImapServiceImpl imapServiceImpl = (ImapServiceImpl) imapCtx.getBean("imapService");
|
|
|
|
|
imapServer = (AlfrescoImapServer) imapCtx.getBean("imapServer");
|
|
|
|
|
|
|
|
|
|
if(!imapServer.isImapServerEnabled())
|
|
|
|
|
if (!imapServer.isImapServerEnabled())
|
|
|
|
|
{
|
|
|
|
|
imapServer.setImapServerEnabled(true);
|
|
|
|
|
imapServer.setHost(HOST);
|
|
|
|
@@ -220,7 +217,6 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
imapHome.setFolderPath(NamespaceService.CONTENT_MODEL_PREFIX + ":" + IMAP_FOLDER_NAME);
|
|
|
|
|
imapServiceImpl.setImapHome(imapHome);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Starting IMAP
|
|
|
|
|
imapServiceImpl.startupInTxn(true);
|
|
|
|
|
|
|
|
|
@@ -228,10 +224,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
namespaceService, false);
|
|
|
|
|
testImapFolderNodeRef = nodeRefs.get(0);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Importing test folders: Test folder contains: "___-___folder_a" "___-___folder_a" contains: "___-___folder_a_a", "___-___file_a", "Message_485.eml" (this is IMAP
|
|
|
|
|
* Message) "___-___folder_a_a" contains: "____-____file_a_a"
|
|
|
|
|
*/
|
|
|
|
|
/* Importing test folders: Test folder contains: "___-___folder_a" "___-___folder_a" contains: "___-___folder_a_a", "___-___file_a", "Message_485.eml" (this is IMAP Message) "___-___folder_a_a" contains: "____-____file_a_a" */
|
|
|
|
|
importInternal("imap/imapservice_test_folder_a.acp", testImapFolderNodeRef);
|
|
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
@@ -243,7 +236,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
// Get the store
|
|
|
|
|
this.store = session.getStore(PROTOCOL);
|
|
|
|
|
//this.store.connect(HOST, PORT, anotherUserName, anotherUserName);
|
|
|
|
|
// this.store.connect(HOST, PORT, anotherUserName, anotherUserName);
|
|
|
|
|
this.store.connect(imapServer.getHost(), imapServer.getPort(), anotherUserName, anotherUserName);
|
|
|
|
|
|
|
|
|
|
// Get folder
|
|
|
|
@@ -349,8 +342,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
fail("Should raise an IOException");
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void dontTestMessageCache() throws Exception
|
|
|
|
@@ -411,8 +403,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
Locale.setDefault(Locale.FRENCH);
|
|
|
|
|
String dateStr = "12-Jul-2020";
|
|
|
|
|
final IMAPFolder.ProtocolCommand uid_search_since = new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
final IMAPFolder.ProtocolCommand uid_search_since = new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object doCommand(IMAPProtocol protocol)
|
|
|
|
|
{
|
|
|
|
@@ -445,8 +436,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
Locale.setDefault(Locale.FRENCH);
|
|
|
|
|
String dateStr = "12-juil.-2020";
|
|
|
|
|
final IMAPFolder.ProtocolCommand uid_search_since = new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
final IMAPFolder.ProtocolCommand uid_search_since = new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object doCommand(IMAPProtocol protocol)
|
|
|
|
|
{
|
|
|
|
@@ -512,7 +502,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
// InternetAddress.toString(new Address[] {address}) - is used in the RFC822MetadataExtracter
|
|
|
|
|
// So, compare with that
|
|
|
|
|
assertFalse("Non ASCII characters in the address should be encoded", decodedAddress.equals(InternetAddress.toString(new Address[] {address})));
|
|
|
|
|
assertFalse("Non ASCII characters in the address should be encoded", decodedAddress.equals(InternetAddress.toString(new Address[]{address})));
|
|
|
|
|
|
|
|
|
|
MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
|
|
|
|
|
|
|
|
|
@@ -540,7 +530,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
// triggers a metadata extract to take place in a post commit method. Previously this would have been a
|
|
|
|
|
// synchronous process. This is no longer true as it may now take place in a T-Engine. So, we need to wait
|
|
|
|
|
// for the extract to take place. There does not
|
|
|
|
|
long end = System.currentTimeMillis()+10000;
|
|
|
|
|
long end = System.currentTimeMillis() + 10000;
|
|
|
|
|
while (System.currentTimeMillis() <= end && origModified.equals(getModified(nodeRef)))
|
|
|
|
|
{
|
|
|
|
|
Thread.currentThread().sleep(1000);
|
|
|
|
@@ -607,8 +597,6 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
lfolder.appendMessages(messages);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The search is not implemented.
|
|
|
|
|
// SearchTerm term = new HeaderTerm("X-Alfresco-Unique", "test8bit");
|
|
|
|
|
// messages = folder.search(term);
|
|
|
|
@@ -619,14 +607,13 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
QueryParameterDefinition[] params = new QueryParameterDefinition[1];
|
|
|
|
|
params[0] = new QueryParameterDefImpl(
|
|
|
|
|
ContentModel.PROP_TITLE,
|
|
|
|
|
serviceRegistry.getDictionaryService().getDataType(DataTypeDefinition.TEXT),
|
|
|
|
|
true,
|
|
|
|
|
subject);
|
|
|
|
|
ContentModel.PROP_TITLE,
|
|
|
|
|
serviceRegistry.getDictionaryService().getDataType(DataTypeDefinition.TEXT),
|
|
|
|
|
true,
|
|
|
|
|
subject);
|
|
|
|
|
|
|
|
|
|
List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, messageXPath, params, namespaceService, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// does the message exist
|
|
|
|
|
assertEquals(1, nodeRefs.size());
|
|
|
|
|
|
|
|
|
@@ -646,8 +633,10 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (messageFileInputStream1 != null) messageFileInputStream1.close();
|
|
|
|
|
if (messageFileInputStream2 != null) messageFileInputStream2.close();
|
|
|
|
|
if (messageFileInputStream1 != null)
|
|
|
|
|
messageFileInputStream1.close();
|
|
|
|
|
if (messageFileInputStream2 != null)
|
|
|
|
|
messageFileInputStream2.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// close connection
|
|
|
|
@@ -656,11 +645,9 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static RFC822DATA getRFC822Message(final IMAPFolder folder, final long uid) throws MessagingException
|
|
|
|
|
{
|
|
|
|
|
return (RFC822DATA) folder.doCommand(new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
return (RFC822DATA) folder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
public Object doCommand(IMAPProtocol p) throws ProtocolException
|
|
|
|
|
{
|
|
|
|
|
Response[] r = p.command("UID FETCH " + uid + " (RFC822)", null);
|
|
|
|
@@ -681,17 +668,20 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Returns BODY object containing desired message fragment
|
|
|
|
|
*
|
|
|
|
|
* @param folder Folder containing the message
|
|
|
|
|
* @param uid Message UID
|
|
|
|
|
* @param from starting byte
|
|
|
|
|
* @param count bytes to read
|
|
|
|
|
* @param folder
|
|
|
|
|
* Folder containing the message
|
|
|
|
|
* @param uid
|
|
|
|
|
* Message UID
|
|
|
|
|
* @param from
|
|
|
|
|
* starting byte
|
|
|
|
|
* @param count
|
|
|
|
|
* bytes to read
|
|
|
|
|
* @return BODY containing desired message fragment
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
private static BODY getMessageBodyPart(IMAPFolder folder, final Long uid, final Integer from, final Integer count) throws MessagingException
|
|
|
|
|
{
|
|
|
|
|
return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
public Object doCommand(IMAPProtocol p) throws ProtocolException
|
|
|
|
|
{
|
|
|
|
|
Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[]<" + from + "." + count + ">)", null);
|
|
|
|
@@ -705,7 +695,7 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FetchResponse fetchResponse = (FetchResponse) r[0];
|
|
|
|
|
BODY body = (BODY) fetchResponse.getItem(com.sun.mail.imap.protocol.BODY.class);
|
|
|
|
|
BODY body = (BODY) fetchResponse.getItem(BODY.class);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@@ -715,7 +705,8 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Finds node by its path
|
|
|
|
|
*
|
|
|
|
|
* @param path String
|
|
|
|
|
* @param path
|
|
|
|
|
* String
|
|
|
|
|
* @return NodeRef
|
|
|
|
|
*/
|
|
|
|
|
private NodeRef findNode(String path)
|
|
|
|
@@ -727,15 +718,16 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Returns the UID of the first message in folder
|
|
|
|
|
*
|
|
|
|
|
* @param folder Folder containing the message
|
|
|
|
|
* @param msn message sequence number
|
|
|
|
|
* @param folder
|
|
|
|
|
* Folder containing the message
|
|
|
|
|
* @param msn
|
|
|
|
|
* message sequence number
|
|
|
|
|
* @return UID of the first message
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
private static Long getMessageUid(IMAPFolder folder, final int msn) throws MessagingException
|
|
|
|
|
{
|
|
|
|
|
return (Long) folder.doCommand(new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
return (Long) folder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
public Object doCommand(IMAPProtocol p) throws ProtocolException
|
|
|
|
|
{
|
|
|
|
|
String command = "FETCH " + msn + " (UID)";
|
|
|
|
@@ -749,38 +741,37 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
throw new ProtocolException("Unable to retrieve message UID");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i = 0 ; i < r.length; i++)
|
|
|
|
|
for (int i = 0; i < r.length; i++)
|
|
|
|
|
{
|
|
|
|
|
if(r[i] instanceof FetchResponse)
|
|
|
|
|
if (r[i] instanceof FetchResponse)
|
|
|
|
|
{
|
|
|
|
|
FetchResponse fetchResponse = (FetchResponse) r[0];
|
|
|
|
|
UID uid = (UID) fetchResponse.getItem(UID.class);
|
|
|
|
|
logger.debug("SECNUM=" + uid.seqnum + ", UID="+uid.uid);
|
|
|
|
|
logger.debug("SECNUM=" + uid.seqnum + ", UID=" + uid.uid);
|
|
|
|
|
return uid.uid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Uh-oh - this is where we would intermittently fall over with a class cast exception.
|
|
|
|
|
* The following code probes why we don't have a FetchResponse
|
|
|
|
|
*/
|
|
|
|
|
* Uh-oh - this is where we would intermittently fall over with a class cast exception. The following code probes why we don't have a FetchResponse
|
|
|
|
|
*/
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
sb.append("command="+command);
|
|
|
|
|
sb.append("command=" + command);
|
|
|
|
|
sb.append('\n');
|
|
|
|
|
sb.append("resp length=" + r.length);
|
|
|
|
|
sb.append('\n');
|
|
|
|
|
for(int i = 0 ; i < r.length; i++)
|
|
|
|
|
for (int i = 0; i < r.length; i++)
|
|
|
|
|
{
|
|
|
|
|
logger.error(r[i]);
|
|
|
|
|
sb.append("class=" + r[i].getClass().getName());
|
|
|
|
|
IMAPResponse unexpected = (IMAPResponse)r[i];
|
|
|
|
|
IMAPResponse unexpected = (IMAPResponse) r[i];
|
|
|
|
|
sb.append("key=" + unexpected.getKey());
|
|
|
|
|
sb.append("number=" + unexpected.getNumber());
|
|
|
|
|
sb.append("rest=" + unexpected.getRest());
|
|
|
|
|
|
|
|
|
|
sb.append("r[" + i + "]=" + r[i] + '\n');
|
|
|
|
|
}
|
|
|
|
|
throw new ProtocolException("getMessageUid: "+ sb.toString());
|
|
|
|
|
throw new ProtocolException("getMessageUid: " + sb.toString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@@ -788,8 +779,10 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Returns size of the message
|
|
|
|
|
*
|
|
|
|
|
* @param folder Folder containing the message
|
|
|
|
|
* @param uid Message UID
|
|
|
|
|
* @param folder
|
|
|
|
|
* Folder containing the message
|
|
|
|
|
* @param uid
|
|
|
|
|
* Message UID
|
|
|
|
|
* @return Returns size of the message
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
@@ -801,15 +794,16 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Returns a full message body
|
|
|
|
|
*
|
|
|
|
|
* @param folder Folder containing the message
|
|
|
|
|
* @param uid Message UID
|
|
|
|
|
* @param folder
|
|
|
|
|
* Folder containing the message
|
|
|
|
|
* @param uid
|
|
|
|
|
* Message UID
|
|
|
|
|
* @return Returns size of the message
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
private static BODY getMessageBody(IMAPFolder folder, final Long uid) throws MessagingException
|
|
|
|
|
{
|
|
|
|
|
return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
|
|
|
|
|
{
|
|
|
|
|
return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
|
public Object doCommand(IMAPProtocol p) throws ProtocolException
|
|
|
|
|
{
|
|
|
|
|
Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[])", null);
|
|
|
|
@@ -831,7 +825,8 @@ public class ImapMessageTest extends TestCase
|
|
|
|
|
/**
|
|
|
|
|
* Simple util for logging response
|
|
|
|
|
*
|
|
|
|
|
* @param r response
|
|
|
|
|
* @param r
|
|
|
|
|
* response
|
|
|
|
|
*/
|
|
|
|
|
private static void logResponse(Response[] r)
|
|
|
|
|
{
|
|
|
|
|