cancel
Showing results for 
Search instead for 
Did you mean: 

Copy payload to an attachment

Former Member
0 Kudos

Hi,

I have a scenario:   3rd Party (XML message) --> HTTP Adapter  -->  PI 7.11 (SP7) --> Proxy  -->  ECC.

In PI there is a mapping.

Now we have a new requirement: the payload from the incoming message must be put in an attachment and attached to outgoing message and also sent to ECC.

I understand this can be done with JAVA-mapping as first mapping step in the Operation Mapping and then we do the original mapping.

Is this correct way of doing it?

Are there other (better?) ways to do it?

Can someone point out how such a JAVA mapping should look like? Or even better, give the source code for it?

Are there other things i need to consider for my new scenario?

Any help is much appreciated.

Kr

Robert

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Robert,


Below are instructions to save the payload in ECC using JAVA-mapping as the first mapping
step in the Operations Mapping and then continue with the original mapping.

this link provides step by step instructions for creating Java Mapping http://techplay.plozzle.com/?p=21.

Based on the instructions from the link above replace the Example Mapping program with the following code (which will save the payload on your app server in ECC):

// start

package copy_payload;


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


import  com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.InputHeader;


import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.io.FileNotFoundException;


public class FileCopy extends AbstractTransformation {


public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
{
// TODO Auto-generated method stub
getTrace().addInfo("Mapping to write payload to server");
String inData = convertStreamToString(arg0.getInputPayload().getInputStream());
//file name
String strArchiveFileName = "file_payload.xml";
 
//archive directory path - default path this is based on your system's path
String strArchiveDir = "/testdir/arc/inb/";
String systemID = System.getProperty("SAPSYSTEMNAME");
 
//SAP PI DEV System specific details - archive directory for development, qa, and prod -replace systemID values with your system id

if(systemID.equals("DPI"))
{
  strArchiveDir = "/testdir/arc/inb/";
}
//SAP PI QA System specific details
else if(systemID.equals("QPI"))
{
  strArchiveDir = "/qadir/arc/inb/";
}
//SAP PI PRD System specific details
else if(systemID.equals("PPI"))
{
  strArchiveDir = "/proddir/arc/inb/";
}


SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss-SSS");

  // Create absolute archive file path
String strArchiveFileNameAndTime = formatter.format(new Date()) + "_" + strArchiveFileName;

// Create the archive file
String strArchiveFilePath = strArchiveDir + strArchiveFileNameAndTime;


try
{
  File outFile = new File(strArchiveFilePath);
  FileOutputStream fos = new FileOutputStream(outFile);
  // Write data to the archive file
  fos.write(inData.getBytes());
  fos.close();
  getTrace().addInfo("Payload Archived to server");
}
catch(FileNotFoundException fofe)
{
  getTrace().addWarning("Exception - " + fofe.getMessage());
}
catch(IOException ioe)
{
  getTrace().addWarning("Exception - " + ioe.getMessage());
}


try
{
  arg1.getOutputPayload().getOutputStream().write(inData.getBytes("UTF-8"));
}
catch(IOException ioe)
{
  getTrace().addWarning("Exception - " + ioe.getMessage());
}
}


public String convertStreamToString(InputStream in)
{
StringBuffer sb = new StringBuffer();
try
{
  InputStreamReader isr = new InputStreamReader(in);
  Reader reader =
  new BufferedReader(isr);
  int ch;
  while((ch = in.read()) > -1) {
  sb.append((char)ch);}
  reader.close();
}
catch(IOException ioe)
{
  getTrace().addWarning("Exception - " + ioe.getMessage());
}
return sb.toString();
}
}

// end

Follow the instructions in the techplay link provided above and export the Java as a jar file and then import it in ESB in an Imported Archives component. As in the techplay example you will add the Imported Archive component in your Operation Mappings which in the link is the Interface Mappings.  In the Operation Mappings component you will execute the Java Mapping first and then your Message Mappings.

Regards,
Shari

former_member181985
Active Contributor
0 Kudos

It can be done within graphical mapping. Use ReturnXML from top most source node. Use "Return as XML" input to UDF code below and map UDF output to  target structure top most node. Ensure that you enable check box "Read Attachments" in operation mapping

UDF Code:

GlobalContainer globalContainer = container.getGlobalContainer(); 

OutputAttachments outputAttachments = globalContainer.getOutputAttachments(); 

try 

        Attachment newopAttachment = outputAttachments.create("attachmentName", var1.getBytes("UTF-8"));  

        outputAttachments.setAttachment(newopAttachment); 

catch (Exception e) 

            e.printStackTrace(); 

return   "1"; 

Former Member
0 Kudos

hi Praveen,

Do you think the above udf can be done in XSLT. Can you please help me with the same function of creating attachments but inside an XSLt mapping.

Regards,

ninu

raj_018
Member
0 Kudos

Hello Praveen,

How this UDF can be used in order to create an attachment of type (.pdf) in PI?

iaki_vila
Active Contributor
0 Kudos

Hi Hoedt,

You could create an UDF to do that issue

- For read an attachment: http://scn.sap.com/thread/1341089

- For write an attachment: http://scn.sap.com/thread/1918880

The api: https://help.sap.com/javadocs/pi/SP3/xpi/index.html

Regards.

Message was edited by: Iñaki Vila

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You can also try using soap (XI mode) receiver adapter for proxy receiver to handle attachment. Your PI version supports this.

You might also want to check this thread.

http://scn.sap.com/thread/1291872

See that helps.