Skip to Content

pdf file from NFS to mail attachment

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Aug 19, 2016 at 09:43 AM

    Hello Swapna,

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

    Regards

    Anupam

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 19, 2016 at 09:49 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 21, 2016 at 03:36 PM

    Swapna,

    Check Scenario 3 in this blog , looks familiar to your requirement with slight modification Stop using Mail Package! © - Simplify your mail receiver adapter scenarios

    Br,

    Manoj

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2016 at 09:01 AM

    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: Reading Binary File Using ICO

    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-91c6d38c5816?QuickLink=index&…

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.