Search in sources :

Example 6 with WebDavMessage

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);
    }
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage) LinkedList(java.util.LinkedList)

Example 7 with WebDavMessage

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;
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) StringEntity(org.apache.http.entity.StringEntity) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with WebDavMessage

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);
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader)

Example 9 with WebDavMessage

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);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with WebDavMessage

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));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) FetchProfile(com.fsck.k9.mail.FetchProfile) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

FetchProfile (com.fsck.k9.mail.FetchProfile)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)9 WebDavMessage (com.fsck.k9.mail.store.webdav.WebDavMessage)7 HttpResponse (org.apache.http.HttpResponse)7 MessagingException (com.fsck.k9.mail.MessagingException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 StatusLine (org.apache.http.StatusLine)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 HttpContext (org.apache.http.protocol.HttpContext)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (com.fsck.k9.mail.Message)3 WebDavFolder (com.fsck.k9.mail.store.webdav.WebDavFolder)3 URISyntaxException (java.net.URISyntaxException)3 Date (java.util.Date)3 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)3 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 Flag (com.fsck.k9.mail.Flag)2