use of com.fsck.k9.mail.Message in project k-9 by k9mail.
the class EncryptionDetectorTest method isEncrypted_withSMimePart_shouldReturnTrue.
@Test
public void isEncrypted_withSMimePart_shouldReturnTrue() throws Exception {
Message message = createMessage("application/pkcs7-mime");
boolean encrypted = encryptionDetector.isEncrypted(message);
assertTrue(encrypted);
}
use of com.fsck.k9.mail.Message in project k-9 by k9mail.
the class EncryptionDetectorTest method isEncrypted_withTextPlain_shouldReturnFalse.
@Test
public void isEncrypted_withTextPlain_shouldReturnFalse() throws Exception {
Message message = createTextMessage("text/plain", "plain text");
boolean encrypted = encryptionDetector.isEncrypted(message);
assertFalse(encrypted);
}
use of com.fsck.k9.mail.Message in project k-9 by k9mail.
the class EncryptionDetectorTest method isEncrypted_withMultipartEncrypted_shouldReturnTrue.
@Test
public void isEncrypted_withMultipartEncrypted_shouldReturnTrue() throws Exception {
Message message = createMultipartMessage("multipart/encrypted", createPart("application/octet-stream"), createPart("application/octet-stream"));
boolean encrypted = encryptionDetector.isEncrypted(message);
assertTrue(encrypted);
}
use of com.fsck.k9.mail.Message 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()));
Log.i(LOG_TAG, "Fetching messages with UID = '" + wdMessage.getUid() + "', URL = '" + wdMessage.getUrl() + "'");
if (wdMessage.getUrl().equals("")) {
throw new MessagingException("Unable to get URL for message");
}
}
try {
Log.i(LOG_TAG, "Fetching message with UID = '" + wdMessage.getUid() + "', URL = '" + 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) {
Log.e(LOG_TAG, "IOException: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe));
throw new MessagingException("I/O Error", ioe);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(istream);
}
} else {
Log.v(LOG_TAG, "Empty response");
}
} catch (IllegalArgumentException iae) {
Log.e(LOG_TAG, "IllegalArgumentException caught " + iae + "\nTrace: " + WebDavUtils.processException(iae));
throw new MessagingException("IllegalArgumentException caught", iae);
} catch (URISyntaxException use) {
Log.e(LOG_TAG, "URISyntaxException caught " + use + "\nTrace: " + WebDavUtils.processException(use));
throw new MessagingException("URISyntaxException caught", use);
} catch (IOException ioe) {
Log.e(LOG_TAG, "Non-success response code loading message, response code was " + statusCode + "\nURL: " + wdMessage.getUrl() + "\nError: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe));
throw new MessagingException("Failure code " + statusCode, ioe);
}
if (listener != null) {
listener.messageFinished(wdMessage, i, count);
}
}
}
use of com.fsck.k9.mail.Message in project k-9 by k9mail.
the class ImapFolder method fetchPart.
@Override
public void fetchPart(Message message, Part part, MessageRetrievalListener<Message> listener) throws MessagingException {
checkOpen();
String partId = part.getServerExtra();
String fetch;
if ("TEXT".equalsIgnoreCase(partId)) {
int maximumAutoDownloadMessageSize = store.getStoreConfig().getMaximumAutoDownloadMessageSize();
fetch = String.format(Locale.US, "BODY.PEEK[TEXT]<0.%d>", maximumAutoDownloadMessageSize);
} else {
fetch = String.format("BODY.PEEK[%s]", partId);
}
try {
String command = String.format("UID FETCH %s (UID %s)", message.getUid(), fetch);
connection.sendCommand(command, false);
ImapResponse response;
int messageNumber = 0;
ImapResponseCallback callback = new FetchPartCallback(part);
do {
response = connection.readResponse(callback);
if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) {
ImapList fetchList = (ImapList) response.getKeyedValue("FETCH");
String uid = fetchList.getKeyedString("UID");
if (!message.getUid().equals(uid)) {
if (K9MailLib.isDebug()) {
Log.d(LOG_TAG, "Did not ask for UID " + uid + " for " + getLogId());
}
handleUntaggedResponse(response);
continue;
}
if (listener != null) {
listener.messageStarted(uid, messageNumber++, 1);
}
ImapMessage imapMessage = (ImapMessage) message;
Object literal = handleFetchResponse(imapMessage, fetchList);
if (literal != null) {
if (literal instanceof Body) {
// Most of the work was done in FetchAttchmentCallback.foundLiteral()
MimeMessageHelper.setBody(part, (Body) literal);
} else if (literal instanceof String) {
String bodyString = (String) literal;
InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream, contentTransferEncoding, contentType));
} else {
// This shouldn't happen
throw new MessagingException("Got FETCH response with bogus parameters");
}
}
if (listener != null) {
listener.messageFinished(message, messageNumber, 1);
}
} else {
handleUntaggedResponse(response);
}
} while (response.getTag() == null);
} catch (IOException ioe) {
throw ioExceptionHandler(connection, ioe);
}
}
Aggregations