what is the easy way to manually parse the RAML file in mule?

1.1k views Asked by At

In mule application I am trying to parse RAML file. I knew that APIKit is doing same as it creates flows after parsing the RAML file. But still, what if I want to parse it in middle of the flow manually?

I have seen the raml parsers available but not finding the proper usage of those javascript libraries or java libraries on how to use them in mule application..

2

There are 2 answers

0
Anirban Sen Chowdhary On BEST ANSWER

Yes you can parse your RAML in your java application using java class or groovy component implementing java.
There are java parser available like RamlModelBuilder which you can use to parse your application RAML like validation of your RAML file, getting APIs name, getting all resources name, method name, scopes, security schema and their names, query parameters, headers and many more...
Just check the example how it is used here. You can simply create a java class and get your RAML parsed

https://github.com/anirban37/Anirban-Custom-Oauth-Policy/blob/master/Anirban-RAML-Oauth-V3/OauthPolicies.xml#L594.

ramlModelResult = new RamlModelBuilder().buildApi(ac.getRaml())

will give you the current RAML file access of the application in the java class

0
Ryan Carter On

Theres nothing in Mule to work with the RAML file at runtime.

But you can create any Java component that uses RAML Java libraries and invoke that from Mule in your flows.

The Mule4 SDK is one way of extending mule through Java.

For more information on Mule SDK can be found here https://mule4-docs.mulesoft.com/mule-sdk/v/1.1/

You can also invoke Java classes but they need to be decoupled from the Mule API and you need to extract any variables, properties or payload and explicitly pass the values to your class. For example passing a static String and a flow var as arguments to a Java constructor:

<java:new class="com.foo.AppleEater" constructor="MyClass(String, Apple)">
  <java:args>#[{name: 'some string arg', apple: vars.apple}]</java:args>
</java:new>

In your class you could use the RAML Java libraries, and pass the file or path to RAML file to load from the classpath.

More on Java integration with Mule 4 here: https://docs.mulesoft.com/mule-runtime/4.1/intro-java-integration