@Test public void testGetMessageContentWithNullDisposition() throws Exception { doReturn(false).when(messageMock).isMimeType(MimeTypes.TEXT_PLAIN); doReturn(false).when(messageMock).isMimeType(MimeTypes.TEXT_HTML); doReturn(multipartMock).when(messageMock).getContent(); int one = 1; doReturn(one).when(multipartMock).getCount(); doReturn(partMock).when(multipartMock).getBodyPart(anyInt()); doReturn(null).when(partMock).getDisposition(); doReturn("text/plain; charset=utf-8; format=flowed").when(partMock).getContentType(); doReturn(inputStreamMock).when(partMock).getInputStream(); byte[] bytes = {1}; PowerMockito.mockStatic(ASCIIUtility.class); PowerMockito.doReturn(bytes).when(ASCIIUtility.class, "getBytes", inputStreamMock); PowerMockito.whenNew(ByteArrayInputStream.class).withArguments(bytes).thenReturn(byteArrayInputStreamMock); int byteArrayInputStreamMockCount = 1; doReturn(byteArrayInputStreamMockCount).when(byteArrayInputStreamMock).available(); doReturn(byteArrayInputStreamMockCount) .when(byteArrayInputStreamMock) .read(new byte[byteArrayInputStreamMockCount], 0, byteArrayInputStreamMockCount); String testCMessage = "testCMessage"; PowerMockito.mockStatic(MimeUtility.class); PowerMockito.doReturn(testCMessage).when(MimeUtility.class, "decodeText", anyString()); serviceSpy.input = inputBuilder.build(); Map<String, String> contentMessage = serviceSpy.getMessageByContentTypes(messageMock, CHARACTERSET); assertEquals(testCMessage, contentMessage.get("text/plain")); verify(messageMock).isMimeType(MimeTypes.TEXT_PLAIN); verify(messageMock).isMimeType(MimeTypes.TEXT_HTML); verify(messageMock).getContent(); verify(multipartMock).getCount(); verify(partMock).getDisposition(); verify(partMock).getInputStream(); PowerMockito.verifyStatic(); ASCIIUtility.getBytes(inputStreamMock); PowerMockito.verifyNew(ByteArrayInputStream.class).withArguments(bytes); verify(byteArrayInputStreamMock).available(); verify(byteArrayInputStreamMock).read(new byte[byteArrayInputStreamMockCount], 0, byteArrayInputStreamMockCount); PowerMockito.verifyStatic(); MimeUtility.decodeText(anyString()); }
protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception { Map<String, String> messageMap = new HashMap<>(); if (message.isMimeType(MimeTypes.TEXT_PLAIN)) { messageMap.put(MimeTypes.TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString())); } else if (message.isMimeType(MimeTypes.TEXT_HTML)) { messageMap.put(MimeTypes.TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString()))); } else if (message.isMimeType(MimeTypes.MULTIPART_MIXED) || message.isMimeType(MimeTypes.MULTIPART_RELATED)) { messageMap.put(MimeTypes.MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet)); } else { Object obj = message.getContent(); Multipart mpart = (Multipart) obj; for (int i = 0, n = mpart.getCount(); i < n; i++) { Part part = mpart.getBodyPart(i); if (input.isEncryptedMessage() && part.getContentType() != null && part.getContentType().matches(SecurityConstants.ENCRYPTED_CONTENT_TYPE)) { part = decryptPart((MimeBodyPart) part); } String disposition = part.getDisposition(); String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";")); if (disposition == null) { if (part.getContent() instanceof MimeMultipart) { MimeMultipart mm = (MimeMultipart) part.getContent(); for (int j = 0; j < mm.getCount(); j++) { if (mm.getBodyPart(j).getContent() instanceof String) { BodyPart bodyPart = mm.getBodyPart(j); if ((characterSet != null) && (characterSet.trim().length() > 0)) { String contentType = bodyPart.getHeader(CONTENT_TYPE)[0]; contentType = contentType .replace(contentType.substring(contentType.indexOf("=") + 1), characterSet); bodyPart.setHeader(CONTENT_TYPE, contentType); } String partContentType1 = bodyPart .getContentType().substring(0, bodyPart.getContentType().indexOf(";")); messageMap.put(partContentType1, MimeUtility.decodeText(bodyPart.getContent().toString())); } } } else { if ((characterSet != null) && (characterSet.trim().length() > 0)) { InputStream istream = part.getInputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream)); int count = bis.available(); byte[] bytes = new byte[count]; count = bis.read(bytes, 0, count); messageMap.put(partContentType, MimeUtility.decodeText(new String(bytes, 0, count, characterSet))); } else { messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString())); } } } } } return messageMap; }
GetMailMessageService { protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception { Map<String, String> messageMap = new HashMap<>(); if (message.isMimeType(MimeTypes.TEXT_PLAIN)) { messageMap.put(MimeTypes.TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString())); } else if (message.isMimeType(MimeTypes.TEXT_HTML)) { messageMap.put(MimeTypes.TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString()))); } else if (message.isMimeType(MimeTypes.MULTIPART_MIXED) || message.isMimeType(MimeTypes.MULTIPART_RELATED)) { messageMap.put(MimeTypes.MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet)); } else { Object obj = message.getContent(); Multipart mpart = (Multipart) obj; for (int i = 0, n = mpart.getCount(); i < n; i++) { Part part = mpart.getBodyPart(i); if (input.isEncryptedMessage() && part.getContentType() != null && part.getContentType().matches(SecurityConstants.ENCRYPTED_CONTENT_TYPE)) { part = decryptPart((MimeBodyPart) part); } String disposition = part.getDisposition(); String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";")); if (disposition == null) { if (part.getContent() instanceof MimeMultipart) { MimeMultipart mm = (MimeMultipart) part.getContent(); for (int j = 0; j < mm.getCount(); j++) { if (mm.getBodyPart(j).getContent() instanceof String) { BodyPart bodyPart = mm.getBodyPart(j); if ((characterSet != null) && (characterSet.trim().length() > 0)) { String contentType = bodyPart.getHeader(CONTENT_TYPE)[0]; contentType = contentType .replace(contentType.substring(contentType.indexOf("=") + 1), characterSet); bodyPart.setHeader(CONTENT_TYPE, contentType); } String partContentType1 = bodyPart .getContentType().substring(0, bodyPart.getContentType().indexOf(";")); messageMap.put(partContentType1, MimeUtility.decodeText(bodyPart.getContent().toString())); } } } else { if ((characterSet != null) && (characterSet.trim().length() > 0)) { InputStream istream = part.getInputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream)); int count = bis.available(); byte[] bytes = new byte[count]; count = bis.read(bytes, 0, count); messageMap.put(partContentType, MimeUtility.decodeText(new String(bytes, 0, count, characterSet))); } else { messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString())); } } } } } return messageMap; } }
GetMailMessageService { protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception { Map<String, String> messageMap = new HashMap<>(); if (message.isMimeType(MimeTypes.TEXT_PLAIN)) { messageMap.put(MimeTypes.TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString())); } else if (message.isMimeType(MimeTypes.TEXT_HTML)) { messageMap.put(MimeTypes.TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString()))); } else if (message.isMimeType(MimeTypes.MULTIPART_MIXED) || message.isMimeType(MimeTypes.MULTIPART_RELATED)) { messageMap.put(MimeTypes.MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet)); } else { Object obj = message.getContent(); Multipart mpart = (Multipart) obj; for (int i = 0, n = mpart.getCount(); i < n; i++) { Part part = mpart.getBodyPart(i); if (input.isEncryptedMessage() && part.getContentType() != null && part.getContentType().matches(SecurityConstants.ENCRYPTED_CONTENT_TYPE)) { part = decryptPart((MimeBodyPart) part); } String disposition = part.getDisposition(); String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";")); if (disposition == null) { if (part.getContent() instanceof MimeMultipart) { MimeMultipart mm = (MimeMultipart) part.getContent(); for (int j = 0; j < mm.getCount(); j++) { if (mm.getBodyPart(j).getContent() instanceof String) { BodyPart bodyPart = mm.getBodyPart(j); if ((characterSet != null) && (characterSet.trim().length() > 0)) { String contentType = bodyPart.getHeader(CONTENT_TYPE)[0]; contentType = contentType .replace(contentType.substring(contentType.indexOf("=") + 1), characterSet); bodyPart.setHeader(CONTENT_TYPE, contentType); } String partContentType1 = bodyPart .getContentType().substring(0, bodyPart.getContentType().indexOf(";")); messageMap.put(partContentType1, MimeUtility.decodeText(bodyPart.getContent().toString())); } } } else { if ((characterSet != null) && (characterSet.trim().length() > 0)) { InputStream istream = part.getInputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream)); int count = bis.available(); byte[] bytes = new byte[count]; count = bis.read(bytes, 0, count); messageMap.put(partContentType, MimeUtility.decodeText(new String(bytes, 0, count, characterSet))); } else { messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString())); } } } } } return messageMap; }  }
GetMailMessageService { protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception { Map<String, String> messageMap = new HashMap<>(); if (message.isMimeType(MimeTypes.TEXT_PLAIN)) { messageMap.put(MimeTypes.TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString())); } else if (message.isMimeType(MimeTypes.TEXT_HTML)) { messageMap.put(MimeTypes.TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString()))); } else if (message.isMimeType(MimeTypes.MULTIPART_MIXED) || message.isMimeType(MimeTypes.MULTIPART_RELATED)) { messageMap.put(MimeTypes.MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet)); } else { Object obj = message.getContent(); Multipart mpart = (Multipart) obj; for (int i = 0, n = mpart.getCount(); i < n; i++) { Part part = mpart.getBodyPart(i); if (input.isEncryptedMessage() && part.getContentType() != null && part.getContentType().matches(SecurityConstants.ENCRYPTED_CONTENT_TYPE)) { part = decryptPart((MimeBodyPart) part); } String disposition = part.getDisposition(); String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";")); if (disposition == null) { if (part.getContent() instanceof MimeMultipart) { MimeMultipart mm = (MimeMultipart) part.getContent(); for (int j = 0; j < mm.getCount(); j++) { if (mm.getBodyPart(j).getContent() instanceof String) { BodyPart bodyPart = mm.getBodyPart(j); if ((characterSet != null) && (characterSet.trim().length() > 0)) { String contentType = bodyPart.getHeader(CONTENT_TYPE)[0]; contentType = contentType .replace(contentType.substring(contentType.indexOf("=") + 1), characterSet); bodyPart.setHeader(CONTENT_TYPE, contentType); } String partContentType1 = bodyPart .getContentType().substring(0, bodyPart.getContentType().indexOf(";")); messageMap.put(partContentType1, MimeUtility.decodeText(bodyPart.getContent().toString())); } } } else { if ((characterSet != null) && (characterSet.trim().length() > 0)) { InputStream istream = part.getInputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream)); int count = bis.available(); byte[] bytes = new byte[count]; count = bis.read(bytes, 0, count); messageMap.put(partContentType, MimeUtility.decodeText(new String(bytes, 0, count, characterSet))); } else { messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString())); } } } } } return messageMap; }  Map<String, String> execute(GetMailMessageInput getMailMessageInput); }
GetMailMessageService { protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception { Map<String, String> messageMap = new HashMap<>(); if (message.isMimeType(MimeTypes.TEXT_PLAIN)) { messageMap.put(MimeTypes.TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString())); } else if (message.isMimeType(MimeTypes.TEXT_HTML)) { messageMap.put(MimeTypes.TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString()))); } else if (message.isMimeType(MimeTypes.MULTIPART_MIXED) || message.isMimeType(MimeTypes.MULTIPART_RELATED)) { messageMap.put(MimeTypes.MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet)); } else { Object obj = message.getContent(); Multipart mpart = (Multipart) obj; for (int i = 0, n = mpart.getCount(); i < n; i++) { Part part = mpart.getBodyPart(i); if (input.isEncryptedMessage() && part.getContentType() != null && part.getContentType().matches(SecurityConstants.ENCRYPTED_CONTENT_TYPE)) { part = decryptPart((MimeBodyPart) part); } String disposition = part.getDisposition(); String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";")); if (disposition == null) { if (part.getContent() instanceof MimeMultipart) { MimeMultipart mm = (MimeMultipart) part.getContent(); for (int j = 0; j < mm.getCount(); j++) { if (mm.getBodyPart(j).getContent() instanceof String) { BodyPart bodyPart = mm.getBodyPart(j); if ((characterSet != null) && (characterSet.trim().length() > 0)) { String contentType = bodyPart.getHeader(CONTENT_TYPE)[0]; contentType = contentType .replace(contentType.substring(contentType.indexOf("=") + 1), characterSet); bodyPart.setHeader(CONTENT_TYPE, contentType); } String partContentType1 = bodyPart .getContentType().substring(0, bodyPart.getContentType().indexOf(";")); messageMap.put(partContentType1, MimeUtility.decodeText(bodyPart.getContent().toString())); } } } else { if ((characterSet != null) && (characterSet.trim().length() > 0)) { InputStream istream = part.getInputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream)); int count = bis.available(); byte[] bytes = new byte[count]; count = bis.read(bytes, 0, count); messageMap.put(partContentType, MimeUtility.decodeText(new String(bytes, 0, count, characterSet))); } else { messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString())); } } } } } return messageMap; }  Map<String, String> execute(GetMailMessageInput getMailMessageInput);  }