public List<ImapResponse> readStatusResponse(String tag, String commandToLog, UntaggedHandler untaggedHandler) throws IOException, NegativeImapResponseException { return responseParser.readStatusResponse(tag, commandToLog, getLogId(), untaggedHandler); }
private List<ImapResponse> saslAuthPlain() throws IOException, MessagingException { String command = Commands.AUTHENTICATE_PLAIN; String tag = sendCommand(command, false); readContinuationResponse(tag); String credentials = "\000" + settings.getUsername() + "\000" + settings.getPassword(); byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes()); outputStream.write(encodedCredentials); outputStream.write('\r'); outputStream.write('\n'); outputStream.flush(); try { return responseParser.readStatusResponse(tag, command, getLogId(), null); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }
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; } }
private List<ImapResponse> authCramMD5() throws MessagingException, IOException { String command = Commands.AUTHENTICATE_CRAM_MD5; String tag = sendCommand(command, false); ImapResponse response = readContinuationResponse(tag); if (response.size() != 1 || !(response.get(0) instanceof String)) { throw new MessagingException("Invalid Cram-MD5 nonce received"); } byte[] b64Nonce = response.getString(0).getBytes(); byte[] b64CRAM = Authentication.computeCramMd5Bytes(settings.getUsername(), settings.getPassword(), b64Nonce); outputStream.write(b64CRAM); outputStream.write('\r'); outputStream.write('\n'); outputStream.flush(); try { return responseParser.readStatusResponse(tag, command, getLogId(), null); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }
@Test(expected = NegativeImapResponseException.class) public void testReadStatusResponseWithErrorResponse() throws Exception { ImapResponseParser parser = createParser("* COMMAND BAR BAZ\r\nTAG ERROR COMMAND errored\r\n"); parser.readStatusResponse("TAG", null, null, null); }
@Test public void testReadStatusResponseWithOKResponse() throws Exception { ImapResponseParser parser = createParser("* COMMAND BAR\tBAZ\r\n" + "TAG OK COMMAND completed\r\n"); List<ImapResponse> responses = parser.readStatusResponse("TAG", null, null, null); assertEquals(2, responses.size()); assertEquals(asList("COMMAND", "BAR", "BAZ"), responses.get(0)); assertEquals(asList("OK", "COMMAND completed"), responses.get(1)); }
private List<ImapResponse> attemptXOAuth2() throws MessagingException, IOException { String token = oauthTokenProvider.getToken(settings.getUsername(), OAuth2TokenProvider.OAUTH2_TIMEOUT); String authString = Authentication.computeXoauth(settings.getUsername(), token); String tag = sendSaslIrCommand(Commands.AUTHENTICATE_XOAUTH2, authString, true); return responseParser.readStatusResponse(tag, Commands.AUTHENTICATE_XOAUTH2, getLogId(), new UntaggedHandler() { @Override public void handleAsyncUntaggedResponse(ImapResponse response) throws IOException { handleXOAuthUntaggedResponse(response); } }); }
@Test public void testReadStatusResponseUntaggedHandlerGetsUntaggedOnly() throws Exception { ImapResponseParser parser = createParser( "* UNTAGGED\r\n" + "A2 OK COMMAND completed\r\n"); TestUntaggedHandler untaggedHandler = new TestUntaggedHandler(); parser.readStatusResponse("A2", null, null, untaggedHandler); assertEquals(1, untaggedHandler.responses.size()); assertEquals(asList("UNTAGGED"), untaggedHandler.responses.get(0)); }
@Test public void testReadStatusResponseUntaggedHandlerStillCalledOnNegativeReply() throws Exception { ImapResponseParser parser = createParser( "+ text\r\n" + "A2 NO Bad response\r\n"); TestUntaggedHandler untaggedHandler = new TestUntaggedHandler(); try { List<ImapResponse> responses = parser.readStatusResponse("A2", null, null, untaggedHandler); } catch (NegativeImapResponseException e) { } assertEquals(1, untaggedHandler.responses.size()); assertEquals(asList("text"), untaggedHandler.responses.get(0)); }
@Test public void testReadStatusResponseSkippingWrongTag() throws Exception { ImapResponseParser parser = createParser("* UNTAGGED\r\n" + "* 0 EXPUNGE\r\n" + "* 42 EXISTS\r\n" + "A1 COMMAND BAR BAZ\r\n" + "A2 OK COMMAND completed\r\n"); TestUntaggedHandler untaggedHandler = new TestUntaggedHandler(); List<ImapResponse> responses = parser.readStatusResponse("A2", null, null, untaggedHandler); assertEquals(3, responses.size()); assertEquals(asList("0", "EXPUNGE"), responses.get(0)); assertEquals(asList("42", "EXISTS"), responses.get(1)); assertEquals(asList("OK", "COMMAND completed"), responses.get(2)); assertEquals(asList("UNTAGGED"), untaggedHandler.responses.get(0)); assertEquals(responses.get(0), untaggedHandler.responses.get(1)); assertEquals(responses.get(1), untaggedHandler.responses.get(2)); }
@Test public void readStatusResponse_withNoResponse_shouldThrow() throws Exception { ImapResponseParser parser = createParser("1 NO\r\n"); try { parser.readStatusResponse("1", "COMMAND", "[logId]", null); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals("Command: COMMAND; response: #1# [NO]", e.getMessage()); } }
@Test public void readStatusResponse_withNoResponseAndAlertText_shouldThrowWithAlertText() throws Exception { ImapResponseParser parser = createParser("1 NO [ALERT] Access denied\r\n"); try { parser.readStatusResponse("1", "COMMAND", "[logId]", null); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals("Access denied", e.getAlertText()); } }