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.

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