private List<ImapFolder> getFolders(List<FolderListItem> folders) { List<ImapFolder> imapFolders = new ArrayList<>(folders.size()); for (FolderListItem folder : folders) { ImapFolder imapFolder = getFolder(folder.getName()); imapFolder.setType(folder.getType()); imapFolders.add(imapFolder); } return imapFolders; }
private String getPrefixedName() throws MessagingException { String prefixedName = ""; if (!INBOX.equalsIgnoreCase(name)) { ImapConnection connection; synchronized (this) { if (this.connection == null) { connection = store.getConnection(); } else { connection = this.connection; } } try { connection.open(); } catch (IOException ioe) { throw new MessagingException("Unable to get IMAP prefix", ioe); } finally { if (this.connection == null) { store.releaseConnection(connection); } } prefixedName = store.getCombinedPrefix(); } prefixedName += name; return prefixedName; }
private long getStartUid(long oldUidNext, long newUidNext) { long startUid = oldUidNext; int displayCount = store.getStoreConfig().getDisplayCount(); if (startUid < newUidNext - displayCount) { startUid = newUidNext - displayCount; } if (startUid < 1) { startUid = 1; } return startUid; }
@Override public List<ImapFolder> getPersonalNamespaces() throws MessagingException { ImapConnection connection = getConnection(); try { List<FolderListItem> folders = listFolders(connection, false); if (!mStoreConfig.isSubscribedFoldersOnly()) { return getFolders(folders); } List<FolderListItem> subscribedFolders = listFolders(connection, true); List<FolderListItem> filteredFolders = limitToSubscribedFolders(folders, subscribedFolders); return getFolders(filteredFolders); } catch (IOException | MessagingException ioe) { connection.close(); throw new MessagingException("Unable to get folder list.", ioe); } finally { releaseConnection(connection); } }
ImapConnection getConnection() throws MessagingException { ImapConnection connection; while ((connection = pollConnection()) != null) { try { connection.executeSimpleCommand(Commands.NOOP); break; } catch (IOException ioe) { connection.close(); } } if (connection == null) { connection = createImapConnection(); } return connection; }
@Test public void create_withClosedFolder_shouldOpenConnectionAndIssueCreateCommand() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); imapFolder.create(); verify(imapConnection).executeSimpleCommand("CREATE \"Folder\""); }
@Override public boolean create() throws MessagingException { /* * This method needs to operate in the unselected mode as well as the selected mode * so we must get the connection ourselves if it's not there. We are specifically * not calling checkOpen() since we don't care if the folder is open. */ ImapConnection connection; synchronized (this) { if (this.connection == null) { connection = store.getConnection(); } else { connection = this.connection; } } try { String encodedFolderName = folderNameCodec.encode(getPrefixedName()); String escapedFolderName = ImapUtility.encodeString(encodedFolderName); connection.executeSimpleCommand(String.format("CREATE %s", escapedFolderName)); return true; } catch (NegativeImapResponseException e) { return false; } catch (IOException ioe) { throw ioExceptionHandler(this.connection, ioe); } finally { if (this.connection == null) { store.releaseConnection(connection); } } }
@Before public void setUp() throws Exception { BinaryTempFileBody.setTempDirectory(RuntimeEnvironment.application.getCacheDir()); imapStore = mock(ImapStore.class); storeConfig = mock(StoreConfig.class); when(storeConfig.getInboxFolder()).thenReturn("INBOX"); when(imapStore.getCombinedPrefix()).thenReturn(""); when(imapStore.getStoreConfig()).thenReturn(storeConfig); imapConnection = mock(ImapConnection.class); }
@Test public void copyMessages_withClosedFolder_shouldThrow() throws Exception { ImapFolder sourceFolder = createFolder("Source"); ImapFolder destinationFolder = createFolder("Destination"); when(imapStore.getConnection()).thenReturn(imapConnection); when(imapStore.getCombinedPrefix()).thenReturn(""); List<ImapMessage> messages = singletonList(mock(ImapMessage.class)); try { sourceFolder.copyMessages(messages, destinationFolder); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Folder Source is not open.", e.getMessage()); } }
String encodedListPrefix = ImapUtility.encodeString(getCombinedPrefix() + "*"); List<ImapResponse> responses = connection.executeSimpleCommand(String.format(commandFormat, encodedListPrefix)); folder = removePrefixFromFolderName(folder); if (folder == null) { continue;
@Nullable private String removePrefixFromFolderName(String folderName) { String prefix = getCombinedPrefix(); int prefixLength = prefix.length(); if (prefixLength == 0) { return folderName; } if (!folderName.startsWith(prefix)) { // Folder name doesn't start with our configured prefix. But right now when building commands we prefix all // folders except the INBOX with the prefix. So we won't be able to use this folder. return null; } return folderName.substring(prefixLength); }
@Override public void checkIncomingServerSettings() throws MessagingException { imapStore.checkSettings(); }
public ImapFolder(ImapStore store, String name) { this(store, name, store.getFolderNameCodec()); }
@Override public void checkSettings() throws MessagingException { try { ImapConnection connection = createImapConnection(); connection.open(); connection.close(); } catch (IOException ioe) { throw new MessagingException("Unable to connect", ioe); } }
@Test public void create_withNegativeImapResponse_shouldReturnFalse() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); doThrow(NegativeImapResponseException.class).when(imapConnection).executeSimpleCommand("CREATE \"Folder\""); boolean success = imapFolder.create(); assertFalse(success); }
synchronized (this) { if (this.connection == null) { connection = store.getConnection(); } else { connection = this.connection; } finally { if (this.connection == null) { store.releaseConnection(connection);
@Override String getCombinedPrefix() { return testCombinedPrefix != null ? testCombinedPrefix : super.getCombinedPrefix(); }
private void configureRemoteStoreWithFolder() { when(remoteStore.getFolder(FOLDER_NAME)).thenReturn(remoteFolder); when(remoteFolder.getServerId()).thenReturn(FOLDER_NAME); }
@Test public void exists_withClosedFolder_shouldOpenConnectionAndIssueStatusCommand() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); imapFolder.exists(); verify(imapConnection).executeSimpleCommand("STATUS \"Folder\" (UIDVALIDITY)"); }
@Override public int getRefreshInterval() { return (store.getStoreConfig().getIdleRefreshMinutes() * 60 * 1000); }