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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • 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
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

Trending

  • Strategies for Securing E-Commerce Applications
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  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 user avatar
Francisco Ribeiro
·
Updated Oct. 29, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
21.4K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 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
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!