How to use getMailRequest method of com.consol.citrus.mail.message.MailMessageConverter class

Best Citrus code snippet using com.consol.citrus.mail.message.MailMessageConverter.getMailRequest

Source:MailMessageConverter.java Github

copy

Full Screen

...49 /** Mail delivery date format */50 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");51 @Override52 public MimeMailMessage convertOutbound(Message message, MailEndpointConfiguration endpointConfiguration, TestContext context) {53 MailRequest mailMessage = getMailRequest(message, endpointConfiguration);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));255 String line = reader.readLine();256 while (line != null && !line.equals(".")) {257 body.append(line);258 body.append(System.getProperty("line.separator"));259 line = reader.readLine();260 }261 } finally {262 if (reader != null) {263 try {264 reader.close();265 } catch (IOException e) {266 log.warn("Failed to close reader", e);267 }268 }269 }270 return body.toString().trim();271 }272 /**273 * Reads Citrus internal mail message model object from message payload. Either payload is actually a mail message object or274 * XML payload String is unmarshalled to mail message object.275 *276 * @param message277 * @param endpointConfiguration278 * @return279 */280 private MailRequest getMailRequest(Message message, MailEndpointConfiguration endpointConfiguration) {281 Object payload = message.getPayload();282 MailRequest mailRequest = null;283 if (payload != null) {284 if (payload instanceof MailRequest) {285 mailRequest = (MailRequest) payload;286 } else {287 mailRequest = (MailRequest) endpointConfiguration.getMarshaller()288 .unmarshal(message.getPayload(Source.class));289 }290 }291 if (mailRequest == null) {292 throw new CitrusRuntimeException("Unable to create proper mail message from payload: " + payload);293 }294 return mailRequest;...

Full Screen

Full Screen

getMailRequest

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;2import com.consol.citrus.mail.message.MailMessageConverter;3import org.springframework.mail.SimpleMailMessage;4import org.testng.annotations.Test;5public class MailMessageConverterTest extends JUnit4CitrusTestDesigner {6 public void getMailRequest() {7 MailMessageConverter mailMessageConverter = new MailMessageConverter();8 SimpleMailMessage simpleMailMessage = new SimpleMailMessage();9 simpleMailMessage.setSubject("Test");10 simpleMailMessage.setText("Hello World!");11 simpleMailMessage.setFrom("

Full Screen

Full Screen

getMailRequest

Using AI Code Generation

copy

Full Screen

1MailRequest request = getMailRequest(mailMessage);2MailMessage message = getMailMessage(request);3MailResponse response = getMailResponse(mailMessage);4MailMessage message = getMailMessage(response);5MailMessage message = getMailMessage(request);6MailRequest request = getMailRequest(mailMessage);7MailResponse response = getMailResponse(mailMessage);8MailMessage message = getMailMessage(response);9MailMessage message = getMailMessage(request);10MailRequest request = getMailRequest(mailMessage);11MailResponse response = getMailResponse(mailMessage);12MailMessage message = getMailMessage(response);

Full Screen

Full Screen

getMailRequest

Using AI Code Generation

copy

Full Screen

1MailMessageConverter mailMessageConverter = new MailMessageConverter();2MailRequest mailRequest = mailMessageConverter.getMailRequest(mailMessage);3MailMessage mailMessage = mailMessageConverter.getMailMessage(mailRequest);4MailMessageConverter mailMessageConverter = new MailMessageConverter();5MailResponse mailResponse = mailMessageConverter.getMailResponse(mailMessage);6MailMessage mailMessage = mailMessageConverter.getMailMessage(mailResponse);7MailMessageConverter mailMessageConverter = new MailMessageConverter();8MailMessage mailMessage = mailMessageConverter.getMailMessage(mailRequest);9MailRequest mailRequest = mailMessageConverter.getMailRequest(mailMessage);10MailMessageConverter mailMessageConverter = new MailMessageConverter();11MailMessage mailMessage = mailMessageConverter.getMailMessage(mailResponse);12MailResponse mailResponse = mailMessageConverter.getMailResponse(mailMessage);13MailMessageConverter mailMessageConverter = new MailMessageConverter();14MailRequest mailRequest = mailMessageConverter.getMailRequest(mailMessage);15MailResponse mailResponse = mailMessageConverter.getMailResponse(mailRequest);

Full Screen

Full Screen

getMailRequest

Using AI Code Generation

copy

Full Screen

1context.setVariable("mailRequest", getMailRequest(mailMessage));2context.setVariable("mailMessage", getMailMessage(mailRequest));3context.setVariable("mailMessage", getMailMessage(mailRequest));4context.setVariable("mailRequest", getMailRequest(mailMessage));5context.setVariable("mailMessage", getMailMessage(mailRequest));6context.setVariable("mailMessage", getMailMessage(mailRequest));7context.setVariable("mailRequest", getMailRequest(mailMessage));8context.setVariable("mailMessage", getMailMessage(mailRequest));9context.setVariable("mailMessage", getMailMessage(mailRequest));10context.setVariable("mailRequest", getMailRequest(mailMessage));11context.setVariable("mailMessage", getMailMessage(mailRequest));

Full Screen

Full Screen

getMailRequest

Using AI Code Generation

copy

Full Screen

1MailMessageConverter converter = new MailMessageConverter();2MailRequest request = converter.getMailRequest(message);3String subject = request.getSubject();4String content = request.getContent();5MailAttachment attachment = MailAttachment.builder()6 .content("This is the content of the attachment")7 .contentType("text/plain")8 .name("attachment.txt")9 .build();10MailMessage message = MailMessage.builder()11 .from("

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful