23
Page | 1 Handling Multiple Attachments using Java Mapping Version 1.0

Handling Multiple Attachments Using Java Mapping -SAP PI

Embed Size (px)

DESCRIPTION

This document describes the steps for creating the PI java mapping to handle multiple attachments and create multimapping structure combining each attachment. This also covers use of IOStream to download attachments to application server.

Citation preview

Handling Multiple Attachments using Java Mapping

Version 1.0

Document Information

Document NameHandling multiple attachments using Java mapping

Component DescriptionThis document describes the steps for creating the PI java mapping to handle multiple attachments and create multimapping structure combining each attachment. This also covers use of IOStream to download attachments to application server.

SAP Component UsedSAP PI 7.4 , SAP Netweaver Development Studio 7.3

Version HistoryVersionAuthorIssue PurposeDate

1.0Ankit SoniInitial Version24/06/14

Table of Contents1.General Description42.Dependencies52.1Dependent Tools52.2Jar Files Dependency53.User Guidelines63.1Implementation Steps63.1.1Create a new Java Project in NWDS63.1.2Include External Jars in Project so that Java class can be compiled93.1.3Create a Java Class113.1.4Enter the Code MultipleAttachment Class143.1.5Compile and create JAR file173.1.6Add the Java mapping in OM193.1.7Testing the Mapping203.2Troubleshooting & Error Handling234.Reference Code23

1. General Description

Invoices from external parties are sent as attachments in email to middleware system PI. PI is required to read all the attachments and download them to application server and then create an INVOIC.INVOIC02 IDOC from each XML file and send the path where the XML file was written to a field in IDOC.Currently with standard feature, we cannot handle multiple attachments using mail adapter also file receiver adapter does not have feature to write attachments. This requirement has been achieved by writing a java mapping which reads all attachment from email and generates the multi-mapping structure which is then used in second message mapping to generate IDOCs. Java APIs for PI have been used to access the attachments & set the name of the attachments.IOStream Java classes are used to download the attachments.

2. Dependencies2.1 Dependent Tools

Before using this technical component the following tools need to be installed and configured. SAP Netweaver Development Studio 7.3

2.2 Jar Files Dependency

In order to develop the module locally on the NW Developer Studio, some PI libraries will be required. These libraries must be included in the external libraries during development.

com.sap.xi.mapping.tool.lib_api.jar com.sap.xpi.ib.mapping.lib.jar com.sap.aii.af.ms.ifc_api.jar

3. User Guidelines3.1Implementation Steps

A Mail to IDOC scenario should be created first. PI would read mail from the Mail server and create multi mapping structure from the attachments. This document does not list down details for configuring the scenario; it concentrates more on the steps to build the logic for the java mapping.However it should be ensured that the keep attachments option is ticked in the mail sender adapter and also in the operational mapping. This would enable that mail attachments are read and sent as attachments through PI.

3.1.1Create a new Java Project in NWDS

In SAP NW Developer Studio, follow the below steps: Create a new project by using menu: File New Project Select Java Project. Click Next

Assign project name to Java Project. Java Project Name: MultipleAttachment. Click Next.

3.1.2Include External Jars in Project so that Java class can be compiled

Add External Jars required Go to Libraries tab. Click on Add External JARs. Select the jar files from directory which we extracted. Click Finish.

3.1.3Create a Java Class

Create a Java class extending AbstractTransformation class Right click on src and select New Class

Assign name to package and Class and select parent class as AbstractTransformation package name: com.sap.multipleattachment class name: MultipleAttachment Click on Browse for Superclass Select AbstractTransformation class from the list and click OK. Click Finish.

3.1.4Enter the Code MultipleAttachment Class

Double click MultipleAttachment.java The Java program will be displayed on the right hand panel. Following screenshot shows the code to be written in the java file. Save the file.

3.1.5Compile and create JAR file

Compile the code and create JAR file. Click on File Export Select JAR file and click Next. Select the files to be exported. Provide the name for JAR file and click Finish.

3.1.6Add the Java mapping in OM

Create an Imported archive and add java mapping as first step in OM. Below are the screenshot of OM and second step message mapping after multi-mapping structure is available after java mapping.

3.1.7Testing the Mapping

Testing Steps:Step1 A Mail containing three attachments is sent.

Step2 Mail and three attachments is received by PI System.

