How To Use Jms Adapter in Sap Cpi To Resend Message

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 73

HOW TO USE JMS ADAPTER IN SAP CPI TO RESEND MESSAGE

Scenario

 We send file XML from POSTMAN to 3rd system, but because of any reason, file XML can not go to SFTP.
And we get complain that can not see file in SFTP. In this case we have to push data again. To resolve
this issue, we use JMS adapter.
 Kindly take short look diagram

In this case, message from sender will send to queue is call message queue. And from there, they will
send to SFTP. If any issue of FTP server, message will be hold and resend

Step 1 : Create integration flow for message queue.

For simple, this integration flow we will just include one sender (HTTPS) and one receiver (JMS)
Queue Name : Name of queue in CPI

When we use POSTMAN to run this integration flow We will receive message in Message Queues
Step 2 : Create integration flow consume message queue in step 1

In this step, we create new integration flow to consume message queue. We just add sender adapter is
JMS with name equal name of queue in step 1, and SFTP adapter for receiver. We simulate two case

 Case 1 : Cannot connect to SFTP. We will see that message still in queue. Queue try resend message
every short time
 Case 2 : Connect SFTP ok, so message send from POSTMAN to SFTP ok

This is integration flow which using JMS adapter for sender


Queue Name : Get name of message queue which config in step 1

Step 3 : Test cases

Case 1 : Message send to queue, connect to SFTP error. Message will hold in queue

 POSTMAN to queue

 Queue to SFTP -> Fail


Message hold in queue

Case 2 : Message consumed by queue


Hi guys, in this article I want to share one more tip in CPI, that is PGP keys. First, kindly take a look
diagram below

As you known in picture, all of thing which we need in scenario are

 Create key pair and save private key (A) by yourself and send public key to partner. Partner will encrypt
data by public key and send to your system (CPI). CPI will use private key to decrypt data.
 Save public key (B) of partner in your system and use it to encrypt data which sent to system partner (B)

In SAP CPI, we will use PGP keys, PGP ENCRYPTOR, PGP DECRYPTOR to do this. OK let’s begin.
I. Create private key and public key

To do this, we will use software KLEOPATRA. you can download it over internet.

Click New Key Pair


Export public key
Export PGP private key

OK, public key and private key will be used in encryption and decryption data.
II. PGP Encryption

Scenario – Data plain text will sent by HTTPS adapter. In IFLOW, data will be encrypt by use component
PGP ENCRYPTOR and send file XML to SFTP folder.

Step II – 1. Import PGP public key into PGP Keys

Step II-2. Design IFLOW with component PGP ENCRYPTOR


Step II-3. TEST by POSTMAN

III. PGP Decryption

Scenario – Receive data which encrypted before to IFLOW, use component PGP DECRYPTOR to decrypt
data and send to backend
Step III-1. Import private key into PGP Keys.

Step III-2. Design IFLOW with component PGP DECRYPTOR

Step III-3. TEST by POSTMAN

This testing, kindly try by your self. Thanks.

IV. How to install many public key/private key in CPI

In PGP Keys of CPI, just allow add only one public key, if we add one more another public key, It will be
overwrite. So, If we need add many public of many provider, how do we will do ?
The answer is very simple, we will add all public key into one file. And after that, add this file into CPI.
This is steps

 Add all public key into file

 Add this file into CPI


NOTE

With private key, we need make sure all key have to the same pass phrase. If not, we will receive error
when import into CPI.
[SAP CPI] – WORKING WITH JDBC ADAPTER RECEIVER ON SAP CPI AND SQL SERVER – INSERT STATEMENT
Hi guys, to be continue for series WORKING WITH JDBC ADAPTER RECEIVER ON SAP CPI AND SQL
SERVER, today I want to setup one scenario only about Insert statement, working with many rows many
table in SQL.

First, we will take a look about Insert Statement syntax in JDBC


For example, we design table in SQL server

And we want insert many rows into this table, example 3 rows, we have to create XML for Insert
statement as below

With another system, JDBC can run one time and they can insert all rows into table of database. But
with CPI-JDBC supports only one record at a time, check this link . You can use General Splitter after
mapping and insert all the records into DB.

In CPI, we have to use component General Splitter with XPATH :


//StatementName/dbTableName/access
Run simulation we will see that source message split to 3 messages

