Apigee Assignment - Based on the Service Callout policy and JSON protection.

·

2 min read

Apigee Assignment - Based on the Service Callout policy and JSON protection.

CREATE 2 Proxy JSON_threat and JSON to XML.
PROXY 1 should be attached to the JSON threat protection policy and other policies if needed.
PROXY 2 should be attached with service callout -Proxy chaining policy, JSON to XML policy, and other policies if needed,
NOTE: JSON threat protection policy
- The number of array elements should not exceed 5.
- Container depth should not exceed 6.
- Name length should not exceed 15.
A client should send a request message with JSON data and get XML data in response.

Implementation

Create No target API PROXY 1 - JSON_threat.

Proxyendpoint - Preflow - Request - ADD JSON threat protection policy.

JSON threat protection policy code should look like below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONThreatProtection continueOnError="false" enabled="true" name="JSON-Threat-Protection-1">
    <DisplayName>JSON Threat Protection-1</DisplayName>
    <Properties/>
    <ArrayElementCount>5</ArrayElementCount>
    <ContainerDepth>6</ContainerDepth>
    <ObjectEntryCount>5</ObjectEntryCount>
    <ObjectEntryNameLength>15</ObjectEntryNameLength>
    <Source>request</Source>
    <StringValueLength>15</StringValueLength>
</JSONThreatProtection>

Save and Deploy.
PROXY 1 detects malicious JSON data based on the configured limits on JSON structure.


Create No Target PROXY 2 - JSONtoXML.

Proxyendpoint - Preflow - Request - ADD Service callout proxy chaining policy (from here call the JSON_threat proxy).

Proxyendpint - Preflow - Request - ADD Assign message policy (To add the response of JSON_threat proxy to Request message.
The assign message policy code should look like the one below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Copy source="calloutResponse">
        <Payload>true</Payload>
    </Copy>
    <Set>
        <!-- posting the XML msg to request-->
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Proxyendpoint - Postflow = Request - Add JSON to XML policy.
The JSON to XML policy code should look like the one below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONToXML continueOnError="false" enabled="true" name="JSON-to-XML-1">
    <DisplayName>JSON to XML-1</DisplayName>
    <Properties/>
    <Options>
        <NullValue>NULL</NullValue>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
        <NamespaceSeparator>:</NamespaceSeparator>
        <TextNodeName>#text</TextNodeName>
        <AttributeBlockName>#attrs</AttributeBlockName>
        <AttributePrefix>@</AttributePrefix>
        <InvalidCharsReplacement>_</InvalidCharsReplacement>
        <ObjectRootElementName>Root</ObjectRootElementName>
        <ArrayRootElementName>Array</ArrayRootElementName>
        <ArrayItemElementName>Item</ArrayItemElementName>
    </Options>
    <OutputVariable>request</OutputVariable>
    <Source>request</Source>
</JSONToXML>

Save and Deploy.

A client sends a POST request message to PROXY 2 (JSONtoXML) with JSON data. In response, client should get XML format data.

JSON data is based on the configured limits in the JSON threat protection policy.

{
    "BillNumber": "8888",
    "BillDate": "2022-02-23",
    "Customer": {
        "Name": "Prashant",
        "Mail": "nk@gmail.com",
        "Address": {
            "Strdr1": "Bay area",
            "Country": "USA"
        }
    },
    "pDetails": {
        "paymentType": "CARD"
    },
    "pDetails2": {
        "paymentType": "CARD"
    }

}