The screenshot below shows XML message in PI.

Step3 Java mapping reads all attachment and download it to application server and generates a multi-mapping structure.Below screenshot shows files for attachments created on target directory which was passed as parameter to Java mapping.

Step4 Java mapping creates a node for each XML in multi-mapping structure each of which creates one INVOIC.INVOIC02 IDOC

3.2Troubleshooting & Error Handling

Following is the list of possible error scenarios and steps to handle them.TypeError DescriptionTroubleshooting/Resolution Steps

ExceptionIOExceptionIf any exception occurs in the IO stream Code to download attachments, the exception raised in the mapping can be seen in the Message Log. These logs can be viewed for troubleshooting the issue.

ExceptionMapping ExceptionThe message log can viewed for any exception occurring in the mapping.

4. Reference Code

The code used in java mapping has been provided below as an attachment.

Page | 7

Page

Title

package com.sap.multipleattachment;

import com.sap.aii.mapping.api.AbstractTransformation;import com.sap.aii.mapping.api.StreamTransformationException;import com.sap.aii.mapping.api.TransformationInput;import com.sap.aii.mapping.api.TransformationOutput;import java.io.*; import java.util.*;import com.sap.aii.mapping.api.*;import com.sap.engine.interfaces.messaging.api.MessageDirection;import com.sap.engine.interfaces.messaging.api.MessageKey;import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;import com.sap.engine.interfaces.messaging.api.auditlog.*;import com.sap.engine.interfaces.messaging.api.exception.MessagingException;

public class MultipleAttachment extends AbstractTransformation {

String attachmentID = null; String AttachmentContent = null;AbstractTrace trace; String xmltag = "";String startString = " ";String endString = " ";String finalString = "";AuditAccess audit = null;/*Method to check if file is completely downloaded on application server*/private boolean isCompletelyWritten(File file) { RandomAccessFile stream = null; try { stream = new RandomAccessFile(file, "rw"); return true; } catch (Exception e) { getTrace().addInfo("Skipping file " + file.getName() + " for this iteration due it's not completely written"); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { getTrace().addDebugMessage("Exception during closing file " + file.getName()); } } } return false;}@Overridepublic void transform(TransformationInput arg0, TransformationOutput arg1)throws StreamTransformationException {

String msgID = arg0.getInputHeader().getMessageId(); // Convert message ID to UUID format (in compliance to RFC 4122). // UUID format is used by Advanced Adapter Engine for identifiers of processed messages. // For the sake of simplicity, conversion is done manually - alternatively, specific libraries can be used for this. final String DASH = "-"; String uuidTimeLow = msgID.substring(0, 8); String uuidTimeMid = msgID.substring(8, 12); String uuidTimeHighAndVersion = msgID.substring(12, 16); String uuidClockSeqAndReserved = msgID.substring(16, 18); String uuidClockSeqLow = msgID.substring(18, 20); String uuidNode = msgID.substring(20, 32);String msgUUID = uuidTimeLow + DASH + uuidTimeMid + DASH + uuidTimeHighAndVersion + DASH + uuidClockSeqAndReserved + uuidClockSeqLow + DASH + uuidNode; // Construct message key (com.sap.engine.interfaces.messaging.api.MessageKey) // for retrieved message ID and inbound message direction (com.sap.engine.interfaces.messaging.api.MessageDirection). MessageKey msgKey = new MessageKey(msgUUID, MessageDirection.INBOUND); AuditAccess msgAuditAccessor;try {msgAuditAccessor = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();msgAuditAccessor.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS, "JAVA Mapping Called");} catch (MessagingException e1) {e1.printStackTrace();} InputAttachments inputAttachments = arg0.getInputAttachments();finalString = xmltag.concat(startString);try { //checks for the availability of attachments in the input message if(inputAttachments.areAttachmentsAvailable()){//gets the attachmentIds and store it in an Object array Collection CollectionIDs = inputAttachments.getAllContentIds(true); Object[] arrayObj = CollectionIDs.toArray(); /*Reading parameter File Path of application server where files are to be downloaded from parameter given in Iflow configuration*/ String FilesPath = arg0.getInputParameters().getString("FilesPath"); getTrace().addInfo(String.valueOf(arrayObj.length)); if(arrayObj.length == 0) { throw new RuntimeException("No Attachments Available"); } for(int i =0;i