use of com.fsck.k9.mail.store.webdav.WebDavMessage in project k-9 by k9mail.
the class WebDavSync method refreshLocalMessageFlags.
private void refreshLocalMessageFlags(final SyncConfig syncConfig, final WebDavFolder remoteFolder, final BackendFolder backendFolder, List<WebDavMessage> syncFlagMessages, final AtomicInteger progress, final int todo, SyncListener listener) throws MessagingException {
final String folder = remoteFolder.getServerId();
Timber.d("SYNC: About to sync flags for %d remote messages for folder %s", syncFlagMessages.size(), folder);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.FLAGS);
List<WebDavMessage> undeletedMessages = new LinkedList<>();
for (WebDavMessage message : syncFlagMessages) {
if (!message.isSet(Flag.DELETED)) {
undeletedMessages.add(message);
}
}
int maxDownloadSize = syncConfig.getMaximumAutoDownloadMessageSize();
remoteFolder.fetch(undeletedMessages, fp, null, maxDownloadSize);
for (WebDavMessage remoteMessage : syncFlagMessages) {
boolean messageChanged = syncFlags(syncConfig, backendFolder, remoteMessage);
if (messageChanged) {
listener.syncFlagChanged(folder, remoteMessage.getUid());
}
progress.incrementAndGet();
listener.syncProgress(folder, progress.get(), todo);
}
}
use of com.fsck.k9.mail.store.webdav.WebDavMessage in project k-9 by k9mail.
the class WebDavFolder method appendWebDavMessages.
public List<WebDavMessage> appendWebDavMessages(List<Message> messages) throws MessagingException {
List<WebDavMessage> retMessages = new ArrayList<>(messages.size());
WebDavHttpClient httpclient = store.getHttpClient();
for (Message message : messages) {
HttpGeneric httpmethod;
HttpResponse response;
StringEntity bodyEntity;
int statusCode;
try {
ByteArrayOutputStream out;
long size = message.getSize();
if (size > Integer.MAX_VALUE) {
throw new MessagingException("message size > Integer.MAX_VALUE!");
}
out = new ByteArrayOutputStream((int) size);
open();
EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(new BufferedOutputStream(out, 1024));
message.writeTo(msgOut);
msgOut.flush();
bodyEntity = new StringEntity(out.toString(), "UTF-8");
bodyEntity.setContentType("message/rfc822");
String messageURL = mFolderUrl;
if (!messageURL.endsWith("/")) {
messageURL += "/";
}
messageURL += encodeUtf8(message.getUid() + ":" + System.currentTimeMillis() + ".eml");
Timber.i("Uploading message as %s", messageURL);
store.sendRequest(messageURL, "PUT", bodyEntity, null, true);
WebDavMessage retMessage = new WebDavMessage(message.getUid(), this);
retMessage.setUrl(messageURL);
retMessages.add(retMessage);
} catch (Exception e) {
throw new MessagingException("Unable to append", e);
}
}
return retMessages;
}
use of com.fsck.k9.mail.store.webdav.WebDavMessage in project k-9 by k9mail.
the class WebDavFolder method fetchMessages.
/**
* Fetches the full messages or up to {@param lines} lines and passes them to the message parser.
*/
private void fetchMessages(List<WebDavMessage> messages, MessageRetrievalListener<WebDavMessage> listener, int lines) throws MessagingException {
WebDavHttpClient httpclient;
httpclient = store.getHttpClient();
/**
* We can't hand off to processRequest() since we need the stream to parse.
*/
for (int i = 0, count = messages.size(); i < count; i++) {
WebDavMessage wdMessage = messages.get(i);
int statusCode = 0;
if (listener != null) {
listener.messageStarted(wdMessage.getUid(), i, count);
}
/**
* If fetch is called outside of the initial list (ie, a locally stored message), it may not have a URL
* associated. Verify and fix that
*/
if (wdMessage.getUrl().equals("")) {
wdMessage.setUrl(getMessageUrls(new String[] { wdMessage.getUid() }).get(wdMessage.getUid()));
Timber.i("Fetching messages with UID = '%s', URL = '%s'", wdMessage.getUid(), wdMessage.getUrl());
if (wdMessage.getUrl().equals("")) {
throw new MessagingException("Unable to get URL for message");
}
}
try {
Timber.i("Fetching message with UID = '%s', URL = '%s'", wdMessage.getUid(), wdMessage.getUrl());
HttpGet httpget = new HttpGet(new URI(wdMessage.getUrl()));
HttpResponse response;
HttpEntity entity;
httpget.setHeader("translate", "f");
if (store.getAuthentication() == WebDavConstants.AUTH_TYPE_BASIC) {
httpget.setHeader("Authorization", store.getAuthString());
}
response = httpclient.executeOverride(httpget, store.getHttpContext());
statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode < 200 || statusCode > 300) {
throw new IOException("Error during with code " + statusCode + " during fetch: " + response.getStatusLine().toString());
}
if (entity != null) {
InputStream istream = null;
StringBuilder buffer = new StringBuilder();
String tempText;
String resultText;
BufferedReader reader = null;
int currentLines = 0;
try {
istream = WebDavHttpClient.getUngzippedContent(entity);
if (lines != -1) {
// Convert the ungzipped input stream into a StringBuilder
// containing the given line count
reader = new BufferedReader(new InputStreamReader(istream), 8192);
while ((tempText = reader.readLine()) != null && (currentLines < lines)) {
buffer.append(tempText).append("\r\n");
currentLines++;
}
IOUtils.closeQuietly(istream);
resultText = buffer.toString();
istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
}
// Parse either the entire message stream, or a stream of the given lines
wdMessage.parse(istream);
} catch (IOException ioe) {
Timber.e(ioe, "IOException during message parsing");
throw new MessagingException("I/O Error", ioe);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(istream);
}
} else {
Timber.v("Empty response");
}
} catch (IllegalArgumentException iae) {
Timber.e(iae, "IllegalArgumentException caught");
throw new MessagingException("IllegalArgumentException caught", iae);
} catch (URISyntaxException use) {
Timber.e(use, "URISyntaxException caught");
throw new MessagingException("URISyntaxException caught", use);
} catch (IOException ioe) {
Timber.e(ioe, "Non-success response code loading message, response code was %d, URL: %s", statusCode, wdMessage.getUrl());
throw new MessagingException("Failure code " + statusCode, ioe);
}
if (listener != null) {
listener.messageFinished(wdMessage, i, count);
}
}
}
use of com.fsck.k9.mail.store.webdav.WebDavMessage in project k-9 by k9mail.
the class WebDavFolderTest method folder_can_fetch_less_than_10_envelopes.
@Test
public void folder_can_fetch_less_than_10_envelopes() throws MessagingException {
when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())).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, MAX_DOWNLOAD_SIZE);
}
use of com.fsck.k9.mail.store.webdav.WebDavMessage in project k-9 by k9mail.
the class WebDavFolderTest method folder_can_handle_empty_response_to_body_request.
@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, MAX_DOWNLOAD_SIZE);
verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25));
verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25));
}
Aggregations