package com.apigee.singcust; import javax.xml.soap.AttachmentPart; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPMessage; import com.apigee.flow.execution.ExecutionContext; import com.apigee.flow.execution.ExecutionResult; import com.apigee.flow.execution.spi.Execution; import com.apigee.flow.message.MessageContext; public class ConvertAttachmentToBody implements Execution { public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) { try { MimeHeaders headers= new MimeHeaders(); headers.setHeader("content-type", "application/octet-stream"); SOAPMessage message =MessageFactory.newInstance().createMessage(headers, messageContext.getResponseMessage().getContentAsStream()); java.util.Iterator iterator = message.getAttachments(); Object content="myValue"; while (iterator.hasNext()) { AttachmentPart attachment = (AttachmentPart)iterator.next(); String id = attachment.getContentId(); String type = attachment.getContentType(); System.out.print("Attachment " + id + " has content type " + type); if (type.equals("application/octet-stream")) { content = attachment.getContent(); System.out.println("Attachment contains:\n" + content); } } messageContext.setVariable("soapAttachment", content); /*SOAPBody body = message.getSOAPBody(); body.addDocument(doc); message.saveChanges(); message.writeTo(System.out);*/ return ExecutionResult.SUCCESS; } catch (Exception ex) { ExecutionResult executionResult = new ExecutionResult(false, ExecutionResult.ABORT.getAction()); executionResult.setErrorResponse(ex.getMessage()); executionResult.addErrorResponseHeader("ExceptionClass", ex .getClass().getName()); return new ExecutionResult(false, ExecutionResult.ABORT.getAction()); } } }