on 10-17-2017 12:07 PM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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/
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();
}
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.
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.
OutputAttachments outAttachments;
OutputAttachments outAttachments = output.getOutputAttachments();
Regards, Evgeniy.
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();
}
User | Count |
---|---|
77 | |
9 | |
9 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.