Sterling Managed File Transfer

Sterling Managed File Transfer

Come for answers, stay for best practices. All we're missing is you.


#Sterling
 View Only

Custom REST API services with Sterling B2B Integrator

By Emiliano Pacelli posted Wed November 11, 2020 10:53 AM

  

As you know during the latest years, thanks to mobile devices and applications containerization, REST  (REpresentational State Transfer) APIs are becoming everyday more important in every solution, including MFT (Managed File Transfer) and B2B ones. Without entering into details of the architectural constraints and the benefits of implementing REST APIs, today I’m going to focus on their implementation to grow your Sterling B2B Integrator (B2Bi).

In fact, as you’re aware as of today B2Bi has an important set of REST APIs available to perform administration and monitoring activities (such as CRUD trading partners, certificates, SSH keys, etc. see knowledge center for details https://www.ibm.com/support/knowledgecenter/SS3JSW_6.1.0/developing/developing/filegateway/B2B_REST_APIs.html) but you can’t expose your custom REST API to, for example, return a file available in mailbox.

Even though there’s no default REST API Server, we can leverage the HTTP server adapter to create a REST API server with basic authentication. Let’s start.

What you need:

  • A business process
  • An HTTP Server adapter

Business process:

The key services to be used in the BP are:

  • XMLtoJSON transformation, to transform the JSON parameters in ProcessData XML values and vice versa
  • A service that performs some tasks such as Lightweight JDBC adapter to perform select/insert/update/delete queries;
  • Mime encode, to encode PrimaryDocument to be sent back via HTTP or HTTPS
  • HTTP Respond service, to send the JSON response back to the client

Below screenshot shows a test BP (you’ll find it here attached JSON_test.bp) you can use to test the REST service and start your own development

Business Process Structure

And below the BP XML source:

<process name="default">

  <rule name="POST">

    <condition>http-method/text()=&quot;POST&quot;</condition>

  </rule>

  <rule name="GET">

    <condition>http-method/text()=&quot;GET&quot;</condition>

  </rule>

  <sequence name="SS JSON REST">

    <choice name="CS Method">

      <select>

        <case ref="POST" activity="SS POST"/>

        <case ref="GET" activity="SS GET"/>

      </select>

      <sequence name="SS POST">

        <operation name="xmljsontransformer">

          <participant name="xmljsontransformer"/>

          <output message="xmljsontransformerInputMessage">

            <assign to="." from="*"></assign>

            <assign to="Input" from="PrimaryDocument/@SCIObjectID"></assign>

            <assign to="InputType">JSON</assign>

            <assign to="OutputPath">text.txt</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

        <operation name="Release Service">

          <participant name="ReleaseService"/>

          <output message="ReleaseServiceTypeInputMessage">

            <assign to="." from="*"></assign>

            <assign to="TARGET">PrimaryDocument</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

        <operation name="Text Document Appender">

          <participant name="TextDocAppender"/>

          <output message="DocAppendInputMessage">

            <assign to="." from="*"></assign>

            <assign to="appendString">{  &quot;userId&quot;: 1,  &quot;id&quot;: 1,  &quot;title&quot;: &quot;delectus aut autem&quot;,  &quot;completed&quot;: false }</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

      </sequence>

      <sequence name="SS GET">

        <operation name="Lightweight JDBC Adapter">

          <participant name="LightweightJDBCAdapterQuery"/>

          <output message="LightweightJDBCAdapterTypeInputMessage">

            <assign to="." from="*"></assign>

            <assign to="pool">db2Pool</assign>

            <assign to="query_type">SELECT</assign>

            <assign to="result_name">RESULT_USER</assign>

            <assign to="row_name">ROW_USER</assign>

            <assign to="sql">SELECT PWDLASTCHANGEDON FROM YFS_USER WHERE USERNAME=&apos;admin&apos;</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="DocToDOM(PrimaryDocument)"></assign>

          </input>

        </operation>

        <assign to="." from="DOMToDoc(/ProcessData/RESULT_USER/ROW_USER,&apos;PrimaryDocument&apos;,&apos;&apos;,&apos;&apos;)"></assign>

        <operation name="xmljsontransformer">

          <participant name="xmljsontransformer"/>

          <output message="xmljsontransformerInputMessage">

            <assign to="." from="*"></assign>

            <assign to="Input" from="PrimaryDocument/@SCIObjectID"></assign>

            <assign to="InputType">XML</assign>

            <assign to="OutputPath">text.txt</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

        <operation name="Release Service">

          <participant name="ReleaseService"/>

          <output message="ReleaseServiceTypeInputMessage">

            <assign to="." from="*"></assign>

            <assign to="TARGET">PrimaryDocument</assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

        <operation name="Text Document Appender">

          <participant name="TextDocAppender"/>

          <output message="DocAppendInputMessage">

            <assign to="." from="*"></assign>

            <assign to="appendString" from="Output/text()"></assign>

          </output>

          <input message="inmsg">

            <assign to="." from="*"></assign>

          </input>

        </operation>

      </sequence>

    </choice>

    <operation name="Assign Mime parms">

      <participant name="AssignService"/>

      <output message="out_message">

        <assign to="." from="*"></assign>

        <assign to="mimeDoc/mime:message/mime:header">&apos;111&apos;</assign>

        <assign to="Doc1" from="PrimaryDocument/@*"></assign>

        <assign to="mimeDoc/mime:message/mime:header/@name" append="true">&apos;Content-ID&apos;</assign>

      </output>

      <input message="inmsg">

        <assign to="." from="*"></assign>

      </input>

    </operation>

    <operation name="Assign Mime parms 2">

      <participant name="AssignService"/>

      <output message="out_message">

        <assign to="." from="*"></assign>

        <assign to="mimeDoc/mime:message/mime:header">&apos;text/json&apos;</assign>

        <assign to="mimeDoc/mime:message/mime:header/@name" append="true">&apos;Content-Type&apos;</assign>

      </output>

      <input message="inmsg">

        <assign to="." from="*"></assign>

      </input>

    </operation>

    <operation name="Assign Document ID for Doc1">

      <participant name="This"/>

      <output message="giveDocumentRequest">

        <assign to="." from="*"></assign>

        <assign to="documentKey" from="&apos;Doc1&apos;"></assign>

      </output>

      <input message="giveDocumentResponse">

        <assign to="Doc1/doc:document-id" from="documentId/text()" append="true"></assign>

      </input>

    </operation>

    <assign name="Assign" to="mimeDoc/mime:message/mime:body" from="Doc1/doc:document-id"></assign>

    <operation name="Release Service">

      <participant name="ReleaseService"/>

      <output message="ReleaseServiceTypeInputMessage">

        <assign to="." from="*"></assign>

        <assign to="TARGET">Doc1 | PrimaryDocument</assign>

      </output>

      <input message="inmsg">

        <assign to="." from="*"></assign>

      </input>

    </operation>

    <operation name="MIME encode documents">

      <participant name="MIME"/>

      <output message="packRequest">

        <assign to="mime-document" from="mimeDoc/mime:message"></assign>

      </output>

      <input message="packResponse">

        <assign to="." from="*"></assign>

      </input>

    </operation>

    <assign name="Assign" to="PrimaryDocument" from="mime-document/@SCIObjectID"></assign>

    <operation name="Release Service">

      <participant name="ReleaseService"/>

      <output message="ReleaseServiceTypeInputMessage">

        <assign to="." from="*"></assign>

        <assign to="TARGET">mime-document</assign>

      </output>

      <input message="inmsg">

        <assign to="." from="*"></assign>

      </input>

    </operation>

    <operation name="HTTP Respond Service">

      <participant name="HttpRespond"/>

      <output message="HttpRespondServiceInputMessage">

        <assign to="." from="*"></assign>

        <assign to="doc-has-headers">true</assign>

        <assign to="ErrorOutAfterConnectionClose">Enable</assign>

        <assign to="transport-instance-id" from="transport-instance-id/text()"></assign>

        <assign to="transport-session-id" from="transport-session-id/text()"></assign>

      </output>

      <input message="inmsg">

        <assign to="." from="*"></assign>

      </input>

    </operation>

  </sequence>

</process>

HTTP Server Adapter:

Http server adapter configuration is pretty simple. For test purposes, we’ll enable just user authentication, not SSL/TLS security. Remember to select a port not used by B2B Integrator or any other service available on the server.

HTTP Server adapter congiuration 1
HTTP Server adapter congiuration 2
HTTP Server adapter congiuration 3

Now we’ve reached the most important piece of the configuration. Type the uri (/user_check in this example but can be whatever URL you prefer) and select “Business Process”

HTTP Server adapter congiuration 4

Then choose, from the drop-down menu, the BP previously created and checked in Sterling B2B Integrator. Answer No to both the questions:

  • Send Raw messages: in this way, you receive just the JSON payload, not the headers
  • Run BP in sync mode: so that you can use the HTTP respond service you’ve put in your BP
HTTP Server adapter congiuration 5

Testing:

If you’re testing via cURL use a command such as:

curl -k --user admin:password -H "Content-Type: application/json" -H "Accept: application/json" --request GET "http://myserver:myport/user_check"

The result will be:

cURL - GET execution


If you’re using another tool such as postman you configure your service with basic authentication:

Postman execution - GET


To test the POST version you can use the following cURL command:

curl -d '{"fileName":"TestdataEmy.txt"}' -k --user admin:password -H "Content-Type: application/json" -H "Accept: application/json" --request POST "http://myserver:myport/user_check"

getting the following result:

cURL test - POST

Similarly in Postman:

Postman execution - POST

 


#IBMSterlingB2BIntegratorandIBMSterlingFileGatewayDevelopers
#DataExchange
0 comments
118 views

Permalink