Last active
October 23, 2019 16:27
-
-
Save fjunior87/dac1c8e239e92bdf00a911d9d91fdbd3 to your computer and use it in GitHub Desktop.
Image API Creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Faltando somente o nome do arquivo dinamico