private void sendDone() { try { imapConnection.setReadTimeout(RemoteStore.SOCKET_READ_TIMEOUT); imapConnection.sendContinuation("DONE"); } catch (IOException e) { imapConnection.close(); } } }
private MessagingException handleAuthenticationFailure(NegativeImapResponseException e) { ImapResponse lastResponse = e.getLastResponse(); String responseCode = ResponseCodeExtractor.getResponseCode(lastResponse); // If there's no response code we simply assume it was an authentication failure. if (responseCode == null || responseCode.equals(ResponseCodeExtractor.AUTHENTICATION_FAILED)) { if (e.wasByeResponseReceived()) { close(); } return new AuthenticationFailedException(e.getMessage()); } else { close(); return e; } }
@Override public void checkSettings() throws MessagingException { try { ImapConnection connection = createImapConnection(); connection.open(); connection.close(); } catch (IOException ioe) { throw new MessagingException("Unable to connect", ioe); } }
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; }
@Override public void close() { messageCount = -1; if (!isOpen()) { return; } synchronized (this) { // If we are mid-search and we get a close request, we gotta trash the connection. if (inSearch && connection != null) { Timber.i("IMAP search was aborted, shutting down connection."); connection.close(); } else { store.releaseConnection(connection); } connection = null; } }
public void stop() { synchronized (threadLock) { if (listeningThread == null) { throw new IllegalStateException("stop() called twice"); } stop = true; listeningThread.interrupt(); listeningThread = null; } ImapConnection conn = connection; if (conn != null) { if (K9MailLib.isDebug()) { Timber.v("Closing connection to stop pushing for %s", getLogId()); } conn.close(); } else { Timber.w("Attempt to interrupt null connection to stop pushing on folderPusher for %s", getLogId()); } }
public String sendSaslIrCommand(String command, String initialClientResponse, boolean sensitive) throws IOException, MessagingException { try { open(); String tag = Integer.toString(nextCommandTag++); String commandToSend = tag + " " + command + " " + initialClientResponse + "\r\n"; outputStream.write(commandToSend.getBytes()); outputStream.flush(); if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { if (sensitive && !K9MailLib.isDebugSensitive()) { Timber.v("%s>>> [Command Hidden, Enable Sensitive Debug Logging To Show]", getLogId()); } else { Timber.v("%s>>> %s %s %s", getLogId(), tag, command, initialClientResponse); } } return tag; } catch (IOException | MessagingException e) { close(); throw e; } }
public ImapResponse readResponse(ImapResponseCallback callback) throws IOException { try { ImapResponse response = responseParser.readResponse(callback); if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { Timber.v("%s<<<%s", getLogId(), response); } return response; } catch (IOException e) { close(); throw e; } }
private MessagingException ioExceptionHandler(ImapConnection connection, IOException ioe) { Timber.e(ioe, "IOException for %s", getLogId()); if (connection != null) { connection.close(); } close(); return new MessagingException("IO Error", ioe); }
private void sendIdle(ImapConnection conn) throws MessagingException, IOException { String tag = conn.sendCommand(Commands.IDLE, false); List<ImapResponse> responses; try { try { responses = conn.readStatusResponse(tag, Commands.IDLE, this); } finally { idleStopper.stopAcceptingDoneContinuation(); } } catch (IOException e) { conn.close(); throw e; } handleUntaggedResponses(responses); }
private void enableCompression() throws IOException, MessagingException { try { executeSimpleCommand(Commands.COMPRESS_DEFLATE); } catch (NegativeImapResponseException e) { Timber.d(e, "Unable to negotiate compression: "); return; } try { InflaterInputStream input = new InflaterInputStream(socket.getInputStream(), new Inflater(true)); ZOutputStream output = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true); output.setFlushMode(JZlib.Z_PARTIAL_FLUSH); setUpStreamsAndParser(input, output); if (K9MailLib.isDebug()) { Timber.i("Compression enabled for %s", getLogId()); } } catch (IOException e) { close(); Timber.e(e, "Error enabling compression"); } }
public List<ImapResponse> executeSimpleCommand(String command, boolean sensitive) throws IOException, MessagingException { String commandToLog = command; if (sensitive && !K9MailLib.isDebugSensitive()) { commandToLog = "*sensitive*"; } String tag = sendCommand(command, sensitive); try { return responseParser.readStatusResponse(tag, commandToLog, getLogId(), null); } catch (IOException e) { close(); throw e; } }
@Test public void close_withoutOpen_shouldNotThrow() throws Exception { ImapConnection imapConnection = createImapConnection( settings, socketFactory, connectivityManager, oAuth2TokenProvider); imapConnection.close(); }
@Test public void getPersonalNamespaces_withoutException_shouldLeaveImapConnectionOpen() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); List<ImapResponse> imapResponses = Collections.singletonList(createImapResponse("5 OK Success")); when(imapConnection.executeSimpleCommand(anyString())).thenReturn(imapResponses); imapStore.enqueueImapConnection(imapConnection); imapStore.getPersonalNamespaces(); verify(imapConnection, never()).close(); }
@Test public void getPersonalNamespaces_withIoException_shouldCloseImapConnection() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); doThrow(IOException.class).when(imapConnection).executeSimpleCommand("LIST \"\" \"*\""); imapStore.enqueueImapConnection(imapConnection); try { imapStore.getPersonalNamespaces(); fail("Expected exception"); } catch (MessagingException ignored) { } verify(imapConnection).close(); }
private void reacquireWakeLockAndCleanUp() { wakeLock.acquire(PUSH_WAKE_LOCK_TIMEOUT); clearStoredUntaggedResponses(); idling = false; pushReceiver.setPushActive(getServerId(), false); try { connection.close(); } catch (Exception me) { Timber.e(me, "Got exception while closing for exception for %s", getLogId()); } connection = null; }
@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); } }
@Test public void close_afterOpen_shouldCloseConnection() throws Exception { MockImapServer server = new MockImapServer(); ImapConnection imapConnection = simpleOpen(server); imapConnection.close(); server.verifyConnectionClosed(); server.shutdown(); }
@Test public void isConnected_afterOpenAndClose_shouldReturnFalse() throws Exception { MockImapServer server = new MockImapServer(); ImapConnection imapConnection = simpleOpen(server); imapConnection.close(); boolean result = imapConnection.isConnected(); assertFalse(result); server.verifyConnectionClosed(); server.shutdown(); }
@Test public void open_afterCloseWasCalled_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server); server.expect("2 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\""); server.output("2 OK LOGIN completed"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); imapConnection.close(); try { imapConnection.open(); fail("Expected exception"); } catch (IllegalStateException e) { assertEquals("open() called after close(). Check wrapped exception to see where close() was called.", e.getMessage()); } }