Skip to Content
Author's profile photo Former Member

How to add attachment using JAVA Mapping (SAP PO)

Hi,

I was having a requirement to pick a PDF file from a file server, send it as attachment and PDF file name as field to the target server.

As a solution I created a JAVA code and used it as JAVA mapping in PI. I hope this code will help PI consultants if they have similar kind of requirement.

/*

* Created on May 14, 2015

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package attach;

/**

* @author ashutosh upadhyay

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import java.io.InputStream;

import java.io.OutputStream;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.Attachment;

import com.sap.aii.mapping.api.DynamicConfigurationKey;

import com.sap.aii.mapping.api.OutputAttachments;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

import  com.sap.aii.mapping.api.DynamicConfiguration;

public class AddAttachment extends AbstractTransformation{

  private static final DynamicConfigurationKey KEY_FILENAME = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“,”FileName”);

  public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) {

  //System.out.println(“A”);

  // access dynamic configuration

  InputStream inputstream = transformationInput.getInputPayload().getInputStream();

  OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

  DynamicConfiguration conf = transformationInput.getDynamicConfiguration();

  String fileName = “”;

   fileName = conf.get(KEY_FILENAME);

  //System.out.println(“C”+fileName);

  // set file name

  conf.put(KEY_FILENAME, fileName);

  try {

  // a) Populate XML for XIPAYLOAD

  String fresult= “”;

  // creating the xml

  fresult=”<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;

  fresult = fresult.concat(“<ns0:MT_Payload xmlns:ns0=\”http://test\”>”);

  fresult = fresult.concat(“<FileName>”);

  fresult = fresult.concat(fileName);

  fresult = fresult.concat(“</FileName>”);

  fresult = fresult.concat(“</ns0:MT_Payload>”);

  outputstream.write(fresult.getBytes(“UTF-8”));

// Write attachment

  OutputAttachments outputAttachments = transformationOutput.getOutputAttachments();

  byte[] b = new byte[inputstream.available()];

  inputstream.read(b);

  Attachment newAttachment = outputAttachments.create(fileName,”application/pdf”, b);

  outputAttachments.setAttachment(newAttachment);

  }

  catch (Exception e) {

  getTrace().addDebugMessage(e.getMessage());

  }

  }

}

Output:

1.JPG

2.JPG

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      this post is really very helpful, thanks..

      Author's profile photo gurudatta d
      gurudatta d

      Nice Blog..

      I have a query.Do we need to create DT,MT and service interface for the above scenario.

      If YES could you please explain

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Guru,

      Yes we need to have those as we are going to populate the filename in a receiver structure, but make sure you have used SI as XI 3.0 Compatible otherwise it will process the non XML content in PO.

      Author's profile photo Artem Solohin
      Artem Solohin

      works like a charm