Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Last active December 8, 2020 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricston-git/9248956 to your computer and use it in GitHub Desktop.
Save ricston-git/9248956 to your computer and use it in GitHub Desktop.
Raml + mulerequester example
public class GetImageListComponent implements Callable {
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
File folder = new File("images");
File[] listOfFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
});
return listOfFiles;
}
}
{
"images": [
{ "image": {"src": "Images/Sun.jpg"}},
{ "image": {"src": "Images/Sun.jpg"}}
]
}
#%RAML 0.8
---
title: Image hosting API
version: v1.0
baseUri: http://image-hosting/api
mediaType: application/json
schemas:
- image: !include schema/image.xsd
- images: !include schema/images.xsd
documentation:
- title: Introduction
content: |
API to manage persons
/images:
get:
description: Retrieves all images
responses:
200:
body:
application/json:
schema: images
example: !include schema/images.json
/{image}:
get:
responses:
200:
body:
"*/*":
public class ListToHtml extends AbstractMessageTransformer{
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String html = "<html><head></head><body><ul>";
File[] fileArray = (File[]) message.getPayload();
String contextUri = message.getInboundProperty("http.context.uri");
for (File file : fileArray) {
html += "<li><a href=\"" + contextUri + "/"+ file.getPath().replace(".jpg", "") + "\">"+file.getName()+"</a></li>";
}
html += "</ul></body></html>";
return html;
}
}
public class ListToMap extends AbstractMessageTransformer {
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
// TODO Auto-generated method stub
File[] fileArray = (File[]) message.getPayload();
//create map of maps images map
HashMap<String, Map<String, String>> images = new HashMap<String, Map<String, String>>();
String contextUri = message.getInboundProperty("http.context.uri");
for (File file : fileArray) {
Map<String, String> image = new HashMap<String, String>();
image.put("src", contextUri + "/images/" + file.getName());
images.put(file.getName(), image);
}
return images;
}
}
<file:connector name="fileconn" autoDelete="false" />
<mulerequester:config name="Mule_Requester" doc:name="Mule Requester" />
<apikit:config name="test-config" raml="test.raml" consoleEnabled="true" consolePath="console" doc:name="Router" />
<flow name="images-main" doc:name="images-main">
<http:inbound-endpoint address="http://localhost:8081/images/api"
doc:name="HTTP" exchange-pattern="request-response" />
<apikit:router config-ref="images-config" doc:name="APIkit Router" />
<exception-strategy ref="images-apiKitGlobalExceptionMapping"
doc:name="Reference Exception Strategy" />
</flow>
<flow name="get:/images:images-config" doc:name="get:/images:images-config">
<component class="org.images.service.GetImageListComponent"
doc:name="GetImaeListComponent" />
<custom-transformer class="org.images.service.ListToMap"
doc:name="FileArrayToMap" />
<json:object-to-json-transformer doc:name="MapToJson"/>
</flow>
<flow name="get:/images/{image}:images-config" doc:name="get:/images/{image}:images-config">
<mulerequester:request config-ref="Mule_Requester"
resource="file:///path_to_images/#[flowVars.image].jpg"
doc:name="Mule Requester" />
<set-property propertyName="Content-Type" value="image/jpeg"
doc:name="Set Content-Type=image/jpeg" />
</flow>
@ravi-dhyani8881
Copy link

schemas:

  • image: !include schema/image.xsd
  • images: !include schema/images.xsd

Can you please share this schema file also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment