Skip to Content
0

Idoc to Rest as file attachment

Oct 17, 2017 at 11:07 AM

103

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Evgeniy Kolmakov Oct 20, 2017 at 02:35 PM
0

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

Show 5 Share
10 |10000 characters needed characters left characters exceeded

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-scenarios/

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();
}

0

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

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.

0
OutputAttachments outAttachments;

OutputAttachments outAttachments = output.getOutputAttachments();

Regards, Evgeniy.

1

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();
}
0
Manoj K Oct 17, 2017 at 12:49 PM
1

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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

0

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
Evgeniy Kolmakov Oct 17, 2017 at 11:56 AM
0

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.

Share
10 |10000 characters needed characters left characters exceeded
Harry Saj Oct 20, 2017 at 01:14 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded