How to use getContent method of com.consol.citrus.mail.model.BodyPart class

Best Citrus code snippet using com.consol.citrus.mail.model.BodyPart.getContent

Source:MailMessageConverter.java Github

copy

Full Screen

...54 try {55 MimeMessage mimeMessage = endpointConfiguration.getJavaMailSender().createMimeMessage();56 MimeMailMessage mimeMailMessage = new MimeMailMessage(new MimeMessageHelper(mimeMessage,57 mailMessage.getBody().hasAttachments(),58 parseCharsetFromContentType(mailMessage.getBody().getContentType())));59 convertOutbound(mimeMailMessage, new DefaultMessage(mailMessage, message.getHeaders()), endpointConfiguration, context);60 return mimeMailMessage;61 } catch (MessagingException e) {62 throw new CitrusRuntimeException("Failed to create mail mime message", e);63 }64 }65 @Override66 public void convertOutbound(MimeMailMessage mimeMailMessage, Message message, MailEndpointConfiguration endpointConfiguration, TestContext context) {67 MailRequest mailRequest = getMailRequest(message, endpointConfiguration);68 try {69 mimeMailMessage.setFrom(mailRequest.getFrom());70 mimeMailMessage.setTo(StringUtils.commaDelimitedListToStringArray(mailRequest.getTo()));71 if (StringUtils.hasText(mailRequest.getCc())) {72 mimeMailMessage.setCc(StringUtils.commaDelimitedListToStringArray(mailRequest.getCc()));73 }74 if (StringUtils.hasText(mailRequest.getBcc())) {75 mimeMailMessage.setBcc(StringUtils.commaDelimitedListToStringArray(mailRequest.getBcc()));76 }77 mimeMailMessage.setReplyTo(mailRequest.getReplyTo() != null ? mailRequest.getReplyTo() : mailRequest.getFrom());78 mimeMailMessage.setSentDate(new Date());79 mimeMailMessage.setSubject(mailRequest.getSubject());80 mimeMailMessage.setText(mailRequest.getBody().getContent());81 if (mailRequest.getBody().hasAttachments()) {82 for (AttachmentPart attachmentPart : mailRequest.getBody().getAttachments().getAttachments()) {83 ByteArrayResource inputStreamSource = new ByteArrayResource(attachmentPart.getContent().getBytes(Charset.forName(parseCharsetFromContentType(attachmentPart.getContentType()))));84 mimeMailMessage.getMimeMessageHelper().addAttachment(attachmentPart.getFileName(), inputStreamSource,85 attachmentPart.getContentType());86 }87 }88 } catch (MessagingException e) {89 throw new CitrusRuntimeException("Failed to create mail mime message", e);90 }91 }92 @Override93 public MailMessage convertInbound(MimeMailMessage message, MailEndpointConfiguration endpointConfiguration, TestContext context) {94 try {95 Map<String, Object> messageHeaders = createMessageHeaders(message);96 return createMailRequest(messageHeaders, handlePart(message.getMimeMessage()), endpointConfiguration);97 } catch (MessagingException | IOException e) {98 throw new CitrusRuntimeException("Failed to convert mail mime message", e);99 }100 }101 /**102 * Creates a new mail message model object from message headers.103 * @param messageHeaders104 * @param bodyPart105 * @param endpointConfiguration106 * @return107 */108 protected MailMessage createMailRequest(Map<String, Object> messageHeaders, BodyPart bodyPart, MailEndpointConfiguration endpointConfiguration) {109 return MailMessage.request(messageHeaders)110 .marshaller(endpointConfiguration.getMarshaller())111 .from(messageHeaders.get(CitrusMailMessageHeaders.MAIL_FROM).toString())112 .to(messageHeaders.get(CitrusMailMessageHeaders.MAIL_TO).toString())113 .cc(messageHeaders.get(CitrusMailMessageHeaders.MAIL_CC).toString())114 .bcc(messageHeaders.get(CitrusMailMessageHeaders.MAIL_BCC).toString())115 .subject(messageHeaders.get(CitrusMailMessageHeaders.MAIL_SUBJECT).toString())116 .body(bodyPart);117 }118 /**119 * Reads basic message information such as sender, recipients and mail subject to message headers.120 * @param msg121 * @return122 */123 protected Map<String,Object> createMessageHeaders(MimeMailMessage msg) throws MessagingException, IOException {124 Map<String, Object> headers = new HashMap<>();125 headers.put(CitrusMailMessageHeaders.MAIL_MESSAGE_ID, msg.getMimeMessage().getMessageID());126 headers.put(CitrusMailMessageHeaders.MAIL_FROM, StringUtils.arrayToCommaDelimitedString(msg.getMimeMessage().getFrom()));127 headers.put(CitrusMailMessageHeaders.MAIL_TO, StringUtils.arrayToCommaDelimitedString((msg.getMimeMessage().getRecipients(javax.mail.Message.RecipientType.TO))));128 headers.put(CitrusMailMessageHeaders.MAIL_CC, StringUtils.arrayToCommaDelimitedString((msg.getMimeMessage().getRecipients(javax.mail.Message.RecipientType.CC))));129 headers.put(CitrusMailMessageHeaders.MAIL_BCC, StringUtils.arrayToCommaDelimitedString((msg.getMimeMessage().getRecipients(javax.mail.Message.RecipientType.BCC))));130 headers.put(CitrusMailMessageHeaders.MAIL_REPLY_TO, StringUtils.arrayToCommaDelimitedString((msg.getMimeMessage().getReplyTo())));131 headers.put(CitrusMailMessageHeaders.MAIL_DATE, msg.getMimeMessage().getSentDate() != null ? dateFormat.format(msg.getMimeMessage().getSentDate()) : null);132 headers.put(CitrusMailMessageHeaders.MAIL_SUBJECT, msg.getMimeMessage().getSubject());133 headers.put(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, parseContentType(msg.getMimeMessage().getContentType()));134 return headers;135 }136 /**137 * Process message part. Can be a text, binary or multipart instance.138 * @param part139 * @return140 * @throws java.io.IOException141 */142 protected BodyPart handlePart(MimePart part) throws IOException, MessagingException {143 String contentType = parseContentType(part.getContentType());144 if (part.isMimeType("multipart/*")) {145 return handleMultiPart((Multipart) part.getContent());146 } else if (part.isMimeType("text/*")) {147 return handleTextPart(part, contentType);148 } else if (part.isMimeType("image/*")) {149 return handleImageBinaryPart(part, contentType);150 } else if (part.isMimeType("application/*")) {151 return handleApplicationContentPart(part, contentType);152 } else {153 return handleBinaryPart(part, contentType);154 }155 }156 /**157 * Construct multipart body with first part being the body content and further parts being the attachments.158 * @param body159 * @return160 * @throws IOException161 */162 private BodyPart handleMultiPart(Multipart body) throws IOException, MessagingException {163 BodyPart bodyPart = null;164 for (int i = 0; i < body.getCount(); i++) {165 MimePart entity = (MimePart) body.getBodyPart(i);166 if (bodyPart == null) {167 bodyPart = handlePart(entity);168 } else {169 BodyPart attachment = handlePart(entity);170 bodyPart.addPart(new AttachmentPart(attachment.getContent(), parseContentType(attachment.getContentType()), entity.getFileName()));171 }172 }173 return bodyPart;174 }175 /**176 * Construct body part form special application data. Based on known application content types delegate to text,177 * image or binary body construction.178 * @param applicationData179 * @param contentType180 * @return181 * @throws IOException182 */183 protected BodyPart handleApplicationContentPart(MimePart applicationData, String contentType) throws IOException, MessagingException {184 if (applicationData.isMimeType("application/pdf")) {185 return handleImageBinaryPart(applicationData, contentType);186 } else if (applicationData.isMimeType("application/rtf")) {187 return handleImageBinaryPart(applicationData, contentType);188 } else if (applicationData.isMimeType("application/java")) {189 return handleTextPart(applicationData, contentType);190 } else if (applicationData.isMimeType("application/x-javascript")) {191 return handleTextPart(applicationData, contentType);192 } else if (applicationData.isMimeType("application/xhtml+xml")) {193 return handleTextPart(applicationData, contentType);194 } else if (applicationData.isMimeType("application/json")) {195 return handleTextPart(applicationData, contentType);196 } else if (applicationData.isMimeType("application/postscript")) {197 return handleTextPart(applicationData, contentType);198 } else {199 return handleBinaryPart(applicationData, contentType);200 }201 }202 /**203 * Construct base64 body part from image data.204 * @param image205 * @param contentType206 * @return207 * @throws IOException208 */209 protected BodyPart handleImageBinaryPart(MimePart image, String contentType) throws IOException, MessagingException {210 ByteArrayOutputStream bos = new ByteArrayOutputStream();211 FileCopyUtils.copy(image.getInputStream(), bos);212 String base64 = Base64.encodeBase64String(bos.toByteArray());213 return new BodyPart(base64, contentType);214 }215 /**216 * Construct simple body part from binary data just adding file name as content.217 * @param mediaPart218 * @param contentType219 * @return220 * @throws IOException221 */222 protected BodyPart handleBinaryPart(MimePart mediaPart, String contentType) throws IOException, MessagingException {223 String contentId = mediaPart.getContentID() != null ? "(" + mediaPart.getContentID() + ")" : "";224 return new BodyPart(mediaPart.getFileName() + contentId, contentType);225 }226 /**227 * Construct simple binary body part with base64 data.228 * @param textPart229 * @param contentType230 * @return231 * @throws IOException232 */233 protected BodyPart handleTextPart(MimePart textPart, String contentType) throws IOException, MessagingException {234 String content;235 if (textPart.getContent() instanceof String) {236 content = (String) textPart.getContent();237 } else if (textPart.getContent() instanceof InputStream) {238 content = FileUtils.readToString((InputStream) textPart.getContent(), Charset.forName(parseCharsetFromContentType(contentType)));239 } else {240 throw new CitrusRuntimeException("Cannot handle text content of type: " + textPart.getContent().getClass().toString());241 }242 return new BodyPart(stripMailBodyEnding(content), contentType);243 }244 /**245 * Removes SMTP mail body ending which is defined by single '.' character in separate line marking246 * the mail body end of file.247 * @param textBody248 * @return249 */250 private String stripMailBodyEnding(String textBody) throws IOException {251 BufferedReader reader = null;252 StringBuilder body = new StringBuilder();253 try {254 reader = new BufferedReader(new StringReader(textBody));...

Full Screen

Full Screen

Source:MailServer.java Github

copy

Full Screen

...144 * @param bodyPart145 * @param messageHeaders146 */147 private Message split(BodyPart bodyPart, Map<String, Object> messageHeaders) {148 MailMessage mailRequest = createMailMessage(messageHeaders, bodyPart.getContent(), bodyPart.getContentType());149 Stack<Message> responseStack = new Stack<>();150 if (bodyPart instanceof AttachmentPart) {151 fillStack(getEndpointAdapter().handleMessage(mailRequest152 .setHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())153 .setHeader(CitrusMailMessageHeaders.MAIL_FILENAME, ((AttachmentPart) bodyPart).getFileName())), responseStack);154 } else {155 fillStack(getEndpointAdapter().handleMessage(mailRequest156 .setHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())), responseStack);157 }158 if (bodyPart.hasAttachments()) {159 for (AttachmentPart attachmentPart : bodyPart.getAttachments().getAttachments()) {160 fillStack(split(attachmentPart, messageHeaders), responseStack);161 }162 }163 return responseStack.isEmpty() ? null : responseStack.pop();164 }165 private void fillStack(Message message, Stack<Message> responseStack) {166 if (message != null) {167 responseStack.push(message);168 }169 }170 /**...

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.mail.model.BodyPart;2import com.consol.citrus.mail.model.MimeMessage;3import com.consol.citrus.mail.model.Multipart;4import com.consol.citrus.mail.model.TextBodyPart;5import com.consol.citrus.mail.model.TextMultipart;6import com.consol.citrus.mail.model.TextMultipart;7import java.io.ByteArrayInputStream;8import java.io.IOException;9import java.nio.charset.StandardCharsets;10import java.util.ArrayList;11import java.util.List;12import javax.mail.MessagingException;13import javax.mail.internet.MimeMessage;14import javax.mail.internet.MimeMultipart;15import javax.mail.internet.MimePart;16import javax.mail.internet.MimeMultipart;17import javax.mail.internet.MimePart;18import org.springframework.mail.javamail.MimeMessageHelper;19import org.sprin

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import com.consol.citrus.mail.message.CitrusMailMessage;3import org.springframework.mail.javamail.MimeMessageHelper;4import org.springframework.mail.javamail.MimeMessagePreparator;5import javax.mail.MessagingException;6import javax.mail.internet.MimeMessage;7public class BodyPart {8 public static void main(String[] args) throws MessagingException {9 CitrusMailMessage message = new CitrusMailMessage();10 message.setMimeMessagePreparator(new MimeMessagePreparator() {11 public void prepare(MimeMessage mimeMessage) throws MessagingException {12 MimeMessageHelper message = new MimeMessageHelper(mimeMessage);13 message.setFrom("

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.mail.model.BodyPart;2import com.consol.citrus.mail.model.MimeMessage;3import com.consol.citrus.mail.model.MimeMultipart;4import org.apache.commons.io.IOUtils;5import org.springframework.core.io.ClassPathResource;6import org.springframework.core.io.Resource;7import java.io.IOException;8import java.io.InputStream;9import java.nio.charset.StandardCharsets;10public class 3 {11 public static void main(String[] args) throws IOException {12 Resource resource = new ClassPathResource("email.txt");13 InputStream inputStream = resource.getInputStream();14 String email = IOUtils.toString(inputStream, StandardCharsets.UTF_8);15 MimeMessage mimeMessage = MimeMessage.parse(email);16 MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getBodyPart().getContent();17 BodyPart bodyPart = mimeMultipart.getBodyPart(0);18 System.out.println(bodyPart.getContent());19 }20}

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.actions;2import com.consol.citrus.mail.model.BodyPart;3import com.consol.citrus.message.Message;4import com.consol.citrus.testng.AbstractTestNGUnitTest;5import com.icegreen.greenmail.util.GreenMail;6import com.icegreen.greenmail.util.GreenMailUtil;7import org.springframework.core.io.ClassPathResource;8import org.springframework.core.io.FileSystemResource;9import org.springframework.mail.javamail.MimeMessageHelper;10import org.springframework.mail.javamail.MimeMessagePreparator;11import org.testng.Assert;12import org.testng.annotations.Test;13import javax.mail.internet.MimeMessage;14import java.io.IOException;15public class ReceiveMailActionTest extends AbstractTestNGUnitTest {16 private GreenMail greenMail = new GreenMail();17 public void testReceiveMailAction() throws IOException {18 greenMail.start();19 MimeMessagePreparator preparator = new MimeMessagePreparator() {20 public void prepare(MimeMessage mimeMessage) throws Exception {21 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);22 helper.setFrom("

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import java.io.IOException;3import java.util.*;4import javax.mail.*;5import javax.mail.internet.*;6import javax.activation.*;7public class BodyPart {8 public static void main(String[] args) throws IOException {9 String msg = "This is a test message";10 MimeMessage message = new MimeMessage(null);11 InternetAddress addressFrom = new InternetAddress("

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public static void main(String[] args) {3 BodyPart bodyPart = new BodyPart();4 bodyPart.setContent("Hello World");5 System.out.println(bodyPart.getContent());6 }7}8public class 4 {9 public static void main(String[] args) {10 BodyPart bodyPart = new BodyPart();11 bodyPart.setContentType("text/plain");12 System.out.println(bodyPart.getContentType());13 }14}15public class 5 {16 public static void main(String[] args) {17 BodyPart bodyPart = new BodyPart();18 bodyPart.setDisposition("attachment");19 System.out.println(bodyPart.getDisposition());20 }21}22public class 6 {23 public static void main(String[] args) {24 BodyPart bodyPart = new BodyPart();25 bodyPart.setFileName("testFile.txt");26 System.out.println(bodyPart.getFileName());27 }28}29public class 7 {30 public static void main(String[] args) {31 BodyPart bodyPart = new BodyPart();32 bodyPart.setHeaders(Collections.singletonMap("key", "value"));33 System.out.println(bodyPart.getHeaders());34 }35}36public class 8 {37 public static void main(String[] args) {38 BodyPart bodyPart = new BodyPart();39 bodyPart.setPartNumber(1);40 System.out.println(bodyPart.getPartNumber());41 }42}43public class 9 {44 public static void main(String[] args) {45 BodyPart bodyPart = new BodyPart();46 bodyPart.setSize(1000);47 System.out.println(bodyPart.getSize());48 }49}

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import java.io.IOException;3import javax.mail.MessagingException;4public class BodyPart {5 private javax.mail.BodyPart bodyPart;6 public BodyPart(javax.mail.BodyPart bodyPart) {7 this.bodyPart = bodyPart;8 }9 public Object getContent() throws MessagingException, IOException {10 return bodyPart.getContent();11 }12}13package com.consol.citrus.mail.model;14import javax.mail.MessagingException;15import javax.mail.Multipart;16public class MimeMessage {17 private javax.mail.internet.MimeMessage mimeMessage;18 public MimeMessage(javax.mail.internet.MimeMessage mimeMessage) {19 this.mimeMessage = mimeMessage;20 }21 public BodyPart getBodyPart(int index) throws MessagingException {22 Multipart multipart = (Multipart) mimeMessage.getContent();23 javax.mail.BodyPart bodyPart = multipart.getBodyPart(index);24 return new BodyPart(bodyPart);25 }26}27package com.consol.citrus.mail.actions;28import java.io.IOException;29import javax.mail.MessagingException;30import com.consol.citrus.mail.model.MimeMessage;31public class ReceiveMailAction {32 private com.consol.citrus.mail.actions.ReceiveMailAction receiveMailAction;33 public ReceiveMailAction(com.consol.citrus.mail.actions.ReceiveMailAction receiveMailAction) {34 this.receiveMailAction = receiveMailAction;35 }36 public MimeMessage getMimeMessage() throws MessagingException, IOException {37 javax.mail.internet.MimeMessage mimeMessage = receiveMailAction.getMimeMessage();38 return new MimeMessage(mimeMessage);39 }40}41package com.consol.citrus.mail.actions;42import java.io.IOException;43import javax.mail.MessagingException;44import org.springframework.mail.javamail.JavaMailSender;45import org.springframework.mail.javamail.JavaMailSenderImpl;46import org.testng.Assert;47import org.testng.annotations.Test;48import com.consol.citrus.mail.actions.ReceiveMailAction;49import com.consol.citrus.mail.client.MailClient;50import com.consol.citrus.mail.model.MimeMessage;51import com.consol

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1BodyPart bodyPart = new BodyPart();2bodyPart.setContent("Hello, World!");3assertEquals(bodyPart.getContent(), "Hello, World!");4BodyPart bodyPart = new BodyPart();5bodyPart.setContentType("text/plain");6assertEquals(bodyPart.getContentType(), "text/plain");7BodyPart bodyPart = new BodyPart();8bodyPart.setDisposition("attachment");9assertEquals(bodyPart.getDisposition(), "attachment");10BodyPart bodyPart = new BodyPart();11bodyPart.setFileName("file.txt");12assertEquals(bodyPart.getFileName(), "file.txt");13BodyPart bodyPart = new BodyPart();14bodyPart.setHeaders(Collections.singletonMap("key", "value"));15assertEquals(bodyPart.getHeaders(), Collections.singletonMap("key", "value"));16BodyPart bodyPart = new BodyPart();17bodyPart.setMimePart(new MimeBodyPart());18assertEquals(bodyPart.getMimePart(), new MimeBodyPart());19BodyPart bodyPart = new BodyPart();20bodyPart.setMimePart(new MimeBodyPart());21assertEquals(bodyPart.getMimePart(), new MimeBodyPart());22BodyPart bodyPart = new BodyPart();23bodyPart.setSubParts(Collections.singletonList(new BodyPart()));24assertEquals(bodyPart.getSubParts(), Collections.singletonList(new BodyPart()));25BodyPart bodyPart = new BodyPart();26bodyPart.setCharset("UTF-8");27assertEquals(bodyPart.getCharset(), "UTF-8");28BodyPart bodyPart = new BodyPart();29bodyPart.setContent("Hello, World!");30assertEquals(bodyPart.getContent(), "Hello, World!");

Full Screen

Full Screen

getContent

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import java.io.IOException;3import javax.mail.BodyPart;4import org.springframework.core.io.Resource;5public class BodyPartUtil {6public static Resource getContent(BodyPart bodyPart) throws IOException, javax.mail.MessagingException {7return new MailBodyPartResource(bodyPart);8}9}10package com.consol.citrus.mail.model;11import java.io.IOException;12import java.io.InputStream;13import java.io.OutputStream;14import javax.mail.BodyPart;15import org.springframework.core.io.AbstractResource;16import org.springframework.core.io.Resource;17public class MailBodyPartResource extends AbstractResource {18private final BodyPart bodyPart;19public MailBodyPartResource(BodyPart bodyPart) {20this.bodyPart = bodyPart;21}22public String getDescription() {23return "Mail body part resource";24}25public InputStream getInputStream() throws IOException {26try {27return bodyPart.getInputStream();28} catch (javax.mail.MessagingException e) {29throw new IOException(e);30}31}32public OutputStream getOutputStream() throws IOException {33throw new UnsupportedOperationException("Mail body part resource is read only");34}35public boolean exists() {36return true;37}38public boolean isReadable() {39return true;40}41public boolean isOpen() {42return false;43}44public boolean isFile() {45return false;46}47public long contentLength() throws IOException {48try {49return bodyPart.getSize();50} catch (javax.mail.MessagingException e) {51throw new IOException(e);52}53}54public long lastModified() throws IOException {55return -1;56}57public Resource createRelative(String relativePath) throws IOException {58throw new UnsupportedOperationException("Cannot create relative path for mail body part resource");59}60}61package com.consol.citrus.mail.model;62import java.io.IOException;63import java.util.ArrayList;64import java.util.List;65import javax.mail.BodyPart;66import javax.mail.MessagingException;67import javax.mail.Multipart;68import javax.mail.internet.MimeMultipart;69import org.springframework.core.io.Resource;70public class MultipartUtil {71public static List<Resource> getContents(Multipart multipart) throws MessagingException, IOException {72List<Resource> bodyParts = new ArrayList<Resource>();73for (int i = 0; i < multipart.getCount(); i++) {74BodyPart bodyPart = multipart.getBodyPart(i);75if (bodyPart.getContent() instanceof Multipart) {76bodyParts.addAll(getContents((Multipart) bodyPart.getContent()));

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful