1 2 3 4 5 6 | < dependency > < groupid >org.mule.weave</ groupid > < artifactid >assertions</ artifactid > < version >1.0.2</ version > < scope >test</ scope > </ dependency > |
Gary Liu's Technical Knowledge Base
Monday, June 28, 2021
Anypoint Studio Error: The project is missing Munit lIbrary to run tests
Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-to-run-tests-Mule-4, it still would not fix the proble.
The only solution at the moment is to add the followiing dependency:
Tuesday, February 16, 2021
Access Multiple Github Accounts From One Computer
Step 1: Step 1: Generate SSH and Add it to ssh-agent
Note: if you have two github accounts, you need to generate two SSH Key
Step 2: Configure ~/.ssh/config
Note: In order to access the different account, you must use the hostname specified inside the ~/.ssh/config file. Here is my example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ cat .ssh/config Host * ServerAliveInterval 240 # garyliu1119 account Host github1119.com HostName github.com AddKeysToAgent yes IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes # garyliu19 account Host github19.com HostName github.com AddKeysToAgent yes IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes |
1 | git clone -b develop git@github19.com:garyliu19/FN-External-API.git |
Tuesday, October 6, 2020
Dataweave 2.0: Convert Calendar Date To Julian Date
A Julian date is commonly used in main frame. It's format is YYDDD. DDD is the date since begining of the year.
In DetaWeave 2.0 the code should be as the following:
julianNow: now() as String {format: "YYD"}, julianAt: "07-10-2020" as Date {format: "dd-MM-yyyy"} as String {format: "YYD"}
Tuesday, September 22, 2020
Mulesoft Send Email With CSV Attachment
Introduction
This blog explain the tricks to convert csv data to plain text for the purpuse of send csv as attachment. The Mulesoft document can be found at here1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < file:read path = "/file.json" > < email:send config-ref = "config" > < email:to-addresses > < email:to-address value = "example@domain.com" > </ email:to-address ></ email:to-addresses > < email:body > < email:content > <!--[CDATA["<h1--> Hello this is an important message"]]></ email:content > </ email:body > < email:attachments > #[{ 'csv-attachment' : payload }] </ email:attachments > </ email:send > </ file:read > |
Code Details
As shown in the follow snapshot, we need to perform two transformations:- from csv to base64 binary
- from base64 binary to plain text>
Step 1: Initial code JSON to CSV
1 2 3 4 5 6 7 8 9 10 11 12 13 | %dw 2.0 output application/csv separator="," --- [ { name: "Gary Liu", ssn: "1234" }, { name: "John Smith", ssN: "4567" } ] |
Step 2: CSV to Base64 Binary
1 2 3 4 5 | %dw 2.0 import * from dw::core::Binaries output text/plain --- toBase64(write (payload, 'application/csv' )) |
Step 3: Base64 Binary to CSV Plain Text
1 2 3 4 5 | %dw 2.0 import fromBase64 from dw::core::Binaries output text/plain --- fromBase64(payload) |
Take Aways
The line of thinking is that Mule runtime engine internally keeps that data as array of records. This java object cannot direct convert to plain text. The code is really mimic the way we store the data to file and then read the data from file to plain text.Monday, August 10, 2020
Mule Develop Tricks: Get The Phone Number
The Requirement
The phone number from customer web site is free string. It can take the following forms:
- +1 (219) 555-8888
- +1 219-555-8888
- (219) 555 8888
- 219 555 8888
- 219 555-8888
- 219-555-8888
- -1-219-555-8888
There could be other form.
We need to use dataweave 2.0 to extract 10 digits of the phone number to: 2195558888.
Solution
%dw 2.0
output application/json
---
{
mobile: (payload.mobilePhone replace /([^0-9.]+)/ with(""))[-10 to -1]
}
Take Aways
- dataweave built in function replace / / with ()
- substring [-10 to -1] take last 10 digits
Saturday, February 15, 2020
Prototyping Mule OAuth2 Client Application
Introduction
This article explains how to prototype Mule OAuth 2.0 client application with grant type of "Client Credentials", which is most popular grant type for Mule integration. Most modern APIs enforce OAuth2.0 security policies. OAuth2.0 has the following Grant Types (The detailed explanation can be found here):- Authorization Code
- PKCE
- Client Credentials
- Device Code
- Refresh Token
- Legacy: Implicit Flow
- Legacy: Password Grant
- Using postman to retrieve access token
- Prototyping retrieve access token using cURL
- Mule flow to retrieve access token
- Sample flow using the access token with caching scope
OAuth 2.0 With Client Credential Grant Type
In order to access the OAuth2.0 enabled APIs, we first have to retrieve the access token from the Identity Providers. Then we can access the API by passing the access token. The parameters for Client Credential can be the following:- grant_type (required)
- scope (optional)
- client_id (required)
- client_secret (required)
1 | echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | base64 |
1 | echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | openssl enc -base64 |
Using Postman to Retrieve Access Token
The postman is the best tool to do prototyping for the OAuth 2 client. The following snapshot shows the setup of the Postman:for body: for Headers: for Authorization:
cURL Solution
Once we have the postman, the solution of cUrl is very straight forward.1 2 3 4 5 6 7 8 9 10 11 12 13 | $ cat oauth2-client.sh #!/bin/bash # CLIENT_ID=MY-CLIENT-ID-GOES-HERE-WITHOUT-QUOTE CLIENT_SECRET=YOUR-CLIENT-SECRET-GOES-HERE-WITHOUT-QUOTE OAUTH_HEADER=$( echo -n "${CLIENT_ID}:${CLIENT_SECRET}" | base64) -H "Content-Type: application/x-www-form-urlencoded" \ -H "Authorization: Basic ${OAUTH_HEADER}" \ -XPOST https: //login .microsoftonline.com /keurig .onmicrosoft.com /oauth2/v2 .0 /token |
Mule Application Solution - Retrieve access_token
The mule application flow for retrieving access token is the following: The Data-Weave transformation code is the following:1 2 3 4 5 6 7 | %dw 2.0 output application/x-www-form-urlencoded --- { grant_type: "client_credentials" , } |
1 2 3 4 5 6 7 | < http:request method = "POST" doc:name = "Request" doc:id = "65997138-84c0-48b3-8347-68abf386b3a1" config-ref = "HTTP_Request_configuration" path = "/keurig.onmicrosoft.com/oauth2/v2.0/token" > < http:headers > <!--[CDATA[#[output application/java --- { "Content-Type" : "application/x-www-form-urlencoded" }]]]--> </ http:headers > </ http:request > |
1 2 3 4 5 6 7 8 9 10 | < http:request-config name = "HTTP_Request_configuration" doc:name = "HTTP Request configuration" doc:id = "fb6e2c7d-010f-4141-bafe-a4a50c4fc540" > < http:request-connection protocol = "${oauth2.protocol}" host = "${oauth2.host}" port = "${oauth2.port}" > < reconnection > < reconnect frequency = "5000" > </ reconnect ></ reconnection > < http:authentication > < http:basic-authentication username = "${secure::oauth2.client.id}" password = "${secure::oauth2.client.secret}" > </ http:basic-authentication ></ http:authentication > </ http:request-connection > </ http:request-config > |
1 2 3 4 5 6 | { "token_type" : "Bearer" , "expires_in" : 3599, "ext_expires_in" : 3599, "access_token" : "eyJ0eXAi......" } |
Mule Application Solution - Use access_token
The following diagram shows the usage of the access_token. The access_token is passed to the server as header. As you can see, we need to use cache scope. This allow us to avoid calling the server if the token is not expired. In this case, the token will expire in one hour. Thus our object store TTL should be less then 60 minutes.Sunday, February 2, 2020
Anypoint Runtime Fabric : Part 1 - Pre-Installation Network Checking
Introduction
In the next few weeks, I will write more details about Mule runtime fabric installation, configuration, and troubleshooting. This article is about to verify the network connectivity before install Anypoint Runtime Fabric (RTF). In order to install Anypoint RTF, the network firewall must open. The details about the port and host whitelisting can be find here: https://docs.mulesoft.com/runtime-fabric/1.4/install-port-reqs.Tools for Network Connectivity Checking
The following tools should be installed and configure on the Linux system:- nslookup
- curl
- nc
- openssl
- chrony
1 | nslookup anypoint.mulesoft.com |
1 2 3 4 5 | Name: anypoint-prod-936665732.us-east-1.elb.amazonaws.com Address: 52.87.103.123 Name: anypoint-prod-936665732.us-east-1.elb.amazonaws.com Address: 3.217.25.79 ... |
1 2 3 4 5 | $ cat /etc/resolv.conf # Generated by NetworkManager search gmcr.com nameserver 10.64.2.13 nameserver 10.124.240.104 |
1 | $ curl -sk https: //anypoint .mulesoft.com:443 |
1 2 3 4 | < title >301 Moved Permanently</ title > < center >< h1 >301 Moved Permanently</ h1 ></ center > < hr >< center >nginx</ center > |
NTP - Network Time Protocol
In order for Anypoint RTF to work, we must make sure NTP is working in the system. Linux use chrony to perform NTP. Run the following command:1 | chronyc tracking |
1 2 3 4 5 6 7 8 9 10 11 12 13 | Reference ID : A3EDDA13 (toddadev.your.org) Stratum : 2 Ref time (UTC) : Mon Feb 03 00:18:55 2020 System time : 0.000235624 seconds slow of NTP time Last offset : -0.000094732 seconds RMS offset : 0.000081353 seconds Frequency : 16.987 ppm slow Residual freq : -0.003 ppm Skew : 0.062 ppm Root delay : 0.025243049 seconds Root dispersion : 0.000095447 seconds Update interval : 514.2 seconds Leap status : Normal |
1 2 3 4 5 6 7 8 | # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.rhel.pool.ntp.org iburst #server 1.rhel.pool.ntp.org iburst #server 2.rhel.pool.ntp.org iburst #server 3.rhel.pool.ntp.org iburst server server 10.70.0.200 ... |
1 2 | sudo systemctl restart chronyd sudo systemctl enable chronyd |
1 | chronyc sources |
Network Connectivity Test Scripts
Mulesoft provided a scripts totest the network connectivity at here If you have gone through the test in the previous sections, this script should run with the following results. Once the above procedures are all done, we are read to perform the installation for Anypoint Runtime Fabric. I will cover that in the following articles.
Subscribe to:
Posts (Atom)
Anypoint Studio Error: The project is missing Munit lIbrary to run tests
Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...
-
Introduction MuleSoft has changed many stuff for the last year. One of the questions developer will ask is how to change the logging level....
-
Introduction The video for this blog is available at: http://www.youtube.com/watch?v=wVCmik-2xAM&feature=youtu.be Mule and JBoss ...
-
Congratulation Gary! First of all, I must congratulate myself for getting this done. For almost a year, I have been thinking to take the e...