How to generate XML attachment file to XML Payload?

Not applicable

Hi ,

I have SOAP endpoint which gives me back with SOAP body with attachment. I want to delete the payload and substitute with attachment through SOAP envelop.

Can you please suggest me,how to achieve this? what are policies we need to use?

Thanks,Venu

0 19 3,039
19 REPLIES 19

Former Community Member
Not applicable

Looks like you need to create a Pass-Through SOAP Proxy as a starting point. You can then leverage transformation policies eg: Assign Message Policy to manipulate the response you expect. Do you have a sample WSDL that you can share?

Not applicable

@Prithpal Bhogill

Thanks for your quick response,right now my target endpoint server is down. I will share you the wsdl file as soon as the server up. Can you please help mean time ,if the response is from target server like below. How do we write Assign message policy.

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
xmlns:s='http://www.w3.org/2001/XMLSchema'>
<SOAP-ENV:Body>
  <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
  <DownloadResponse xmlns="http://www.filetransfer.org">
     <DownloadResult><Filename>sample.pdf</Filename>
     <IsBinary>true</IsBinary>
     <BinaryContents>
        [very long binary content not shown here]
     </BinaryContents></attachment></Upload>  
</SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

Former Community Member
Not applicable

Your XML is invalid, can you post the right XML?

In any case, the response from the target server will usually be available in the {response.content} variable. In the assign message policy you can reference the {response.content} variable & transform it & assign it to the response variable. The transformation can be done using a combination of Extract Variable & Assign Message policies or you can also use a Javascript policy for that.

Not applicable

@Prithpal Bhogill

Once again Thanks for help me here. Please find the attached response from target . I am done the assign message policy but bit confusion how to extract the policy.

Could you please help me out

@Prithpal Bhogill

I have tried below Extract policy,but i could not able to extract.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="true" enabled="true" name="ExtractWayBillAttachment"> <DisplayName>ExtractWayBillAttachment</DisplayName> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <Source>response.content</Source> <XMLPayload stopPayloadProcessing="false"> <Namespaces> <Namespace prefix="soapenv">http://www.w3.org/2003/05/soap-envelope/</Namespace> <Namespace prefix="xsd">http://www.w3.org/2001/XMLSchema</Namespace> <Namespace prefix="xsi">http://www.w3.org/2001/XMLSchema-instance</Namespace> <Namespace prefix="mesa">http://www.sterlingcommerce.com/mesa</Namespace> </Namespaces> <Variable name="service"> <XPath>/soapenv:Envelope/soapenv:Body/mesa:processData/mesa:ProcessData:service</XPath> </Variable> </XMLPayload> </ExtractVariables>

Thanks,

Venu

Former Community Member
Not applicable

Hi @Venu.Pujari1 your Extract Variables policy is a little messed up. Based on the sample SOAP message you provided, it should look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Source clearPayload="false">response</Source>
    <XMLPayload stopPayloadProcessing="false">
        <Namespaces>
            <Namespace prefix="soapenv">http://schemas.xmlsoap.org/soap/envelope/</Namespace>
            <Namespace prefix="xsd">http://www.w3.org/2001/XMLSchema</Namespace>
            <Namespace prefix="xsi">http://www.w3.org/2001/XMLSchema-instance</Namespace>
            <Namespace prefix="mesa">http://www.sterlingcommerce.com/mesa</Namespace>
        </Namespaces>
        <Variable name="service">
            <XPath>/soapenv:Envelope/soapenv:Body/mesa:processData/ProcessData/service</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

I have attached a sample proxy for you that you can import and try it out. It is a simple no target proxy with 2 policies. An Assign Message that sets the response variable to your SOAP message & then an Extract variable that extracts the service element from the response. (See attached trace session image below)

1241-screen-shot-2015-09-23-at-114709-pm.png

Hope this helps.

@Prithpal Bhogill,

Thanks Prithapal its working fine!.

Attachment data means do we need to get the below XPATH elements from below data.Is that only attachment data?

<SOAPOutboundAttachments> <SOAPAttachment1 Content-ID="cid:attachment=-1866659783_1443101234428@sterlingcommerce.com" SCIObjectID="89633514fff837aebnode1"/> </SOAPOutboundAttachments> <attachmentCID>cid:attachment=-1866659783_1443101234428@sterlingcommerce.com</attachmentCID> <INSERT_SECURITY_HEADER>false</INSERT_SECURITY_HEADER> </ProcessData> </mesa:processData> <mesa:attachment href="cid:attachment=-1866659783_1443101234428@sterlingcommerce.com" xmlns:mesa="http://www.sterlingcommerce.com/mesa"/>

Thanks,

Venu

Not applicable
@Prithpal Bhogill

Thanks Prithapal its working fine!.

Attachment data means do we need to get the XPATH elements from below data.Is that only attachment data? Please find the attachment.

Thanks,

Venu

Former Community Member
Not applicable

To process attachments you will have to use a Java Callout policy. Extract Message policy only handles XML or JSON payloads.

@Prithpal Bhogill,