And after that, JDBC adapter will insert every message into database.

But, we also have another way, no need use Splitter Component. It is create structure XML with many
segment Statement. In above example we will create XML syntax as
With this syntax, data also insert into database as well.

Second, we will consider some scenario

Scenario 01 : Insert data(s) into single table

This scenario, we considered above.

Scenario 02 : Insert data(s) into multiple table. Example Master – Detail table.
In this scenario, we need focus to data which insert into database have to the same master and data. If
master cannot insert or detail cannot insert into database with any issue, all transaction will be
ROLLBACK. OK, let’s config it

Example we have two table in database which related together TBL_002 and TBL_002A. Key relationship
of two table is SAP_PO.

We need insert data from this XML structure into database through JDBC
Step 01 : Create XSD for source message
Step 02 : Create XSD for destination message (Insert aJDBC)
Step 03 : Create integration flow and mapping source message with destination message
Mapping for Header
After mapping is done, we test by run simulation
Add Filter component to get all Element Statement

After this step we will have payload as


Next we need wrap this payload into Root element by add Content Modifier
Because of we want consistency data so we need config BATCH mode in JDBC receiver adapter
Batch operation we have two mode

 Atomic : Considers each batch operation as a single unit. It updates the whole batch operation
successfully or reverts the entire operation to its initial state if anything in the batch operation fails.
 Non-Atomic : This is based on the behavior of the database. It updates all the successfully executed
records and throws an exception if anything fails. It does not revert the failed records to its initial state.

Kindly reference this link for more detail about JDBC adapter

Test case

Case 01 : Dupplicate at Item, no data insert database

Request

Response
Case 02 : Happy case

Request

Response
Check data in SQL

Case 03 : Error when config JDBC Data Source Wrong


This issue happen because we config JDBC invalid with Cloud Connector. We need check and config it
valid. Please focus to option SQL (Cloud) or SQL (On-premise). IF choose on-premise we should be input
location ID if we have many Cloud connector for one sub account CPI

[SAP CPI] – HOW TO FILTER FILE WHEN WORKING WITH SFTP


May 6, 2022 Huy NguyenDuc CPI - Integration Scenarios, CPI - Tips & Troubleshooting, SAP CPI One
comment
Hi guys, in this article I share one mini scenario when working with SFTP.

Scenario
Integration Flow want get all files with extension XML from folder SFTP into flow. Do some logic and
then will sent payload into queue.

Queue is JMS adapter.

All information of SFTP, ex: address, folder, credential… will be set by parameter.

This is integration flow of this scenario

#1. Configuration sender adapter

#2. Configuration SFTP sender adapter

In this configuration, we will use externalize parameter to config dynamic value for SFTP.
This is parameter table for this configuration
#3. Configuration Content Modifier

In this content modifier, we will create header with name CAMELFILENAME to get extension of file
which get from SFTP folder.

When working with File, we can get some information of FILE as

${file.name} Get full file with extension HCI\InputFile.xml

${file:name.ext} Get extension of file xml

${file:name.noext} Get file with no extension HCI\InputFile


${file:onlyname} Get file name InputFile.xml

${file:onlyname.noext} Get file name no extension InputFile

${file:ext} Get extension xml

#4. Configuration Router

Base on value of HEADER.CAMELFILENAME, we set condition in router to get just file with extension is
XML

#5. Configuration for queue. JMS receiver adapter

#6. Input value for parameters

After save, we need to input value for parameters which we define in integration flow.
Go to artifact, choose configure.

#7. Test from POSTMAN

In this step, we will do test script as

 First, send file with extension NOT equal XML. (Ex: ZIP file). In this case flow will run router default
(Route 2), and we will receiver ABC as payload
 Second, send file with extension EQUAL XML, in this case flow will run route 1, and we will receiver data
in JMS (data in JMS queue will be decrypt)

 Check in queue

[SAP CPI] – HOW TO CALL EXTERNAL


REST API WITH ACCESS TOKEN AND
CSRF TOKEN IN CPI
Hello guys, in this article I want to share one scenario which in there I used some my understand about CSRF-token, access
token, send header value from outside into caller http WEBSERVICE.
First, kindly take look scenarios

Scenario 01

