cancel
Showing results for 
Search instead for 
Did you mean: 

OutputAttachment Java mapping

vk_k3
Participant
0 Kudos

HI all,

I am using "Inputattachment" method from the Java  API com.sap.aii.mapping.api in the mapping in a UDF.

There are some methods here which are working fine however there are some methods which are not working.

String B64 = "";

String AttachmentID = "";

GlobalContainer globalContainer = container.getGlobalContainer();

InputAttachments IA = globalContainer.getInputAttachments();

OutputAttachments OA = globalContainer.getOutputAttachments();

OA = IA.copyInputAttachments();

B64 = IA.getBase64EncodedContent();

  

     Collection<String> CollectionIDs = IA.getAllContentIds(true);

     Object[] arrayObj = CollectionIDs.toArray();

     int attachmentCount = arrayObj.length;

     for(int i=0;i<attachmentCount;i++)

    {

            AttachmentID =(String) arrayObj[i];

            Attachment attachments =IA.getAttachment(AttachmentID);

     }

OA.setAttachment(attachments);           

if(IA.areAttachmentsAvailable())

{return "true";}

else

return "false";

The above two methods are not working though they are available in the documentation of help.sap.com or maybe I am not using them with proper syntax.

My requirement is to get the attachment into this UDF via Inputattachments and use Outputattachments to send it after Encoding  it into base64.

Regards,

Vkjoat

Accepted Solutions (0)

Answers (1)

Answers (1)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

> maybe I am not using them with proper syntax.

One general suggestion. You use for loop to get attachmentID and using getAttachment method you assign attachment object. What will happen in your loop count if the attachment not available for the last attachmentID and so no attachment object attached? Why are you overwriting attachment with new values each time?

vk_k3
Participant
0 Kudos

HI baskar,

This is just a rough code, I am mainly looking for the correct usage of the methods getBase64EncodedContent() and copyInputattachments and Outputattachments.

Regards,

Vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

From your input attachment check whether contents are available. If so, then retrieve content type, content of type byte and so.  Then using outputattachment class call create method and pass those values. Then finally attach that attachment to the outputattachment. Just a thought.

vk_k3
Participant
0 Kudos

HI baskar,

I appreciate your inputs !!

However,  the problem here is not logical!!

It is with the syntax.

So, Can you please put your steps in syntax ?

Regards,

Vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

This is some basic level code. You might have to customize for your needs.

GlobalContainer globalContainer = container.getGlobalContainer();
InputAttachments IA = globalContainer.getInputAttachments();
OutputAttachments OA = globalContainer.getOutputAttachments();

if(IA.areAttachmentsAvailable()) {

Collection<String> CollectionIDs = IA.getAllContentIds(true);
Object[] arrayObj = CollectionIDs.toArray();

if(arrayObj.length > 0){
 
    String attachmentID =(String) arrayObj[0];
    Attachment attachment =IA.getAttachment(attachmentID);

    String base64 =  attachment.getBase64EncodedContent();
    String contentType = attachment.getContentType();
    OA.create(attachmentID, base64.getBytes() , contentType); // if you dont want contentType then use first two arguments method.

}

}

vk_k3
Participant
0 Kudos

HI Baskar,

Awesome!!

The code compiled without an Error, i had just declared AttachmentID.

I will test it and I may have to change the statistics of the thread from helpful to correct. 🙂

The whole Problem is with RNIF Adapter where the Content Transfer Encoding of attachment is expected to be  Base64 by the trading partner.We are sending it as Base64via ABAP proxy, but the RNIF adapter overwrites the attachment with Content Transfer Encoding as Binary.

I have raised other discussions, we can continue discussion there if you have time.

Thanks a lot!!

Regards,

Vkjoat

vk_k3
Participant
0 Kudos

HI Baskar,

I have tested syntax,

I am receiving an attachment from ECC which is binary (I can see the PDF file content in sxmb_moni).

But, while executing the method "Attachment.getBase64EncodedContent()" I am getting the following exception.

  ... 49 more Caused by: java.lang.NullPointerException: while trying to invoke the method com.sap.aii.mapping.api.Attachment.getBase64EncodedContent() of an object loaded from local variable '<9>' at com.sap.xi.tf._MM_TEST_ATTACHMENT_.Attachment_Modification(_MM_TEST_ATTACHMENT_.java:436) ... 54 more</Trac

does the method requires any particular format of attachment or any binary will do ?

Regards,

Vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

  String attachmentID =(String) arrayObj[0];

  System.out.println(attachmentID);  //see whether you pass attachmentID or null

  Attachment attachment =IA.getAttachment(attachmentID); 

//If you pass invalid or empty attachmentID you will get null attachment object.  That's the issue. 

vk_k3
Participant
0 Kudos

HI Baskar,

I have fixed the problem.

it was just a Typo for the case.

however, The File is not Based64 Encoded.

In the Output I see that it is not Encoded properly.

Do you have any other suggestions to test the Encoding ?

Regards,

Vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>The File is not Based64 Encoded.  In the Output I see that it is not Encoded properly

Actually we try to do as per the API recommendation. Otherwise you pass as byte content and later you can use base64 logic to encode the file content. Search online for converting string to base64 encode logic. you will see plenty of them.

vk_k3
Participant
0 Kudos

Later in the sense in an Adapter Module ?

baskar_gopalakrishnan2
Active Contributor
0 Kudos

No. You can pass input attachment data as is. YOu might want to try base64 encoding logic instead of api method and see if that helps.

vk_k3
Participant
0 Kudos

The logic which you have suggested is API  or base64 encoding ?

if not Can you please suggest the base64 encoding too ?

Regards,

vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>The logic which you have suggested is API  or base64 encoding ?

base64 encoding. What I'm trying to tell is implement base64 encoding instead of that api method and see how that helps. Example coding as below... Call this method in output attachment data content.

http://www.wikihow.com/Encode-a-String-to-Base64-With-Java

former_member181985
Active Contributor
0 Kudos

You can use SAP standard API instead of custom BASE64 code. You need only few lines of code

import com.sap.aii.utilxi.base64.api.*;

byte[] attachmentBytes = attachment.getContent();

String base64String = Base64.encode(attachmentBytes);

OA.create(attachmentID, base64String .getBytes() );

vk_k3
Participant
0 Kudos

HI The import package "import com.sap.aii.utilxi.base64.api.*;" doesnt exist!!

The Code doesnt recognize the Base64 variable

Regards,

Vkjoat

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>The Code doesnt recognize the Base64 variable

That means you don't have required jar file in the java mapping. Please use it. Refer this link

http://wiki.sdn.sap.com/wiki/display/XI/Sending+Binary+Data+to+Inbound+Plain+HTTP+Adapter+in+XI+and+...

Looks like you need  aii_utilxi_misc.jar, aii_map_api.jar

Hope that helps.

vk_k3
Participant
0 Kudos

Hi Baskar,

Anyways, the initial syntax is given by you and that is a standard API itself which i have used already!! but the file is not encoded properly into base64 so I was trying something different to encode it properly to a base64 string.

Regards,

Vkjoat