From c7d437d2076ccea2f4192b0ac188e99368dccb4f Mon Sep 17 00:00:00 2001 From: chathurabuddi Date: Tue, 22 Dec 2020 21:19:38 +0530 Subject: [PATCH] exclude error responses from response filtering. EXTGW-626 INTGW-1280 --- .../ResponseFilterMediator.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/components/response-filter/src/main/java/com/wso2telco/dep/responsefilter/ResponseFilterMediator.java b/components/response-filter/src/main/java/com/wso2telco/dep/responsefilter/ResponseFilterMediator.java index 0b4012094..40e0b08e5 100644 --- a/components/response-filter/src/main/java/com/wso2telco/dep/responsefilter/ResponseFilterMediator.java +++ b/components/response-filter/src/main/java/com/wso2telco/dep/responsefilter/ResponseFilterMediator.java @@ -15,12 +15,16 @@ */ package com.wso2telco.dep.responsefilter; +import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Iterator; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.wso2telco.core.dbutils.DbUtils; import com.wso2telco.core.dbutils.util.DataSourceNames; import lk.chathurabuddi.json.schema.constants.FreeFormAction; @@ -51,7 +55,9 @@ public boolean mediate(MessageContext messageContext) { String filterSchema = findFilterSchema(userId, appName, api, httpVerb, resource); if (filterSchema != null) { String filteredJson = new JsonSchemaFilter(filterSchema, jsonString, FreeFormAction.DETACH).filter(); - JsonUtil.getNewJsonPayload(axis2MessageContext, filteredJson, true, true); + if (isNonEmptyJson(filteredJson) || isMatchingPayload(jsonString, filterSchema)) { + JsonUtil.getNewJsonPayload(axis2MessageContext, filteredJson, true, true); + } } } } catch (Exception e) { @@ -61,6 +67,31 @@ public boolean mediate(MessageContext messageContext) { return true; } + // compare the root elements of the payload according to the filter schema + private boolean isMatchingPayload(String jsonString, String filterSchema) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + JsonNode json = mapper.readTree(jsonString); + JsonNode schema = mapper.readTree(filterSchema); + String rootElementType = schema.findPath("type").asText(); + return !((json.isArray() && !"array".equals(rootElementType)) || + ("object".equals(rootElementType) && !rootElementsMatched(json, schema))); + } + + private boolean rootElementsMatched(JsonNode json, JsonNode schema) { + Iterator properties = schema.findPath("properties").fieldNames(); + while (properties.hasNext()) { + String property = properties.next(); + if (json.has(property)) { + return true; + } + } + return false; + } + + private boolean isNonEmptyJson(String filteredJson) { + return filteredJson != null && !filteredJson.isEmpty() && !"{}".equals(filteredJson); + } + public String findFilterSchema(String sp, String application, String api, String httpVerb, String resource) throws Exception { Connection connection = null; PreparedStatement statement = null;