How To Upload Image Files with WSO2 Rest API and VFS

In this post we are going to show how we can use WSO2 Rest API and VFS to upload images and save them to disk.

Below we can see the code of the REST API that we defined on Enterprise Integrator:

Let us explain each part of the above code:

We declared a REST API named ImageAPI that is respoding to the context /image. This API has only one resource /add that will respond for POST requests.

Our API expects a payload like below:

{
  "fileName":"imageNew2.png",
  "fileContent": "Base64 IMAGECONTENT"
}

We send to the API the file name and the image content Base64 encoded.

1. Reading the JSON Payload Content

The first step we read the JSON payload properties using the property mediator and JSONPath using json-eval method.

<property expression="json-eval($.fileName)" name="fileName" scope="default" type="STRING"/>
<property expression="json-eval($.fileContent)" name="fileContent" scope="default" type="STRING"/>

2. Build the Payload to save the image to disk

We use the payloadFactory mediator to build a binary payload. Using the fileContent property defined in the step above as parameter:

<payloadFactory media-type="xml">
    <format>
        <ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">$1</ns:binary>
    </format>
    <args>
        <arg evaluator="xml" expression="$ctx:fileContent"/>
    </args>
</payloadFactory>

3. Set the binary property for the payload data

Even using the binary payload the framework still handles it as a text payload. We use a script mediator to set the binary flag to the first node.

<script language="js"><![CDATA[var binaryNode =       
      mc.getEnvelope().getBody().getFirstElement().getFirstOMChild();  
   binaryNode.setBinary(true);]]>
</script>

4. Set the file name to be saved

We defined a VFS parameter to specify the name of the file that will be saved in the Disk.

<property name="transport.vfs.ReplyFileName" expression="$ctx:fileName" scope="transport"/>

5. Set some required properties to enable the file saving

We need to set the properties below in order to enable the API to save the file and use the specified file name defined in the previous steps.

<property name="OUT_ONLY" value="true"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>

6. Write the image to the disk using VFS endpoint

We send the payload to the disk as a file using a call mediator with vfs endpoint having the target folder as the address.

<call>
   <endpoint>
      <address uri="vfs:file:///E:/test/"/>
   </endpoint>
</call>

7. We respond a success message to the client

We use a payloadFactory to create a simple response message to the client that made the request.

<payloadFactory media-type="json">
   <format>
       {                
           "status": "success",                
           "statusMessage" : "Image Uploaded"               
       }            
    </format>
   <args/>
</payloadFactory>

Conclusion

Now we are able to use WSO2 Rest API and VFS to upload image files and save the to disk. I hope you enjoyed.

See you in the next post!

comments powered by Disqus