Skip to Content
Personal Insights
Author's profile photo Shubhravrat Deshpande

Java mapping to download pdf from URL in response and send it as attachment along with response payload.

Okay, So this is the first time I’m writing a blog post on SAP blogs.

For a long time, I was thinking of doing this and looking for the correct topic to start my blogging and then a few days ago I received a requirement where I had to download a pdf from response payload and send it as an attachment along with the response payload.

I was exploring options to do this when I decided to go with Java mapping. At this point, I knew that this is the correct topic where I could share my experience and insight with you.

Functional Requirement:

System A sends a request to retrieve PDF documents, via SAP PO using a SOAP request, In response System B provides download link instead of documents. SAP PO has to download these documents using URLs in the payload and send them as attachments back to System A.

Flow Diagram:


Sender Channel : SOAP / Best effort.

Receiver Chanel : SOAP


Request : Request is sent to retrieve download URL.

Response : Download URL is returned as a response.



No mapping is required for request.

Java mapping is added for response message.


Below is the screenshot of Request and Response tested from SOAP UI.


As you can see from the field sampleurltodownloadfile, we are retrieving download url to download pdf document.

Below is the Java Code that I have used to do this:



import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class CompleteClassCode extends AbstractTransformation {

	private static ArrayList<String> downloadURL = new ArrayList<String>();
	private String fileName = null;
	private String contentType = null;
	//private int contentLength = 0;

	private static final int BUFFER_SIZE = 1024;

	public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
		try {
			try {
				this.mapping(in, out);
			} catch (ParserConfigurationException | SAXException e) {
				// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block

	public void mapping(TransformationInput in, TransformationOutput out)
			throws IOException, ParserConfigurationException, SAXException {

		InputStream inputstream = in.getInputPayload().getInputStream();
		OutputStream outputstream = out.getOutputPayload().getOutputStream();

		OutputAttachments outputAttachments = out.getOutputAttachments();

		String xmlOutputPayload = null;

		// Below method domParser(in, out); is only used for reading the sampleurltodownloadfile
		// for the XML payload and put the value into the ArrayList downloadURL.
		domParser(in, out);

		// Method printOutPutStream(inputstream1) is used to write payload to the
		// output.
		xmlOutputPayload = printOutPutStream(inputstream);

		// Below statement will write the output payload

		for (int i = 0; i < downloadURL.size(); i++) {

			byte[] attachmentContent = new byte[BUFFER_SIZE];

			fileName = downloadURL.get(i).substring(downloadURL.get(i).lastIndexOf("/") + 1,

			// Method downloadFile(downloadURL.get(i)) --> does the connection and download
			// file and gives the output as a byte array
			attachmentContent = downloadFile(downloadURL.get(i));

			Attachment attachments = outputAttachments.create(fileName, contentType, attachmentContent);





	public void domParser(TransformationInput tiIn, TransformationOutput tiOut)
			throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse(tiIn.getInputPayload().getInputStream());


		Element root = document.getDocumentElement();

		NodeList nList = document.getElementsByTagName("item");

		for (int temp = 0; temp < nList.getLength(); temp++) {
			Node node = nList.item(temp);
			System.out.println(""); // Just a separator
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				// Print each employee's detail
				Element eElement = (Element) node;
				System.out.println("URL : " + eElement.getElementsByTagName("sampleurltodownloadfile").item(0).getTextContent());

				downloadURL.add(new String(eElement.getElementsByTagName("sampleurltodownloadfile").item(0).getTextContent()));


	private String printOutPutStream(InputStream inInput) throws IOException {
		String finaloutput = "";
		String xmlTag = "<?xml version="1.0" encoding="UTF-8"?>\r\n";
		String soapEnvelopeOpening = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\">\r\n" + "<SOAP-ENV:Body>\r\n";
		String soapEnvelopeClosing = "</SOAP-ENV:Body>\r\n </SOAP-ENV:Envelope>\r\n";

		StringBuffer sb = new StringBuffer();
		InputStreamReader isr = new InputStreamReader(inInput);
		Reader reader = new BufferedReader(isr);
		int ch;
		while ((ch = > -1) {
			sb.append((char) ch);
		String inData = sb.toString();

		finaloutput = xmlTag + soapEnvelopeOpening + inData + soapEnvelopeClosing;

		return finaloutput;

	private byte[] downloadFile(String urlInput) throws IOException {
		byte[] byteContent = new byte[BUFFER_SIZE];

		URL url = new URL(urlInput);

		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		int responseCode = httpConn.getResponseCode();

		System.out.println(responseCode == HttpURLConnection.HTTP_OK);

		if (responseCode == HttpURLConnection.HTTP_OK) {

			InputStream connInputStream = httpConn.getInputStream();

			contentType = httpConn.getContentType();
			//contentLength = httpConn.getContentLength();

			// Method downloadFile(url.openStream()) --> downloads the file using only URL,
			// it also returns the byte array.

			byteContent = downloadFile(url.openStream());


		} else {

			String failedReponse = "Download URL: " + urlInput + "/r/n/nNo file to download. Server replied HTTP code: "
					+ Integer.toString(responseCode);
			byteContent = failedReponse.getBytes();


		return byteContent;

	private byte[] downloadFile(InputStream connectionInput) throws IOException {

		BufferedInputStream bis = new BufferedInputStream(connectionInput);
		ByteArrayOutputStream os = new ByteArrayOutputStream();

		byte[] buffer = new byte[BUFFER_SIZE];
		int count = 0;
		while ((count =, 0, BUFFER_SIZE)) != -1) {

			os.write(buffer, 0, count);


		return os.toByteArray();

	public static void main(String[] args) {




By using the above code, you can download and stream other file formats as well by changing Content-Type.

I hope this helps 🙂


Reference Links:

1. How to add attachment using JAVA Mapping (SAP PO) :

2. SAP PI/PO – Java Mapping Create attachments – SFTP :

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Anupam Ghosh
      Anupam Ghosh

      Hi Shubhravrat,

      Good article. Hope you keep writing more.




      Author's profile photo Shubhravrat Deshpande
      Shubhravrat Deshpande
      Blog Post Author

      Hi Anupam,

      Thank you for your feedback!

      Best regards,