assertEquals("text/plain", myAttachment.getContentType()); assertEquals("my_data", FileCopyUtils.copyToString(new InputStreamReader(myAttachment.getInputStream())));
private void copySoapAttachments(final TestContext context, final org.springframework.ws.soap.SoapMessage soapRequest, final SoapMessage soapMessage) { for (final Attachment attachment : soapMessage.getAttachments()) { String contentId = context.replaceDynamicContentInString(attachment.getContentId()); if (!contentId.startsWith("<")) { contentId = "<" + contentId + ">"; } if (log.isDebugEnabled()) { log.debug(String.format("Adding attachment to SOAP message: '%s' ('%s')", contentId, attachment.getContentType())); } soapRequest.addAttachment(contentId, attachment::getInputStream, attachment.getContentType()); } }
/** * Static construction method from Spring mime attachment. * @param attachment * @return */ public static SoapAttachment from(Attachment attachment) { SoapAttachment soapAttachment = new SoapAttachment(); String contentId = attachment.getContentId(); if (contentId.startsWith("<") && contentId.endsWith(">")) { contentId = contentId.substring(1, contentId.length() - 1); } soapAttachment.setContentId(contentId); soapAttachment.setContentType(attachment.getContentType()); if (attachment.getContentType().startsWith("text")) { try { soapAttachment.setContent(FileUtils.readToString(attachment.getInputStream()).trim()); } catch (IOException e) { throw new CitrusRuntimeException("Failed to read SOAP attachment content", e); } } else { // Binary content soapAttachment.setDataHandler(attachment.getDataHandler()); } soapAttachment.setCharsetName(Citrus.CITRUS_FILE_ENCODING); return soapAttachment; }
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "addTodoEntryRequest") @ResponsePayload public AddTodoEntryResponse addTodoEntry(@RequestPayload AddTodoEntryRequest request, MessageContext messageContext) { TodoEntry entry = new TodoEntry(request.getTitle(), request.getDescription()); Iterator<org.springframework.ws.mime.Attachment> it = ((SoapMessage)messageContext.getRequest()).getAttachments(); if (it.hasNext()) { org.springframework.ws.mime.Attachment attachment = it.next(); Attachment todoAttachment = new Attachment(); String contentId = attachment.getContentId(); if (contentId.startsWith("<") && contentId.endsWith(">")) { contentId = contentId.substring(1, contentId.length() - 1); } todoAttachment.setCid(contentId); todoAttachment.setContentType(attachment.getContentType()); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); FileCopyUtils.copy(attachment.getInputStream(), out); todoAttachment.setData(Base64.getEncoder().encodeToString(out.toByteArray())); } catch (IOException e) { throw new RuntimeException("Failed to read attachment", e); } entry.setAttachment(todoAttachment); } todoListService.addEntry(entry); AddTodoEntryResponse response = new AddTodoEntryResponse(); response.setSuccess(true); return response; }