
we might get bellowing result by consuming an API. A response with only PDF raw data. no xml, no soap, no json in the payload.
if we analyse the response raw data, it should be as below:
Message processData(Message message, def testFlag = null) {
def body = message.getBody(Reader);
//def body = message.getBody() as String;
def sourceRequest = body.getText();
def targetRequest = Mapping(sourceRequest,testFlag);
message.setHeader('Content-Type',CONTENT_TYPE);
message.setBody(targetRequest);
return message;
}
Message processData(Message message, def testFlag = null) {
def body = message.getBody(InputStream);
//def body = message.getBody() as String;
def pdfBytes = body.getBytes();
}
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.transform.Field
import groovy.xml.MarkupBuilder
import org.apache.camel.impl.DefaultAttachment
import javax.mail.util.ByteArrayDataSource
@Field String CONTENT_TYPE = 'text/xml;charset=UTF-8';
@Field String MIME_TYPE_PDF = 'application/pdf';
@Field String FILE_NAME = 'Attached File.pdf';
Message processData(Message message, def testFlag = null) {
// 1. Parse PDF Stream
def body = message.getBody(InputStream);
def pdfBytes = body.getBytes();
// 2. Attach PDF as attachment to the message
def dataSource = new ByteArrayDataSource(pdfBytes, MIME_TYPE_PDF);
def attachment = new DefaultAttachment(dataSource);
message.addAttachmentObject(FILE_NAME, attachment);
// 3. This is a normal mapping for response payload
def documentId = message.getProperties().get('DocumentID');
def targetRequest = Mapping(documentId, testFlag);
message.setHeader('Content-Type',CONTENT_TYPE);
message.setBody(targetRequest);
return message;
}
String Mapping(def order, def testFlag = null) {
def xmlWriter = new StringWriter();
def xmlMarkup = new MarkupBuilder(xmlWriter);
xmlMarkup
.'ns0:SalesOrderCreateResponse'('xmlns:ns0': 'urn:yourcompany.com:sapecc:lead2order') {
Order(order);
}
return xmlWriter.toString();
}
DATA:
lv_pdf TYPE xstring, " PDF content in binary data
lr_proxy TYPE REF TO zco_sales_order, " Synchronous outbound proxy
lr_attachments TYPE REF TO if_wsprotocol_attachments,
lt_attachments TYPE prx_attach.
FIELD-SYMBOLS:
<lr_attachement> TYPE REF TO if_ai_attachment.
CREATE OBJECT lr_proxy.
* The calling of outbound proxy has been omitted here
* It should be very similar as below
* lr_proxy->order_create_request(
* EXPORTING
* output = ls_request
* IMPORTING
* input = ls_response
* ).
lr_attachments ?= lr_proxy->get_protocol( if_wsprotocol=>attachments ).
lt_attachments = lr_attachments->get_attachments( ).
READ TABLE lt_attachments INDEX 1 ASSIGNING <lr_attachement>.
IF sy-subrc EQ 0.
lv_pdf = <lr_attachement>->get_binary_data( ).
ENDIF.
CALL FUNCTION 'ADS_CREATE_PDF_SPOOLJOB'
EXPORTING
printer = p_prt "Printer name supporting PDF device type
pages = 1
pdf_data = lv_pdf "XSTRING internal table
immediate_print = 'X'
EXCEPTIONS
no_data = 1
not_pdf = 2
wrong_devtype = 3
operation_failed = 4
cannot_write_file = 5
device_missing = 6
no_such_device = 7
OTHERS = 8.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
9 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 |