We have one REST API which use access token to access. We want call this API, we will have do some action below:

 Step 1: Call to URL TOKEN to get access token. In this article, for example TOKEN will get from service key.
Endpoint : https://<Host>/oauth/token
 Step 2: Use access token in step 1 to call REST API. In this article endpoint : https://<host>/http/restapi

Configuration for Scenario 01

For simple in this article, I will use integration flow in CPI to create one REST API and call it from another integration.

Step 1 : Create integration flow with type is REST API


In this scenario, we do not use CSRF Protected.

Save and deploy this REST API

Test this API from POSTMAN, we need to check this API run OK.

As we see, this API need authorize by OAUTH2 with grant type is Client Credentials. So, we need call to token provider to
get access token.

All information of token provider will be get from service key with type Process Integration Runtime.
Step 2 : Create integration flow call REST API in Step 01

In this step, we will create new integration flow will call REST API in step 01. We also focus that, REST API use access
token to call, so in integration flow we need call to token provider to get access token and after that will use this access
token to call REST API. All this things, we will action on integration flow.

Step 2.1 : Create integration flow with sender adapter is HTTP


We will have two ways to call REST API which use OATHAU2 for credentials. First way, we will create OAUTH2 Client
Credential in CPI and use it to call direct to REST API. The second way, we will call manual to Token provider to get access
token, and after that use this access token to call REST API

Step 2.2: First way – Create OAUTH2 client Credential in CPI

 Go to Security Material in section Manage Security


 Create button -> OAuth2 Client Credentials
 (1) – (2) – (3) : Get from service key because this REST API created from SAP CPI for example. In fact, API owner will
provide these information.
 (4) : Send as Request Header for example in this article. In fact, base on configure of API Owner.

Next, We will use component Request Reply to call direct REST API with Authentication Type is OAuth2 Client Credentials
and input name of Oauth2 client credentials above in here
Step 2.2: Second way – Use access token to call REST API

The first, we try call API which provided by Token Provider by POSTMAN to understand how to

Token provider will provide information

 Token URL
 client_id : in this article, get it from service key. In fact, this value partner will provide
 client_secret : in this article get it from service key. In fact this value partner will provide
 Content-type : x-www-form-urlencoded
 response_type : token
 grant_type : client_credentials
OK, let configure this on SAP CPI integration flow

Step 2.2.1: Add content modifier and add header, body

 Add content modifier

 Add header
 grant_type : client_credentials
 response_type : token
 Content-Type : application/x-www-form-urlencoded
 client_id
 client_serect
 Next, because this REST API use content-type = x-www-form-urlendcoded. So we will create body for request with syntax :
Key1=Value1&Key2=Value2&Key3=Value3

Step 2.2.2: Add request reply call to REST API of token provider with http adapter to get access token

 Configure for HTTP


 (1) : Url of token provider
 (2) : Method of API token provider. In this case use POST
 (3) : Headers which we want to send from outside into to this process. In this case, It is Content-Type which defined in
before component ( Content Modifier)

Step 2.2.3: Add Content modifier to get access token payload response
In this time, Run this integration from POSTMAN we will receive access token in payload response.

Step 2.2.3: Call to REST API with access token in 2.2.2


Next, use value of access token in 2.2.2 to call REST API. Because REST API use authorization with type OAUTH2, so we
have to add one header with syntax :

 Key : Authorization
 Value : Bearer <access_token>

For example

