Skip to Content
Technical Articles

Using Advanto Rest Adapter with Attachment

INTRODUCTION:

Requirement was to send payment files to Rest service via attachment and calculating the MD5 of same file in order to process it successfully, In addition to it we need to send the files to different servers based on file name.

URL which was dynamically created contains the MD5 which is validated at target side if it matches than PI has a successful call, else in response PI gets appropriate error.

Used Java Mapping as we need to Read files and the same file need to send as attachment in the rest call.

To generate dynamic URL graphical mapping is used because as this are payment file we need to use fix mapping in order to follow compliance, and same has been extracted via variable substitution in receiver rest channel. If there is no restriction of compliance url can be generated in same mapping.

Below is the Java Code which is used:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.MessageDigest;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class RestAPI extends AbstractTransformation {
	
	/*Main method is used only for Testing purpose as PI compiler looks for Transform method if required it can be comment before deploying in PI*/

	private static final DynamicConfigurationKey KEY_FILENAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
	
	 public static void main(String agrs[]) throws StreamTransformationException 
	  {
            
			try
			{    
				
				  //FileInputStream inStream = new FileInputStream ("C:\\Users\\File1.txt"); //INPUT FILE (PAYLOAD) 
				  FileInputStream ins = new FileInputStream ("C:\\Users\\File-4.pdf");
				  FileOutputStream outStream = new FileOutputStream ("C:\\Users\\Output.xml");
                  RestAPI mapping = new RestAPI();
				  mapping.execute(ins,ins,outStream);          
			}
			catch (Exception e) {

				e.printStackTrace();
			}                  
	  }
      
	  public void execute(InputStream arg0,InputStream ins, OutputStream arg1) throws StreamTransformationException {
		try {
			
				DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
				Document document = documentBuilder.newDocument();
				String source = ""; String targetxml =""; String line =""; String inptfile="";
		  		BufferedReader br = new BufferedReader( new InputStreamReader(ins));
				BufferedReader buff = new BufferedReader( new InputStreamReader(ins));
				StringBuffer filecontent = new StringBuffer("");
								
			/*Below code to convert file data into MD5 */	

				byte[] buf = new byte[1024];	
				MessageDigest md = MessageDigest.getInstance("MD5");
				  int read;
				  do {
					read = arg0.read(buf);
					if (read > 0) {
					  md.update(buf, 0, read); 
					}
				  } while (read != -1);

				  byte[] b = md.digest();
				  String checksum = "";
				  for (int i = 0; i < b.length; i++) {
				checksum += Integer.toString((b[i]&0xff)+0x100, 16).substring(1);
				  }
				System.out.println("MD5 value is  "+ checksum);
				 br.close();
			
			/*Below code  create Namespace and payload for target message */
                document.setXmlVersion("1.0");/*****set the XML Version**********/

				Element Namespace= document.createElementNS("urn:XYZ.com:ABC:EFS", "ns0:EFSFeeds");
				document.appendChild(Namespace);
				Element Element = document.createElement("file");
				Namespace.appendChild(Element);
				Element Element1 = document.createElement("hash");
				Element.appendChild(Element1);
				Element1.setTextContent(checksum);
							
				
			/* Assigning the created target message to "TransformationOutput"*/
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource domSource = new DOMSource(document);
				StreamResult streamResult = new StreamResult(arg1);
				transformer.transform(domSource, streamResult);
				arg1.write(targetxml.getBytes());
				arg1.flush();
				arg1.close();
			  }
		catch (Exception e) {   
			e.printStackTrace();  
			} 
		}
		
	  /* The transform method which must be implemented and source file is created as attachment*/            
	  public void transform(TransformationInput in, TransformationOutput out) 
				  throws StreamTransformationException {        
		try {
				 execute(in.getInputPayload().getInputStream(),in.getInputPayload().getInputStream(),out.getOutputPayload().getOutputStream());
				 String fName = "file";
				 OutputAttachments outputAttachments = out.getOutputAttachments();
				byte[] b = new byte[in.getInputPayload().getInputStream().available()];
				in.getInputPayload().getInputStream().read(b);
				Attachment newAttachment = outputAttachments.create(fName,b);
				outputAttachments.setAttachment(newAttachment);
			} 
			catch (Exception e) {
                    e.printStackTrace();
			}
                     
	  }
}

Mapping, due to compliance issue need to create graphical mapping else url can be create in same mapping itself.

Rest channel configuration

Result:

Both request and response logs are generated and can be viewed in below screen print.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.