Posts

Dataweave Code to Validate All Key Values of an Object in JSON Array

Image
This document covers Dataweave code to validate all JSON key values in a JSON array. For this demonstration, we are using Mulesoft's Online Dataweave Playground - Link as an IDE. Let’s begin... Solution Preview Code Snippet %dw 2.0 output application/json import * from dw::Runtime import * from dw::core::Strings fun valRequiredAlphanum(value, key, len = 30) =  if (isEmpty(value)) fail(key ++ " is a required field; ")  else if (!(value matches /[A-Za-z0-9]+/)) fail(key ++ " can only contain alphanumeric values; ")  else "" fun valNum(value, key, len = 10) =  if (!(value matches /[0-9]+/)) fail(key ++ " can only contain number; ")  else if (!(sizeOf(value) <= len)) fail(key ++ " must be less than " ++ (len as String) ++ " digits; ")  else "" fun valList(value, key, list) =  if (isEmpty(list find value)) fail(key ++ " field has invalid selection; ")  else "" var validatedRecords = payload map...

Dataweave Code to Identify Duplicate Key Values in JSON Array

Image
This document covers Dataweave code to identify and flag duplicate field or key values in a JSON array. This code will treat the first copy as original and mark the remaining copies as duplicate. For this demonstration, we are using Mulesoft's Online Dataweave Playground - Link as an IDE. Let’s begin... Solution Preview Code Snippet %dw 2.0 output application/json fun getDistinctRecords(records, field) = flatten((records groupBy ((item, index) -> item[field])) pluck ((value, key, index) -> value) map ((item, index) -> item distinctBy ((item, index) -> item[field]))) var distincValues = getDistinctRecords(payload, "Username") var duplicates = payload -- distincValues --- (distincValues) ++ (duplicates map ((item, index) -> item ++ {"Duplicate Flag": "Yes"})) Brief Explanation The main logic for this problem revolves around the distinctBy function. But you are here because you al so found out about this function's limitation when dea...

Securing API with Salesforce as OAuth Provider on Anypoint Platform

Image
This document covers steps for securing an API with Salesforce as OAuth 2.0 Provider on Anypoint Platform. We'll create a Connected App on Salesforce account for this demo. Then on the Anypoint Platform we will setup Salesforce as a Client Provider of type - OpenID Connect Dynamic Client Registration. Next, we'll configure default Client provider in the environment where our API lives. After that, we will deploy our API to Cloudhub and then secure it using OpenId Connect access token enforcement Policy. Let’s begin... Pre-requisites Salesforce Account Mulesoft Anypoint Platform Account API ready for Cloudhub Deployment For this demo, we're using the following Non-MuleAPI - Coffee SampleAPI RAML File -  Link . You can import it in Design Center and then publish it to your Anypoint Exchange. We'll later deploy it as a Proxy Application as mentioned in the steps below. Do not deploy the API until Client Provider is registered on Anypoint Platform (Just follow the events as...

HTTP Redirection or Forwarding using Mule App

Image
This document covers a Mule Application code that is capable to taking some input parameters or payload and after some kind of processing, it can redirect or forward the flow to another URL. Such implementation is made possible with HTTP Listener and can be useful in a variety of use-cases. Let’s begin... Pre-requisites Postman Anypoint Studio Anypoint Studio Code Create a new Mule Application project in Anypoint Studio. Open the project’s main  *.xml  file and use the following XML code to setup a listener flow - <flow    name="http-RedirectFlow"    doc:id="0a946780-95b5-4dea-a0db-37efe5f3f36b"  >   <http:listener      doc:name="/request"      doc:id="77f08c1c-da16-4f7e-b591-385cfd036593"      config-ref="HTTP_Listener_config"      path="/request"   >     <http:response       statusCode='#["302"]'      >     ...

Run Flex Gateway in Linux VM for Non-Mule API

Image
This document covers steps to setup Anypoint Flex Gateway as a Service in a Linux virtual machine. The Linux distribution being used here is Ubuntu Server. It will be running inside a virtual machine using VMware Workstation Player in the local Windows environment. The API being used will be a Non-Mule API from SampleAPIs.com called Coffee . You'll find a RAML for it in pre-requisites section. We'll publish it to the Anypoint Exchange and then use our Flex Gateway as an API Gateway for it. We'll also briefly cover installation of Ubuntu Server in VMware Workstation Player. Let’s begin... Pre-requisites Installed VMware Workstation Player -  Link Ubuntu Server ISO (LTS Version Recommended) -  Link Installed OpenSSH Client in your Windows System - Link Mulesoft Anypoint Platform Account Coffee SampleAPI RAML File - Link Postman Setup Ubuntu Server in VMware Open VMware Workstation Player and click on Create a New Virtual Machine link on the right side. In the New Virtual Mac...