Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating an API to Encrypt Payload and Archive It Using Mule 4

DZone's Guide to

Creating an API to Encrypt Payload and Archive It Using Mule 4

This tutorial explains how to create an API to encrypt payloa and archive it using Mule 4.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Hi Mulies! Hope all are doing great! In this tutorial, we are going to create an API to encrypt the payload then archive it and send an email notification to the particular vendor in Mule4.

I just want to discuss a few changes in Mule4 compared to Mule 3.

  • Simplified Event and Message Model
  • No MEL completely Dataweave.(Version2.0) 
  • Streaming Management
  • Non-blocking, self-tuning runtime
  • Simplified Connectors and Modules Experience
  • Simplified Error Handling and New Try Scope

As we all know how to write a RAML code, I directly pasted my RAML logic here, which I developed.

Go to Anypointplatform and create RAML for the services as below.

This is my RAML code to develop an API:

#%RAML 1.0
version: v1
title: p-Encrypt-Archive-Vendordata-api
/pgpEncryption/{Username}/{Password}:
  description: "Encrypting the vendor details and Archive the details and notifies through email"
  post:
    body: 
      application/xml:
        example: |
          <vendordetails>
            <VendorID>V-0001</VendorID>
            <VendorName>Wood Inc.</VendorName>
            <VendorEmail>pakalavenkatreddy@wood.com</VendorEmail>
            <VendorType>Local</VendorType>
            <VendorPhnNo>9640531543</VendorPhnNo>
          </vendordetails>
    responses: 
      200:
        body: 
          application/json:
            example:
              {"Message":"Vendor Details has Encrypted Successfully"}

Lets test this code.

On the mocking service, which is on the right top corner, test the code, which gives a success 200OK response if the code does not have any errors, like below:Image title

As we all know, we can import our RAML code to Anypoint Studio in 2 ways. 1. Export over there and choose or 2. You can choose while creating a project from Design Center.

Then you can see the RAML logic will be created as flow in the studio.

Below is the logic, which I have developed to encrypt and archive the payload and to notify the vendor through mail with the data.

Image title

Here is the logic code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp" xmlns:crypto="http://www.mulesoft.org/schema/mule/crypto" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.mulesoft.org/schema/mule/crypto http://www.mulesoft.org/schema/mule/crypto/current/mule-crypto.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
    <http:listener-config name="p-Encrypt-Archive-Vendordata-httpListenerConfig">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <apikit:config name="p-Encrypt-Archive-Vendordata-config" raml="p-Encrypt-Archive-Vendordata.raml" outboundHeadersMapName="outboundHeaders" httpStatusVarName="httpStatus" />
    <crypto:pgp-config name="Crypto_Pgp" doc:name="Crypto Pgp" doc:id="7c61838b-20a5-4c88-b5f8-b68f8321707e" publicKeyring="${public.key}" privateKeyring="${private.key}" >
<crypto:pgp-key-infos >
<crypto:pgp-asymmetric-key-info keyId="${key.id}" fingerprint="${fingerprint}" passphrase="${passphrase}" />
</crypto:pgp-key-infos>
</crypto:pgp-config>
<ftp:config name="FTP_Config" doc:name="FTP Config" doc:id="0e2f29f9-f73c-46a6-b5fd-ec5bdf09b08b" >
<ftp:connection workingDir="/Input" host="localhost" username="admin" password="admin" />
</ftp:config>
<configuration-properties doc:name="Configuration properties" doc:id="38734e98-7756-4eb2-a929-0ee6b8d9c582" file="Meetup.properties" />
<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="8e49d3e2-b66d-43b1-b893-2f084536de36" >
<email:smtp-connection host="${Host}" user="${user}" password="${pass}">
<email:properties />
</email:smtp-connection>
</email:smtp-config>
<flow name="p-Encrypt-Archive-Vendordata-main">
        <http:listener config-ref="p-Encrypt-Archive-Vendordata-httpListenerConfig" path="/api/*">
            <http:response statusCode="#[vars.httpStatus default 200]">
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:response>
            <http:error-response statusCode="#[vars.httpStatus default 500]">
                <http:body>#[payload]</http:body>
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:error-response>
        </http:listener>
        <apikit:router config-ref="p-Encrypt-Archive-Vendordata-config" />
        <error-handler>
            <on-error-propagate type="APIKIT:BAD_REQUEST">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Bad request"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">400</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
            <on-error-propagate type="APIKIT:NOT_FOUND">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">404</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
            <on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Method not allowed"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">405</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
            <on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not acceptable"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">406</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
            <on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unsupported media type"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">415</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
            <on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not Implemented"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">501</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
        </error-handler>
    </flow>
    <flow name="p-Encrypt-Archive-Vendordata-console">
        <http:listener config-ref="p-Encrypt-Archive-Vendordata-httpListenerConfig" path="/console/*">
            <http:response statusCode="#[vars.httpStatus default 200]">
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:response>
            <http:error-response statusCode="#[vars.httpStatus default 500]">
                <http:body>#[payload]</http:body>
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:error-response>
        </http:listener>
        <apikit:console config-ref="p-Encrypt-Archive-Vendordata-config" />
        <error-handler>
            <on-error-propagate type="APIKIT:NOT_FOUND">
                <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
                    <ee:message>
                        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
                    </ee:message>
                    <ee:variables>
                        <ee:set-variable variableName="httpStatus">404</ee:set-variable>
                    </ee:variables>
                </ee:transform>
            </on-error-propagate>
        </error-handler>
    </flow>
    <flow name="post:\pgpEncryption\(Username)\(Password):p-Encrypt-Archive-Vendordata-config">
<crypto:pgp-encrypt doc:name="Pgp encrypt" doc:id="2b22b6d5-a04d-4ae6-953d-374f0b66c7a6" config-ref="Crypto_Pgp" keyId="${key.id}" target="EncryptedData"/>
<logger level="INFO" doc:name="Logger" doc:id="9b1205ee-4dff-4470-9738-c86640edb285" message="#[vars.EncryptedData]"/>
<ftp:write doc:name="Write" doc:id="3a5da509-6d44-4df7-bf32-a9ba71ede1d5" config-ref="FTP_Config" path="/Input/EncryptedFile"/>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="d05eac8c-6187-4e13-993a-c76ef09658c0">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[{
VedorID: payload.vendordetails.VendorID,
VendorName: payload.vendordetails.VendorName,
VendorEmail: payload.vendordetails.VendorEmail,
VendorType: payload.vendordetails.VendorType,
VendorPhnNo: payload.vendordetails.VendorPhnNo
}]]]></ee:set-payload>
            </ee:message>
        </ee:transform>
<email:send doc:name="Send" doc:id="b96204fc-03d7-4b7d-9b14-7476cc47c9ca" config-ref="Email_SMTP" fromAddress="#[payload.FromMail]" subject="#[payload.Subject]">
<email:to-addresses>
<email:to-address value="#[payload.ToMail]" />
</email:to-addresses>
<email:cc-addresses>
<email:cc-address value="#[payload.Cc]" />
</email:cc-addresses>
<email:body contentType="text/plain">
<email:content><![CDATA[#[payload.Body]]]></email:content>
</email:body>
</email:send>
    </flow>
</mule>

Hope this helps!

Feel free to post your suggestions in the comments. 

Thank You!

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
mulesoft ,mule ,raml ,api ,mule 3.9 ,mule developer ,mule 4 ,mule esb ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}