OK, let configure on integration flow

 Add JSON to XML converter component to convert payload JSON to XML

 Add Content modifier to create string look like : Bearer <access_token>


 Add content modifier component and create new header with name : Authorization
 Add request reply component to call HTTP REST API and transfer header authorization into there
 (1) : Endpoint of REST API
 (2 : Method
 (3) : Header from before step

In this time, we call integration flow from POSTMAN we will receive data from REST API success.
OK, good ! To this time, we have already configured done scenario: How to call HTTP REST API with access token which
provide by Token provider with CLIENT_ID and CLIENT_SECRET. Next, we try to go deeper this scenario when REST API
use more CSRF-TOKEN

Step 2.3: (Extension) – Rest API use more CSRF-TOKEN

For example in this article, go to REST API which create in step 1. At HTTP adapter sender, check into check box CSRF
Protected
In this time, call this API we will receiver 403 HTTP status code Forbidden

And call from integration flow consume which created in step 2, we will receive 500 http status code
Check log in CPI, we also receiver status code 403

OK, let’s go to config

Step 2.2.1: Add more header with name x-csrf-token at component after get access token
Step 2.2.2: Add Request reply component and call to REST API with method GET and send 2 headers x-csrf-token
and Authorization into there

Step 2.2.3: Add Request reply component and call to REST API with method POSTand send 2 headers x-csrf-token
and Authorization into there
After this step, we have to add one more important config. If not config we still receiver 403 forbidden although send X-
CSRF-TOKEN into header.

Step 2.2.4: Switch HTTP Session Reuse to On Exchange

In this step, we have to switch HTTP Session Reuse from default to On Exchange. Click anywhere outside flow, choose tab
Runtime Configuration, change value of HTTP Session Reuse
OK, Now we test API from POSTMAN and receive 200 HTTP status and payload

Summary

In this article I shared step by step one scenario call external REST API with authorization by use Oauth2. Access token will
be gotten from Token provider by Client ID, Client Secret, Token URL. If external REST API also use more one token called
X-CSRF-TOKEN to protect API, We also can get more CSRF token and call REST API with access token. Thanks for your
reading and hope this article useful. If have any advise, kindly leave your comment on this. Thanks.

[SAP CPI] – WORKING WITH


SIMULATION IN SAP CPI
#1. Integration Simulation in SAP CPI

In first TIP, I talk about how to run integration flow in simulation mode. Example I have integration flow collect data from XML
and insert database by JDBC adapter
I want to run simulator to check my integration right or wrong. This is step by step I do.

 In mode read only, click on arrow from start to message mapping -> choose start point.

 Click on icon Start Simulation -> enter body of request. IF your request include Header or Properties, also add in here
 Click on arrow where you want to stop simulation -> Choose Stop endpoint.

 After set start point and end point. Go to menu choose start simulation
 Run simulation success we can click every envelop to view message data output.

#2 XML & XSD

Because of CPI mapping use format XML, XSD. Do we have to know how to parse XSD base on XML.

Go to here to convert XSD from XML


#3. Integration flow simulation with input ZIP file send to SFTP

 We have ZIP file which include many file in there and integration flow will split every file with file name, after that will send to
SFTP.
 Create integration flow

 Configure of ZIP Splitter


 Configure of Content Modifier, get file name of files from request

NOTES

${header.CamelFileNameOnly.replaceAll(“[0-9A-Za-z]+\/”,””)}

 Set start point from Sender to ZIP splitters


 End point after content modifier.
 Click on Start point. Choose ZIP file into body

 Run Simulation. After success, we will see one list file which split from zip file
HTTP Inbuilt Retry Option
The retry option in HTTP receiver adapter (with adapter version number 1.15) provides you an option to select retrying of failed
HTTP requests. This features comes handy for the backend HTTP server facing some challenges to serve the requests, especially
intermittent issues at backed.

By default, this option is not selected. Once you select this option, you will need to provide HTTP error response codes for which
you want the HTTP receiver adapter to trigger the retry.

Also, you can choose the number of iterations and the time between the each iterations. Sample screenshot provided below.
Retry option with checkbox control
SCPI: Message Mapping simulation and Limitations

Simulate is functionality to test the message mapping while designing the Integration flow in CPI.
Step 1: Create an iFlow in a package as below.

Step 2: Configure the IDOC sender channel as below. Provide the endpoint name which is unique.
Step 3: Now add the message mapping in the iFlow and click on the pointed symbol as below.

Step 4: Now import the XSD from the local folder as input and output schemas as below.
Map source and target structures with conditions or one to one.

Step 5: To simulate or test, click on the Simulate option in the mapping as below and then a new window pops up. Now click on Browse button and select the input
file for testing.
Step 6: Now click on the test option and check the output structure is creating. Once the file uploaded, we can change the edit the field data and test multiple times
if necessary.
Verify the mapping conditions are working as defined as below.

Step 7: We can also perform the Display Queue option in mapping for analyzing node by node as below.
Limitations:

1. Without uploading the sample XML, simulation is not possible.


2. You cannot copy the similar or same mapping for one target element to another.
3. GUI is a little problem as you have to scroll to the right for complex mappings.
4. Display queue option take more time to load compared with SAP PO.

You might also like