private FetchProfile createFetchProfile(Item... items) { FetchProfile fetchProfile = new FetchProfile(); Collections.addAll(fetchProfile, items); return fetchProfile; }
@Nullable private LocalMessage loadLocalMessageByMessageId(long messageId) throws MessagingException { Map<String, List<String>> foldersAndUids = getFoldersAndUids(Collections.singletonList(messageId), false); if (foldersAndUids.isEmpty()) { return null; } Map.Entry<String,List<String>> entry = foldersAndUids.entrySet().iterator().next(); String folderServerId = entry.getKey(); String uid = entry.getValue().get(0); LocalFolder folder = getFolder(folderServerId); LocalMessage localMessage = folder.getMessage(uid); FetchProfile fp = new FetchProfile(); fp.add(Item.BODY); folder.fetch(Collections.singletonList(localMessage), fp, null); return localMessage; }
private void downloadSaneBody(Folder remoteFolder, BackendFolder backendFolder, Message message) throws MessagingException { /* * The provider was unable to get the structure of the message, so * we'll download a reasonable portion of the messge and mark it as * incomplete so the entire thing can be downloaded later if the user * wishes to download it. */ FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY_SANE); /* * TODO a good optimization here would be to make sure that all Stores set * the proper size after this fetch and compare the before and after size. If * they equal we can mark this SYNCHRONIZED instead of PARTIALLY_SYNCHRONIZED */ remoteFolder.fetch(Collections.singletonList(message), fp, null); // Store the updated message locally backendFolder.savePartialMessage(message); }
public LocalMessage loadMessageMetadata(Account account, String folderServerId, String uid) throws MessagingException { LocalStore localStore = localStoreProvider.getInstance(account); LocalFolder localFolder = localStore.getFolder(folderServerId); localFolder.open(Folder.OPEN_MODE_RW); LocalMessage message = localFolder.getMessage(uid); if (message == null || message.getDatabaseId() == 0) { throw new IllegalArgumentException("Message not found: folder=" + folderServerId + ", uid=" + uid); } FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); localFolder.fetch(Collections.singletonList(message), fp, null); localFolder.close(); return message; }
@Test public void folder_can_fetch_more_than_10_envelopes() throws MessagingException { when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List<WebDavMessage> messages = new ArrayList<>(); for (int i = 0; i < 15; i++) { WebDavMessage mockMessage = createWebDavMessage(i); messages.add(mockMessage); } FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); folder.fetch(messages, profile, listener); }
@Test public void folder_can_fetch_less_than_10_envelopes() throws MessagingException { when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List<WebDavMessage> messages = new ArrayList<>(); for (int i = 0; i < 5; i++) { WebDavMessage mockMessage = createWebDavMessage(i); messages.add(mockMessage); } FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); folder.fetch(messages, profile, listener); }
@Test public void folder_can_fetch_more_than_20_flags() throws MessagingException { when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List<WebDavMessage> messages = new ArrayList<>(); for (int i = 0; i < 25; i++) { WebDavMessage mockMessage = createWebDavMessage(i); messages.add(mockMessage); } FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.FLAGS); folder.fetch(messages, profile, listener); }
@Test public void folder_can_fetch_less_than_20_flags() throws MessagingException { when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List<WebDavMessage> messages = new ArrayList<>(); for (int i = 0; i < 5; i++) { WebDavMessage mockMessage = createWebDavMessage(i); messages.add(mockMessage); } FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.FLAGS); folder.fetch(messages, profile, listener); }
private void loadSearchResultsSynchronous(Account account, List<String> messageServerIds, LocalFolder localFolder) throws MessagingException { FetchProfile fetchProfile = new FetchProfile(); fetchProfile.add(FetchProfile.Item.FLAGS); fetchProfile.add(FetchProfile.Item.ENVELOPE); fetchProfile.add(FetchProfile.Item.STRUCTURE); Backend backend = getBackend(account); String folderServerId = localFolder.getServerId(); for (String messageServerId : messageServerIds) { LocalMessage localMessage = localFolder.getMessage(messageServerId); if (localMessage == null) { Message message = backend.fetchMessage(folderServerId, messageServerId, fetchProfile); localFolder.appendMessages(Collections.singletonList(message)); } } }
@Test public void folder_can_handle_empty_response_to_body_request() throws MessagingException, IOException { setupStoreForMessageFetching(); List<WebDavMessage> messages = setup25MessagesToFetch(); when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(HttpContext.class))).thenAnswer( new Answer<HttpResponse>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { HttpResponse httpResponse = mock(HttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); return httpResponse; } }); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.BODY_SANE); folder.fetch(messages, profile, listener); verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25)); verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25)); }
public LocalMessage loadMessage(Account account, String folderServerId, String uid) throws MessagingException { LocalStore localStore = localStoreProvider.getInstance(account); LocalFolder localFolder = localStore.getFolder(folderServerId); localFolder.open(Folder.OPEN_MODE_RW); LocalMessage message = localFolder.getMessage(uid); if (message == null || message.getDatabaseId() == 0) { throw new IllegalArgumentException("Message not found: folder=" + folderServerId + ", uid=" + uid); } FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localFolder.fetch(Collections.singletonList(message), fp, null); localFolder.close(); notificationController.removeNewMailNotification(account, message.makeMessageReference()); markMessageAsReadOnView(account, message); return message; }
@Test public void fetch_withEnvelopeProfile_setsSizeOfMessage() throws MessagingException, IOException { folder.open(Folder.OPEN_MODE_RW); List<Pop3Message> messageList = setupMessageFromServer(); FetchProfile fetchProfile = new FetchProfile(); fetchProfile.add(Item.ENVELOPE); when(mockConnection.readLine()).thenReturn("1 100").thenReturn("."); folder.fetch(messageList, fetchProfile, mockListener); assertEquals(100, messageList.get(0).getSize()); }
@Test public void folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies() throws MessagingException, IOException, URISyntaxException { setupStoreForMessageFetching(); when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List<WebDavMessage> messages = setup25MessagesToFetch(); when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(HttpContext.class))).thenAnswer( new Answer<HttpResponse>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { HttpResponse httpResponse = mock(HttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); BasicHttpEntity httpEntity = new BasicHttpEntity(); String body = ""; httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8"))); when(httpResponse.getEntity()).thenReturn(httpEntity); return httpResponse; } }); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.FLAGS); profile.add(FetchProfile.Item.BODY); folder.fetch(messages, profile, listener); verify(listener, times(25)).messageStarted(any(String.class), anyInt(), anyInt()); verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), anyInt()); }
@Test public void fetch_withBodyProfile_setsContentOfMessage() throws MessagingException, IOException { InputStream messageInputStream = new ByteArrayInputStream(( "From: <adam@example.org>\r\n" + "To: <eva@example.org>\r\n" + "Subject: Testmail\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: text/plain\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "this is some test text.").getBytes()); folder.open(Folder.OPEN_MODE_RW); List<Pop3Message> messageList = setupMessageFromServer(); FetchProfile fetchProfile = new FetchProfile(); fetchProfile.add(Item.BODY); when(mockConnection.readLine()).thenReturn("1 100").thenReturn("."); when(mockConnection.getInputStream()).thenReturn(messageInputStream); folder.fetch(messageList, fetchProfile, mockListener); ByteArrayOutputStream bodyData = new ByteArrayOutputStream(); messageList.get(0).getBody().writeTo(bodyData); assertEquals("this is some test text.", new String(bodyData.toByteArray(), "UTF-8")); }
@Test public void migrateTextHtml() throws Exception { SQLiteDatabase db = createV50Database(); insertMultipartAlternativeMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("9"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(8, msg.getDatabaseId()); Assert.assertEquals(9, msg.getHeaderNames().size()); Assert.assertEquals("multipart/alternative", msg.getMimeType()); Assert.assertEquals(0, msg.getAttachmentCount()); Multipart msgBody = (Multipart) msg.getBody(); Assert.assertEquals("------------060200010509000000040004", msgBody.getBoundary()); }
@Test public void migratePgpInlineEncryptedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertPgpInlineEncryptedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("7"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(6, msg.getDatabaseId()); Assert.assertEquals(12, msg.getHeaderNames().size()); Assert.assertEquals("text/plain", msg.getMimeType()); Assert.assertEquals(0, msg.getAttachmentCount()); Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody); String msgTextContent = MessageExtractor.getTextFromPart(msg); Assert.assertEquals(OpenPgpUtils.PARSE_RESULT_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent)); }
@Test public void migratePgpInlineClearsignedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertPgpInlineClearsignedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("8"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(7, msg.getDatabaseId()); Assert.assertEquals(12, msg.getHeaderNames().size()); Assert.assertEquals("text/plain", msg.getMimeType()); Assert.assertEquals(0, msg.getAttachmentCount()); Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody); String msgTextContent = MessageExtractor.getTextFromPart(msg); Assert.assertEquals(OpenPgpUtils.PARSE_RESULT_SIGNED_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent)); }
@Test public void migrateHtmlWithRelatedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertHtmlWithRelatedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("10"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(9, msg.getDatabaseId()); Assert.assertEquals(11, msg.getHeaderNames().size()); Assert.assertEquals("multipart/mixed", msg.getMimeType()); Assert.assertEquals(1, msg.getAttachmentCount()); Multipart msgBody = (Multipart) msg.getBody(); Assert.assertEquals("------------050707070308090509030605", msgBody.getBoundary()); Multipart multipartAlternativePart = (Multipart) msgBody.getBodyPart(0).getBody(); BodyPart htmlPart = multipartAlternativePart.getBodyPart(1); String msgTextContent = MessageExtractor.getTextFromPart(htmlPart); Assert.assertNotNull(msgTextContent); Assert.assertTrue(msgTextContent.contains("cid:part1.07090108.09020601@example.org")); Assert.assertEquals("image/jpeg", msgBody.getBodyPart(1).getMimeType()); }
@Test public void migratePgpMimeSignedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertPgpMimeSignedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("5"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(4, msg.getDatabaseId()); Assert.assertEquals(8, msg.getHeaderNames().size()); Assert.assertEquals("multipart/mixed", msg.getMimeType()); Assert.assertEquals(2, msg.getAttachmentCount()); Multipart body = (Multipart) msg.getBody(); Assert.assertEquals(3, body.getCount()); Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType()); Assert.assertEquals("image/png", body.getBodyPart(1).getMimeType()); Assert.assertEquals("application/pgp-signature", body.getBodyPart(2).getMimeType()); }
@Test public void migrateTextPlain() throws Exception { SQLiteDatabase db = createV50Database(); insertSimplePlaintextMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("3"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals("text/plain", msg.getMimeType()); Assert.assertEquals(2, msg.getDatabaseId()); Assert.assertEquals(13, msg.getHeaderNames().size()); Assert.assertEquals(0, msg.getAttachmentCount()); Assert.assertEquals(1, msg.getHeader("User-Agent").length); Assert.assertEquals("Mutt/1.5.24 (2015-08-30)", msg.getHeader("User-Agent")[0]); Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length); Assert.assertEquals("text/plain", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null)); Assert.assertEquals("utf-8", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "charset")); Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody); String msgTextContent = MessageExtractor.getTextFromPart(msg); Assert.assertEquals("nothing special here.\r\n", msgTextContent); }