cancel
Showing results for 
Search instead for 
Did you mean: 

pdf file from NFS to mail attachment

swapna_patha
Explorer
0 Kudos

Hi All, We have a requirement to pick the pdf file from AL11 and send it to mail as attachment with dynamic subject line(file name should be the subject). We are trying to achieve this using java mapping, PFB code: package com.pdf.testing; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Map; 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.StreamTransformationConstants; import com.sap.aii.mapping.api.StreamTransformationException; import com.sap.aii.mapping.api.TransformationInput; import com.sap.aii.mapping.api.TransformationOutput; public class JM_simple extends AbstractTransformation { public void transform (TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {  //String inputPayload_bytes = convertInputStreamToString(arg0.getInputPayload().getInputStream()); //String inputPayload = convertInputStreamToString(arg0.getInputPayload().getInputStream()); // Calls convertInputStreamToString function  Map mapParameters = (Map) arg0.getInputHeader().getAll();  String Directory =  arg0.getInputParameters().getString("Directory"); // Assigns value of 'Directory' - Parameterized mapping  String From =  arg0.getInputParameters().getString("From_id"); // Assigns value of 'From_id' - Parameterized mapping  String To1 =  arg0.getInputParameters().getString("To_id_1"); // Assigns value of 'To_id' - Parameterized mapping  String To2 =  arg0.getInputParameters().getString("To_id_2"); // Assigns value of 'To_id' - Parameterized mapping  String newSubject="";  /*<<>>*/  DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);  DynamicConfigurationKey  KEY_FILENAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");  String SourceFileName = conf.get(KEY_FILENAME); // Assigns file name to String SourceFileName newSubject= SourceFileName; /// Set the subject of the file String PreText = ""; PreText = PreText.concat("

").concat("\r\n");  PreText = PreText.concat("").concat(newSubject).concat("").concat(From).concat("").concat(To1).concat(To2).concat("multipart/mixed;boundary=AaZz--AaZz");  PreText = PreText.concat("\r\nContent-Type: text/plain; charset=UTF-8").concat("\r\n");  PreText = PreText.concat("Content-Disposition: inline").concat("\r\n").concat("\r\n");  PreText = PreText.concat(Directory+SourceFileName+" is attached.").concat("\r\n");  PreText = PreText.concat("--AaZz").concat("\r");  PreText = PreText.concat("Content-Type: application/pdf; name="+SourceFileName).concat("\r"); PreText = PreText.concat("Content-Transfer-Encoding: base64").concat("\r");  PreText = PreText.concat("Content-Disposition: attachment; filename="+SourceFileName).concat("\r\n"); /*------*/ // concat(inputPayload).concat("\r\n"); //reading the input payload in byte stream. ByteArrayInputStream inStream = (ByteArrayInputStream) arg0.getInputPayload().getInputStream(); InputStream inStream2 = arg0.getInputPayload().getInputStream(); String PostText = ""; PostText = PostText.concat("--AaZz--").concat("\r"); PostText = PostText .concat(""); /*---*/ try { byte a[]= PreText.getBytes(); byte b[]= PostText.getBytes(); //byte p[]= (byte) inStream.read(); byte[] array = new byte[inStream2.available()]; inStream2.read(array); ByteArrayOutputStream outputStream = new ByteArrayOutputStream( ); outputStream.write(a); outputStream.write(array); outputStream.write(b); /* * Output payload is returned using the TransformationOutput class            * arg1.getOutputPayload().getOutputStream( */  //arg1.getOutputPayload().getOutputStream().write(outputPayload.getBytes("UTF-8"));  arg1.getOutputPayload().getOutputStream().write(outputStream.toByteArray()); } catch (Exception exception1) {  getTrace().addWarning("Exception caught in Transform: " + exception1.toString());  }  }  /* <<< Reads input payload>>>*/  public String convertInputStreamToString(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 (Exception exception) {  getTrace().addWarning("Exception caught in convertInputStreamToString: "+ exception.toString());  }  return sb.toString();  } } we are getting the below error : MP: exception caught with cause com.sap.aii.af.sdk.xi.srt.BubbleException: Failed to call the endpoint  [null "null"]; nested exception caused by: com.sap.aii.af.sdk.xi.util.XMLScanException: Can't parse the document; nested exception caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence. Looks like the file is corrupted even before the mapping because the MainDocument which is the PDF payload is not opening as a PDF. Could any one please assist ASAP, this is urgent and critical requirement. Regards, Swapna.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hello Swapna,
seems like file adapter tries to read .pdf as XML payload, but it has to be processed as binary. Try to follow the steps as here:

Also, please try to configure the modules in receiver Mail channel as guided here: http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/6d967fbc-0a01-0010-4fb4-91c6d38c5...

manoj_khavatkopp
Active Contributor
0 Kudos

Swapna,

Check Scenario 3 in this blog , looks familiar to your requirement with slight modification

Br,

Manoj

swapna_patha
Explorer
0 Kudos

Hi Manoj,

We have file naming commission number.pdf file in AL11 paths, as per the blog configured the below in sender channel.

It is not picking any .pdf files from AL11 paths.

Have tried putting * in filename but its throwing error as ".mail" is not found.

Could you please assist on how to pick *.pdf files using additional file option.

Regards,

Swapna.

manoj_khavatkopp
Active Contributor
0 Kudos

As per your configuration 2 files should be present in the AL11 directory .mail and .pdf file where .mail file will come as main payload and .pdf file will come as attachment.

In your case i guess you need to pick only .pdf file if that is so then you need to mention .pdf in file name schema no need to put additional files.

br,

Manoj

swapna_patha
Explorer
0 Kudos

Hi All,


We have a requirement to pick the pdf file from AL11 and send it to mail as attachment with dynamic subject line(file name should be the subject).


We are trying to achieve this using java mapping, PFB code:


package com.pdf.testing;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.util.Map;

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.StreamTransformationConstants;

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

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

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

public class JM_simple extends AbstractTransformation {

       public void transform (TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException { 

             

             

              //String inputPayload_bytes = convertInputStreamToString(arg0.getInputPayload().getInputStream());

              //String inputPayload = convertInputStreamToString(arg0.getInputPayload().getInputStream()); // Calls convertInputStreamToString function 

              Map mapParameters = (Map) arg0.getInputHeader().getAll(); 

              String Directory = arg0.getInputParameters().getString("Directory"); // Assigns value of 'Directory' - Parameterized mapping 

              String From = arg0.getInputParameters().getString("From_id"); // Assigns value of 'From_id' - Parameterized mapping 

              String To1 =  arg0.getInputParameters().getString("To_id_1"); // Assigns value of 'To_id' - Parameterized mapping 

              String To2 = arg0.getInputParameters().getString("To_id_2"); // Assigns value of 'To_id' - Parameterized mapping 

              String newSubject=""

              /*<<<Dynamic File Name Configuration>>>*/ 

              DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION); 

              DynamicConfigurationKey  KEY_FILENAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName"); 

              String SourceFileName = conf.get(KEY_FILENAME); // Assigns file name to String SourceFileName

              newSubject= SourceFileName; /// Set the subject of the file

              String PreText = "";

              PreText = PreText.concat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").concat("\r\n"); 

              PreText = PreText.concat("<ns1:Mail xmlns:ns1=\"http://sap.com/xi/XI/Mail/30\"><Subject>").concat(newSubject).concat("</Subject><From>").concat(From).concat("</From><To>").concat(To1).concat(To2).concat("</To><Content_Type>multipart/mixed;boundary=AaZz</Content_Type><Content>--AaZz"); 

              PreText = PreText.concat("\r\nContent-Type: text/plain; charset=UTF-8").concat("\r\n"); 

              PreText = PreText.concat("Content-Disposition: inline").concat("\r\n").concat("\r\n"); 

              PreText = PreText.concat(Directory+SourceFileName+" is attached.").concat("\r\n"); 

              PreText = PreText.concat("--AaZz").concat("\r"); 

              PreText = PreText.concat("Content-Type: application/pdf; name="+SourceFileName).concat("\r");

              PreText = PreText.concat("Content-Transfer-Encoding: base64").concat("\r"); 

              PreText = PreText.concat("Content-Disposition: attachment; filename="+SourceFileName).concat("\r\n");

              /*------*/

             

                    

              // concat(inputPayload).concat("\r\n");

             

      

              //reading the input payload in byte stream.

              ByteArrayInputStream inStream = (ByteArrayInputStream) arg0.getInputPayload().getInputStream();

              InputStream inStream2 = arg0.getInputPayload().getInputStream();

      

             

             

              String PostText = "";

              PostText = PostText.concat("--AaZz--").concat("\r");

              PostText = PostText .concat("</Content></ns1:Mail>");

              /*---*/

              try {

                     byte a[]= PreText.getBytes();

                     byte b[]= PostText.getBytes();

                     //byte p[]= (byte) inStream.read();

                     byte[] array = new byte[inStream2.available()];

                     inStream2.read(array);

                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

                     outputStream.write(a);

                     outputStream.write(array);

                     outputStream.write(b);

                     /*

                      * Output payload is returned using the TransformationOutput class           

                      * arg1.getOutputPayload().getOutputStream(

                      */ 

                     //arg1.getOutputPayload().getOutputStream().write(outputPayload.getBytes("UTF-8")); 

                     arg1.getOutputPayload().getOutputStream().write(outputStream.toByteArray());

              } catch (Exception exception1) { 

                     getTrace().addWarning("Exception caught in Transform: " + exception1.toString()); 

              } 

       } 

      

       /* <<< Reads input payload>>>*/ 

       public String convertInputStreamToString(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 (Exception exception) { 

                     getTrace().addWarning("Exception caught in convertInputStreamToString: "+ exception.toString()); 

              } 

              return sb.toString(); 

       }

}


we are getting the below error :


MP: exception caught with cause com.sap.aii.af.sdk.xi.srt.BubbleException: Failed to call the endpoint  [null "null"]; nested exception caused by: com.sap.aii.af.sdk.xi.util.XMLScanException: Can't parse the document; nested exception caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence.


Looks like the file is corrupted even before the mapping because the MainDocument which is the PDF payload is not opening as a PDF. Could any one please assist ASAP, this is urgent and critical requirement.


Regards, Swapna.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Swapna,

                   If the pdf file is corrupted, sending it via email is of no use because the receiver cannot read the file. First can you request the sender to send proper pdf file.

secondly what is the vesrion of apache jar class file you are using for mapping ? what is the version of PI? Not all apache jar files will work for PI 7.4.

Thirdly if all of above issues are fixed, then follow this blog step by step to get proper email as output

Let me know if you face any issues while implementing this solution.

You are currently creating  mail package through java mapping which is obsolete now.

Please follow this blog instead of mail packaging .

Regards

Anupam

swapna_patha
Explorer
0 Kudos

Hi Anupam, 1. the PDF file is not corrupted. 2. jar files which we are using is 1.8 and sap version is 7.3. 3. I have seen the blog will try it out and get back to you. Regards, Swapna.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Swapna,

                  You do mot need any external jar files for this. Just create a binary mail attachment following the blog, that will suffice.

Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hello Swapna,

                  Please can you post the code in properly indented manner. This will help the forum understand the code.

Regards

Anupam