cancel
Showing results for 
Search instead for 
Did you mean: 

Idoc to Rest as file attachment

0 Kudos

Hi Folks,

I have a requirement to Transform idoc into XML file & upload as rest attachment.

Can I use below approach:

1st ICO - IDOC to file

2nd ICO NO ESR objects dummy interface like pass through- File to Rest attachment using payload swap bean.

Is this possible or is there a better way to handle this requirement.

Accepted Solutions (1)

Accepted Solutions (1)

former_member190293
Active Contributor

Hi Harry!

OutputAttachments outAttachments;
Attachment attachment = outAttachments.create(attName, attContentType, attBytes); outAttachments.setAttachment(attachment);

You should get byte array for your main payload and put it to attachment.

It's worth noting that you should mark "Read attachments" checkbox in Operation mapping.

Regards, Evgeniy

0 Kudos

Thanks for your time & code.

I tried with the code & it gives me below string conversion error:

error: incompatible types: Attachment cannot be converted to String Attachment attachment = outAttachments.create(attachment, "application/xml", b);

I'm not really well-versed with Java mapping so I followed the blog to write below mapping - https://blogs.sap.com/2013/03/13/write-java-mapping-directly-in-esr/

https://blogs.sap.com/2016/03/10/stop-using-mail-package-simplify-your-mail-receiver-adapter-scenari...

Code I tried in ESR-> Dummy Message Mapping -> Functions-> Attributes & Method:

public void transform(TransformationInput input, TransformationOutput output) throws StreamTransformationException {
  InputStream inStream = input.getInputPayload().getInputStream();
  OutputStream outStream = output.getOutputPayload().getOutputStream();
  try {
//Write Attachment
   OutputAttachments outAttachments;
	// Transfer main payload content from input to output
	byte[] b = getInputStreamBytes(inStream);
   outStream.write(b);
   Attachment attachment = outAttachments.create(attachment, "application/xml", b);
   outAttachments.setAttachment(attachment);
  } catch (Exception e) {
   throw new StreamTransformationException("Exception: " + e.getMessage(), e);
  }
}
private byte[] getInputStreamBytes(InputStream inStream) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[8192];
  int read = 0;
  while ((read = inStream.read(buffer, 0, buffer.length)) != -1) {
   baos.write(buffer, 0, read);
  }
  baos.flush();
  return baos.toByteArray();
}

former_member190293
Active Contributor
0 Kudos

Hi Harry!

Attachment attachment = outAttachments.create(attachment, "application/xml", b);

First parameter is just the attachment's (file) name, for example "myfile.xml", second is attachment's content type and the third parameter is byte array representing attachment content.

Regards, Evgeniy.

0 Kudos

I changed it to be below code & gives me initialization error:

//Write Attachment
   OutputAttachments outAttachments;
   Attachment attachment = outAttachments.create("Shipment.xml", "multipart/form-data", b);
   outAttachments.setAttachment(attachment);

Error:

error: variable outAttachments might not have been initialized Attachment attachment = outAttachments.create("Shipment.xml", "multipart/form-data", b);

To transfer the payload I used below code

// Transfer main payload content from input to output
byte[] b = getInputStreamBytes(inStream);
   outStream.write(b);

Could you please guide me on what I'm missing.

former_member190293
Active Contributor
OutputAttachments outAttachments;

OutputAttachments outAttachments = output.getOutputAttachments();

Regards, Evgeniy.

0 Kudos

Thanks Evgeniy Kolmakov.

The code is generating attachment.

Thanks a lot for your help.

For any one who'd like to reuse the code for attachment:

public void transform(TransformationInput input, TransformationOutput output) throws StreamTransformationException {
  InputStream inStream = input.getInputPayload().getInputStream();
  OutputStream outStream = output.getOutputPayload().getOutputStream();
  try {
// Transfer main payload content from input to output
byte[] b = getInputStreamBytes(inStream);
   outStream.write(b);
//Write Attachment
OutputAttachments outAttachments = output.getOutputAttachments();
   Attachment attachment = outAttachments.create("Shipment.xml", "multipart/form-data", b);
   outAttachments.setAttachment(attachment);
  } catch (Exception e) {
   throw new StreamTransformationException("Exception: " + e.getMessage(), e);
  }
}
private byte[] getInputStreamBytes(InputStream inStream) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[8192];
  int read = 0;
  while ((read = inStream.read(buffer, 0, buffer.length)) != -1) {
   baos.write(buffer, 0, read);
  }
  baos.flush();
  return baos.toByteArray();
}

Answers (3)

Answers (3)

manoj_khavatkopp
Active Contributor

Hi Harry,

Rest is adapter is capable of handling attachment only after 7.5 SP05> . If you are on the lower level you need to the pass the attachment as base64/binary format in one of the filed and ask third party to decode it back again.

Br,

Manoj

0 Kudos

Thanks I'm on 7.5 SP7.

I do see option Support Attachment but it gives error while attaching.

Do I need to still need to encode the payload into base64 or is there an other approach to attach the entire payload as attachment

manoj_khavatkopp
Active Contributor
0 Kudos

Yes u can attach the entire payload as attachment via java mapping but additional to that you need to have some main payload content as well .

0 Kudos

Hi,

Payload swap does not move the main payload to attachment. Could you please help me with java mapping on how to create attachment.

Most of the blogs say how to read the attachments only, I'm looking for converting main payload to attachment. Any pointers is much appreciated.

Thanks

former_member190293
Active Contributor
0 Kudos

Hi Harry!

Why to use two ICOs for that? Since your sender IDoc adapter converts source document to IDoc-XML format you can send it directly to REST receiver. But I doubt the ability of PayloadSwapBean to move main payload to attachment. I guess you should use java mapping to move your main payload to attachments section.

Regards, Evgeniy.