DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular

Trending

  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team
  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Upload Image Files With a WSO2 REST API and VFS

How to Upload Image Files With a WSO2 REST API and VFS

In this post, we are going to show you how we can use a WSO2 REST API and VFS to upload images and save them to your disk.

By 
Francisco Ribeiro
·
Updated Oct. 29, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

<?xml version="1.0" encoding="UTF-8"?>
<api context="/image" name="ImageAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" uri-template="/add">
        <inSequence>
            <log level="full" />
            <!--
                1. Reading the JSON attributes from the request payload;
            -->
            <property expression="json-eval($.fileName)" name="fileName" scope="default" type="STRING"/>
            <property expression="json-eval($.fileContent)" name="fileContent" scope="default" type="STRING"/>
            <!--
                2. Creating the Payload for a binary payload.
            -->
            <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. Even using a binary payload, the Axis2 frameworks keeps considering the payload as text.
                We use a script mediator to set the fist node as binary;
            -->
            <script language="js"><![CDATA[var binaryNode =       
      mc.getEnvelope().getBody().getFirstElement().getFirstOMChild();  
   binaryNode.setBinary(true);]]></script>
            <log level="full" >
            <property name="fileName" expression="$ctx:fileName"></property>
            </log>
         <!--
            4. We set the name of the file that will be saved in the disk using a VFS parameter
         -->
         <property name="transport.vfs.ReplyFileName" expression="$ctx:fileName" scope="transport"/>
         <log level="full">
            <property name="fileName" expression="$ctx:fileName"/>
         </log>
         <!--
            5. We need to set the OUT_ONLY property and remove the REST_URL_POSTFIX to make the API to be able to save the file
            and set the expected file name.
         -->
         <property name="OUT_ONLY" value="true"/>
         <property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
         <!--
            6. We use the call mediator in order to invoke a vfs endpoint with the target directory.
         -->
         <property name="messageType" value="application/octet-stream" scope="axis2"/>
         <call>
            <endpoint>
               <address uri="vfs:file:///E:/test/"/>
            </endpoint>
         </call>
         <!--
            7. We define a success message to be the respose of the REST API request.
         -->
         <payloadFactory media-type="json">
            <format>
                {                
                    "status": "success",                
                    "statusMessage" : "Image Uploaded"               
                }            
             </format>
            <args/>
         </payloadFactory>
         <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Let us explain each part of the above code.

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

Our API expects a payload like below:

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

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

1. Reading the JSON Payload Content

The first step is to read the JSON payload properties using the property mediator and JSONPath using the 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 a VFS endpoint, having the target folder as the address.

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

7. We Respond With a Success Message to the Client

We use 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 a WSO2 REST API and VFS to upload image files and save them to disk. I hope you enjoyed.

See you in the next post!

API REST Web Protocols Upload

Published at DZone with permission of Francisco Ribeiro. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook