-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediation-sequence.xml
61 lines (53 loc) · 2.93 KB
/
mediation-sequence.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="size-limit-mediation"
xmlns="http://ws.apache.org/ns/synapse">
<!-- defining the payload size limit class mediator in the sequence with the respective size limit in MB unit -->
<!-- the class mediator will return a boolean value with the property named "payload-size-too-large" to specify whether the message is too large or not -->
<class name="com.sample.mediator.PayloadSizeLimitMediator">
<!-- the size limit has been configured to 10MB limit, change this according to your requirement -->
<property name="sizeLimit" value="10" />
<!-- specifying the API name. You can use either $ctx:API_NAME or $ctx:SYNAPSE_REST_API -->
<property name="apiName" expression="$ctx:API_NAME" />
<!-- specify the direction flow of the execution whether in flow or out flow -->
<property name="flowDirection" value="In flow" />
</class>
<!-- a custom log mediator to log the property returned by class mediator -->
<log level="custom">
<property name="is-payload-larger" expression="get-property('payload-size-too-large')" />
</log>
<!-- filter and send a custom error response based on the class mediator output property -->
<!-- if the property 'payload-size-too-large' is set to true means, that the payload size is larger than the defined value -->
<filter source="get-property('payload-size-too-large')" regex="true">
<then>
<!-- a custom log mediator -->
<log level="custom">
<property name="message" value="The payload size is larger than the specified limit" />
</log>
<!-- payload factory mediator to specify custom response -->
<payloadFactory media-type="json">
<format>
{
"status": $1,
"message": "$2"
}
</format>
<args>
<arg value="413" />
<arg value="payload is too large" />
</args>
</payloadFactory>
<!-- change the HTTP status code. Below is set to 400 status code -->
<property name="HTTP_SC" value="413" scope="axis2" />
<!-- response back to the client with application/json format -->
<property name="messageType" value="application/json" scope="axis2" />
<!-- respond back to the client if the payload size is too large with the above-mentioned custom response -->
<respond />
</then>
<else>
<!-- a custom log mediator to log the flow -->
<log level="custom">
<property name="message" value="The payload size is not larger than specified limit" />
</log>
</else>
</filter>
</sequence>