Can you please help me how to process the soap attachment and convert into soap substitute. i have done javacallout policy. but it not working.

I have shared the complete soap response file for your ref.

Thanks,

Venu

Not applicable

@Prithpal Bhogill

As per you suggestion,i am trying to use the java callout policy,but there was some issue in java callout policy. can you please let me know,how to process the soap attachment data by using java callout policy. I have attached the proxy bundle as well as java calllout policy.

Thanks,

Venu

@Prithpal Bhogill,

Could you please help me ,how to process the SOAP attachment through Java callout policy.

I am attaching here complete soap response from my target server.Hope it will help you to write java callout policy. Thanks for your timely response.

Thanks,

Venu

Not applicable

@Prithpal Bhogill,

Could you please help me ,how to process the SOAP attachment through Java callout policy.

I am attaching here complete soap response from my target server.Hope it will help you to write java callout policy. Thanks for your timely response.

Thanks,

Ven

@Prithpal Bhogill

I am using below java code to create java callout policy ,i am getting error when i attached this policy execute the flow. Can you please help me.

Thanks,

Venu

package com.apigee.singcust; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringReader; import java.nio.charset.Charset; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.soap.AttachmentPart; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConstants; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import org.w3c.dom.Document; import org.xml.sax.InputSource; import com.apigee.flow.execution.ExecutionContext; import com.apigee.flow.execution.ExecutionResult; import com.apigee.flow.execution.spi.Execution; import com.apigee.flow.message.FlowContext; import com.apigee.flow.message.MessageContext; public class ConvertAttachmentToBody implements Execution { public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) { try { String xml = messageContext.getVariable("myResponse"); messageContext.setVariable("soapAttachString", getAttachment(xml)); //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()); } } public String getAttachment(String xmlInput) throws SOAPException, IOException { MessageFactory factory = MessageFactory .newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL); SOAPMessage message = factory.createMessage( new MimeHeaders(), new ByteArrayInputStream(xmlInput.getBytes(Charset .forName("UTF-8")))); //SOAPPart soapPart = message.getSOAPPart(); Object content=null; java.util.Iterator iterator = message.getAttachments(); 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(); } } return String.valueOf(content); } }

Former Community Member
Not applicable

@Venu.Pujari1 sorry got pulled into a few things. You may need to package some of the javax.xml.* & potentially other packages into the JAR file for your Java Callout. I may get some time to look at this over the weekend.

@Prithpal Bhogill

Thanks a lot for your time and please let me know ,if you need any information from my side.

Thanks,

Venu

Not applicable

@Prithpal Bhogill

I am attaching proxy bundle and java callout code,could please let me know,where i am doing mistakes and also i am not able to get the XPATH ,when i am using online tool,i am getting the values from xml documents,but while execution,not populating the values.

If you get some time today or tomorrow could you please suggest me.If you need wsdl i can provide you.

<Variable name="attachmentCID"> <XPath>/soapenv:Envelope/soapenv:Body/mesa:processData/ProcessData/attachmentCID</XPath> </Variable> <Variable name="attachment"> <XPath>/soapenv:Envelope/soapenv:Body/mesa:attachment</XPath> </Variable>

Thanks,

Venu Pujari

Former Community Member
Not applicable

Hey @Venu.Pujari1 the XML Payload you are setting in your first AssignMessage policy is below.

<mesa:attachment
                        xmlns:mesa="http://www.sterlingcommerce.com/mesa" href="cid:attachment=-2144654932_1443061953281@sterlingcommerce.com"/>

In your Extract Variables policy you are trying to extract it into the variable named "attachment":

<Variable name="attachment">
            <XPath>/soapenv:Envelope/soapenv:Body/mesa:attachment</XPath>
        </Variable>

The problem is there is no value in the element, just attributes. I believe that's why your javacallout is failing as well.

@Prithpal Bhogill,

Thanks a lot for your time.Can you please let me know,how to extract SOAP attachment by using java callout policiy. Since Edge gives us a String or Message object . How actually we do using callout policies. I am just copied the code which i have done. Please have look into that and let me know your thoughts.

package com.apigee.singcust;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
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 {
String myXML = messageContext.getVariable("myResponse");
//stringToDom(myXML,messageContext);
loadXMLFrom(myXML);
messageContext.setVariable("soapAttachment",  loadXMLFrom(myXML));
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());
}
}
/*public static Document stringToDom(String xmlSource) throws SAXException,
ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}*/
public static Document loadXMLFrom(String xml) throws TransformerException {
    Source source = new StreamSource(new StringReader(xml));
    DOMResult result = new DOMResult();
    TransformerFactory.newInstance().newTransformer().transform(source , result);
    return (Document) result.getNode();
} 
public static void stringToDom(String xmlSource, MessageContext messageContext) throws SAXException, ParserConfigurationException, IOException, TransformerException{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
    //doc.getElementsByTagName("Declaration");
    messageContext.setVariable("soapAttachment",  doc);
    //.getElementsByTagName("Declaration"));
}  
}

Thanks,

Venu